@@ -887,6 +887,29 @@ def _fix_up_method_description(method_desc, root_desc, schema):
887
887
return path_url , http_method , method_id , accept , max_size , media_path_url
888
888
889
889
890
+ def _fix_up_media_path_base_url (media_path_url , base_url ):
891
+ """
892
+ Update the media upload base url if its netloc doesn't match base url netloc.
893
+
894
+ This can happen in case the base url was overridden by
895
+ client_options.api_endpoint.
896
+
897
+ Args:
898
+ media_path_url: String; the absolute URI for media upload.
899
+ base_url: string, base URL for the API. All requests are relative to this URI.
900
+
901
+ Returns:
902
+ String; the absolute URI for media upload.
903
+ """
904
+ parsed_media_url = urllib .parse .urlparse (media_path_url )
905
+ parsed_base_url = urllib .parse .urlparse (base_url )
906
+ if parsed_media_url .netloc == parsed_base_url .netloc :
907
+ return media_path_url
908
+ return urllib .parse .urlunparse (
909
+ parsed_media_url ._replace (netloc = parsed_base_url .netloc )
910
+ )
911
+
912
+
890
913
def _urljoin (base , url ):
891
914
"""Custom urljoin replacement supporting : before / in url."""
892
915
# In general, it's unsafe to simply join base and url. However, for
@@ -1133,6 +1156,7 @@ def method(self, **kwargs):
1133
1156
# Use the media path uri for media uploads
1134
1157
expanded_url = uritemplate .expand (mediaPathUrl , params )
1135
1158
url = _urljoin (self ._baseUrl , expanded_url + query )
1159
+ url = _fix_up_media_path_base_url (url , self ._baseUrl )
1136
1160
if media_upload .resumable ():
1137
1161
url = _add_query_parameter (url , "uploadType" , "resumable" )
1138
1162
0 commit comments