Skip to content

Commit

Permalink
fix: Fix media upload URI when API endpoint is overridden with client…
Browse files Browse the repository at this point in the history
…_opions.api_endpoint (#1992)

* Fix media upload URI when API endpoint is overridden with client_options.api_endpoint

This can happen when using Private Service Connect to access the APIs.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

Co-authored-by: Anthonios Partheniou <partheniou@google.com>
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 7, 2022
1 parent fa65fca commit d0f8a05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
24 changes: 24 additions & 0 deletions googleapiclient/discovery.py
Expand Up @@ -887,6 +887,29 @@ def _fix_up_method_description(method_desc, root_desc, schema):
return path_url, http_method, method_id, accept, max_size, media_path_url


def _fix_up_media_path_base_url(media_path_url, base_url):
"""
Update the media upload base url if its netloc doesn't match base url netloc.
This can happen in case the base url was overridden by
client_options.api_endpoint.
Args:
media_path_url: String; the absolute URI for media upload.
base_url: string, base URL for the API. All requests are relative to this URI.
Returns:
String; the absolute URI for media upload.
"""
parsed_media_url = urllib.parse.urlparse(media_path_url)
parsed_base_url = urllib.parse.urlparse(base_url)
if parsed_media_url.netloc == parsed_base_url.netloc:
return media_path_url
return urllib.parse.urlunparse(
parsed_media_url._replace(netloc=parsed_base_url.netloc)
)


def _urljoin(base, url):
"""Custom urljoin replacement supporting : before / in url."""
# In general, it's unsafe to simply join base and url. However, for
Expand Down Expand Up @@ -1133,6 +1156,7 @@ def method(self, **kwargs):
# Use the media path uri for media uploads
expanded_url = uritemplate.expand(mediaPathUrl, params)
url = _urljoin(self._baseUrl, expanded_url + query)
url = _fix_up_media_path_base_url(url, self._baseUrl)
if media_upload.resumable():
url = _add_query_parameter(url, "uploadType", "resumable")

Expand Down
15 changes: 15 additions & 0 deletions tests/test_discovery.py
Expand Up @@ -58,6 +58,7 @@
V1_DISCOVERY_URI,
V2_DISCOVERY_URI,
ResourceMethodParameters,
_fix_up_media_path_base_url,
_fix_up_media_upload,
_fix_up_method_description,
_fix_up_parameters,
Expand Down Expand Up @@ -374,6 +375,20 @@ def test_fix_up_method_description_insert(self):
result, (path_url, http_method, method_id, accept, max_size, media_path_url)
)

def test_fix_up_media_path_base_url_same_netloc(self):
result = _fix_up_media_path_base_url(
"https://www.googleapis.com/upload/foo",
"https://www.googleapis.com/upload/bar",
)
self.assertEqual(result, "https://www.googleapis.com/upload/foo")

def test_fix_up_media_path_base_url_different_netloc(self):
result = _fix_up_media_path_base_url(
"https://www.googleapis.com/upload/foo",
"https://www.example.com/upload/bar",
)
self.assertEqual(result, "https://www.example.com/upload/foo")

def test_urljoin(self):
# We want to exhaustively test various URL combinations.
simple_bases = ["https://www.googleapis.com", "https://www.googleapis.com/"]
Expand Down

0 comments on commit d0f8a05

Please sign in to comment.