Skip to content

Commit cd4e8f4

Browse files
author
Dmitry Frenkel
authored
fix: discovery uses V2 when version is None (#975)
Passing version=None to discovery.build(), with intent to always get the latest version of the discovery document, no longer attempts to use Discovery V1, which fails with Bad Request errors. Instead, it uses Discovery V2, which supports null version use case. Fixes: #971
1 parent d059ad8 commit cd4e8f4

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

Diff for: googleapiclient/discovery.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# Standard library imports
3131
import copy
32-
32+
from collections import OrderedDict
3333
try:
3434
from email.generator import BytesGenerator
3535
except ImportError:
@@ -241,7 +241,8 @@ def build(
241241
else:
242242
discovery_http = http
243243

244-
for discovery_url in (discoveryServiceUrl, V2_DISCOVERY_URI):
244+
for discovery_url in \
245+
_discovery_service_uri_options(discoveryServiceUrl, version):
245246
requested_url = uritemplate.expand(discovery_url, params)
246247

247248
try:
@@ -270,6 +271,29 @@ def build(
270271
raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, version))
271272

272273

274+
def _discovery_service_uri_options(discoveryServiceUrl, version):
275+
"""
276+
Returns Discovery URIs to be used for attemnting to build the API Resource.
277+
278+
Args:
279+
discoveryServiceUrl:
280+
string, the Original Discovery Service URL preferred by the customer.
281+
version:
282+
string, API Version requested
283+
284+
Returns:
285+
A list of URIs to be tried for the Service Discovery, in order.
286+
"""
287+
288+
urls = [discoveryServiceUrl, V2_DISCOVERY_URI]
289+
# V1 Discovery won't work if the requested version is None
290+
if discoveryServiceUrl == V1_DISCOVERY_URI and version is None:
291+
logger.warning(
292+
"Discovery V1 does not support empty versions. Defaulting to V2...")
293+
urls.pop(0)
294+
return list(OrderedDict.fromkeys(urls))
295+
296+
273297
def _retrieve_discovery_doc(url, http, cache_discovery,
274298
cache=None, developerKey=None, num_retries=1):
275299
"""Retrieves the discovery_doc from cache or the internet.

Diff for: tests/test_discovery.py

+31
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,37 @@ def test_api_endpoint_override_from_client_options_dict(self):
837837
)
838838
self.assertEqual(zoo._baseUrl, api_endpoint)
839839

840+
def test_discovery_with_empty_version_uses_v2(self):
841+
http = HttpMockSequence(
842+
[
843+
({"status": "200"}, read_datafile("zoo.json", "rb")),
844+
]
845+
)
846+
build("zoo", version=None, http=http, cache_discovery=False)
847+
validate_discovery_requests(self, http, "zoo", None, V2_DISCOVERY_URI)
848+
849+
def test_discovery_with_empty_version_preserves_custom_uri(self):
850+
http = HttpMockSequence(
851+
[
852+
({"status": "200"}, read_datafile("zoo.json", "rb")),
853+
]
854+
)
855+
custom_discovery_uri = "https://foo.bar/$discovery"
856+
build(
857+
"zoo", version=None, http=http,
858+
cache_discovery=False, discoveryServiceUrl=custom_discovery_uri)
859+
validate_discovery_requests(
860+
self, http, "zoo", None, custom_discovery_uri)
861+
862+
def test_discovery_with_valid_version_uses_v1(self):
863+
http = HttpMockSequence(
864+
[
865+
({"status": "200"}, read_datafile("zoo.json", "rb")),
866+
]
867+
)
868+
build("zoo", version="v123", http=http, cache_discovery=False)
869+
validate_discovery_requests(self, http, "zoo", "v123", V1_DISCOVERY_URI)
870+
840871

841872
class DiscoveryRetryFromHttp(unittest.TestCase):
842873
def test_repeated_500_retries_and_fails(self):

0 commit comments

Comments
 (0)