Skip to content

Commit 6d7f655

Browse files
Merge pull request #202 from ethanbao/remote_master
Fallback to use new discovery uri pattern when the old one fails.
2 parents a42243c + 12b7cd3 commit 6d7f655

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

googleapiclient/discovery.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
VARNAME = re.compile('[a-zA-Z0-9_-]+')
8383
DISCOVERY_URI = ('https://www.googleapis.com/discovery/v1/apis/'
8484
'{api}/{apiVersion}/rest')
85+
V1_DISCOVERY_URI = DISCOVERY_URI
86+
V2_DISCOVERY_URI = ('https://{api}.googleapis.com/$discovery/rest?'
87+
'version={apiVersion}')
8588
DEFAULT_METHOD_DOC = 'A description of how to use this function'
8689
HTTP_PAYLOAD_METHODS = frozenset(['PUT', 'POST', 'PATCH'])
8790
_MEDIA_SIZE_BIT_SHIFTS = {'KB': 10, 'MB': 20, 'GB': 30, 'TB': 40}
@@ -196,21 +199,23 @@ def build(serviceName,
196199
if http is None:
197200
http = httplib2.Http()
198201

199-
requested_url = uritemplate.expand(discoveryServiceUrl, params)
200-
201-
try:
202-
content = _retrieve_discovery_doc(requested_url, http, cache_discovery,
203-
cache)
204-
except HttpError as e:
205-
if e.resp.status == http_client.NOT_FOUND:
206-
raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName,
207-
version))
208-
else:
209-
raise e
202+
for discovery_url in (discoveryServiceUrl, V2_DISCOVERY_URI,):
203+
requested_url = uritemplate.expand(discovery_url, params)
204+
205+
try:
206+
content = _retrieve_discovery_doc(requested_url, http, cache_discovery,
207+
cache)
208+
return build_from_document(content, base=discovery_url, http=http,
209+
developerKey=developerKey, model=model, requestBuilder=requestBuilder,
210+
credentials=credentials)
211+
except HttpError as e:
212+
if e.resp.status == http_client.NOT_FOUND:
213+
continue
214+
else:
215+
raise e
210216

211-
return build_from_document(content, base=discoveryServiceUrl, http=http,
212-
developerKey=developerKey, model=model, requestBuilder=requestBuilder,
213-
credentials=credentials)
217+
raise UnknownApiNameOrVersion(
218+
"name: %s version: %s" % (serviceName, version))
214219

215220

216221
def _retrieve_discovery_doc(url, http, cache_discovery, cache=None):

tests/test_discovery.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def test_failed_to_parse_discovery_json(self):
352352
def test_unknown_api_name_or_version(self):
353353
http = HttpMockSequence([
354354
({'status': '404'}, open(datafile('zoo.json'), 'rb').read()),
355+
({'status': '404'}, open(datafile('zoo.json'), 'rb').read()),
355356
])
356357
with self.assertRaises(UnknownApiNameOrVersion):
357358
plus = build('plus', 'v1', http=http, cache_discovery=False)
@@ -425,6 +426,13 @@ def test_userip_missing_is_not_added_to_discovery_uri(self):
425426
except HttpError as e:
426427
self.assertEqual(e.uri, 'http://example.com')
427428

429+
def test_discovery_loading_from_v2_discovery_uri(self):
430+
http = HttpMockSequence([
431+
({'status': '404'}, 'Not found'),
432+
({'status': '200'}, open(datafile('zoo.json'), 'rb').read()),
433+
])
434+
zoo = build('zoo', 'v1', http=http, cache_discovery=False)
435+
self.assertTrue(hasattr(zoo, 'animals'))
428436

429437
class DiscoveryFromAppEngineCache(unittest.TestCase):
430438
def test_appengine_memcache(self):

0 commit comments

Comments
 (0)