Skip to content
Browse files
fix: discovery uses V2 when version is None (#975)
Passing version=None to, 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
  • Loading branch information
DFrenkel committed Jul 30, 2020
1 parent d059ad8 commit cd4e8f429422232dd82ef7e9bc685061d5df94a1
Showing with 57 additions and 2 deletions.
  1. +26 鈭2 googleapiclient/
  2. +31 鈭0 tests/
@@ -29,7 +29,7 @@

# Standard library imports
import copy

from collections import OrderedDict
from email.generator import BytesGenerator
except ImportError:
@@ -241,7 +241,8 @@ def build(
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)

@@ -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.
string, the Original Discovery Service URL preferred by the customer.
string, API Version requested
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:
"Discovery V1 does not support empty versions. Defaulting to V2...")
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.
@@ -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 = "$discovery"
"zoo", version=None, http=http,
cache_discovery=False, discoveryServiceUrl=custom_discovery_uri)
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):

0 comments on commit cd4e8f4

Please sign in to comment.