Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: discovery uses V2 when version is None #975

Merged
merged 1 commit into from Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions googleapiclient/discovery.py
Expand Up @@ -29,7 +29,7 @@

# Standard library imports
import copy

from collections import OrderedDict
try:
from email.generator import BytesGenerator
except ImportError:
Expand Down Expand Up @@ -241,7 +241,8 @@ def build(
else:
discovery_http = http

for discovery_url in (discoveryServiceUrl, V2_DISCOVERY_URI):
for discovery_url in \
_discovery_service_uri_options(discoveryServiceUrl, version):
requested_url = uritemplate.expand(discovery_url, params)

try:
Expand Down Expand Up @@ -270,6 +271,29 @@ def build(
raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, version))


def _discovery_service_uri_options(discoveryServiceUrl, version):
"""
Returns Discovery URIs to be used for attemnting to build the API Resource.

Args:
discoveryServiceUrl:
string, the Original Discovery Service URL preferred by the customer.
version:
string, API Version requested

Returns:
A list of URIs to be tried for the Service Discovery, in order.
"""

urls = [discoveryServiceUrl, V2_DISCOVERY_URI]
# V1 Discovery won't work if the requested version is None
if discoveryServiceUrl == V1_DISCOVERY_URI and version is None:
logger.warning(
"Discovery V1 does not support empty versions. Defaulting to V2...")
urls.pop(0)
return list(OrderedDict.fromkeys(urls))


def _retrieve_discovery_doc(url, http, cache_discovery,
cache=None, developerKey=None, num_retries=1):
"""Retrieves the discovery_doc from cache or the internet.
Expand Down
31 changes: 31 additions & 0 deletions tests/test_discovery.py
Expand Up @@ -837,6 +837,37 @@ def test_api_endpoint_override_from_client_options_dict(self):
)
self.assertEqual(zoo._baseUrl, api_endpoint)

def test_discovery_with_empty_version_uses_v2(self):
http = HttpMockSequence(
[
({"status": "200"}, read_datafile("zoo.json", "rb")),
]
)
build("zoo", version=None, http=http, cache_discovery=False)
validate_discovery_requests(self, http, "zoo", None, V2_DISCOVERY_URI)

def test_discovery_with_empty_version_preserves_custom_uri(self):
http = HttpMockSequence(
[
({"status": "200"}, read_datafile("zoo.json", "rb")),
]
)
custom_discovery_uri = "https://foo.bar/$discovery"
build(
"zoo", version=None, http=http,
cache_discovery=False, discoveryServiceUrl=custom_discovery_uri)
validate_discovery_requests(
self, http, "zoo", None, custom_discovery_uri)

def test_discovery_with_valid_version_uses_v1(self):
http = HttpMockSequence(
[
({"status": "200"}, read_datafile("zoo.json", "rb")),
]
)
build("zoo", version="v123", http=http, cache_discovery=False)
validate_discovery_requests(self, http, "zoo", "v123", V1_DISCOVERY_URI)


class DiscoveryRetryFromHttp(unittest.TestCase):
def test_repeated_500_retries_and_fails(self):
Expand Down