diff options
author | Nathaniel Manista <nathaniel@google.com> | 2016-03-15 18:59:12 -0700 |
---|---|---|
committer | Nathaniel Manista <nathaniel@google.com> | 2016-03-15 18:59:12 -0700 |
commit | 6d7f655d572415d8d8c812f79e83a46557921a53 (patch) | |
tree | 7351825c310a942c66f671ec6705e71bdb93632f | |
parent | a42243ce1225bb0c8ba131b1d05deca9c891e02e (diff) | |
parent | 12b7cd39c4609c641e510499eb0ec1671a909a92 (diff) | |
download | google-api-python-client-6d7f655d572415d8d8c812f79e83a46557921a53.tar.gz |
Merge pull request #202 from ethanbao/remote_master
Fallback to use new discovery uri pattern when the old one fails.
-rw-r--r-- | googleapiclient/discovery.py | 33 | ||||
-rw-r--r-- | tests/test_discovery.py | 8 |
2 files changed, 27 insertions, 14 deletions
diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py index cee56284c..ecb75fa48 100644 --- a/googleapiclient/discovery.py +++ b/googleapiclient/discovery.py @@ -82,6 +82,9 @@ URITEMPLATE = re.compile('{[^}]*}') VARNAME = re.compile('[a-zA-Z0-9_-]+') DISCOVERY_URI = ('https://www.googleapis.com/discovery/v1/apis/' '{api}/{apiVersion}/rest') +V1_DISCOVERY_URI = DISCOVERY_URI +V2_DISCOVERY_URI = ('https://{api}.googleapis.com/$discovery/rest?' + 'version={apiVersion}') DEFAULT_METHOD_DOC = 'A description of how to use this function' HTTP_PAYLOAD_METHODS = frozenset(['PUT', 'POST', 'PATCH']) _MEDIA_SIZE_BIT_SHIFTS = {'KB': 10, 'MB': 20, 'GB': 30, 'TB': 40} @@ -196,21 +199,23 @@ def build(serviceName, if http is None: http = httplib2.Http() - requested_url = uritemplate.expand(discoveryServiceUrl, params) - - try: - content = _retrieve_discovery_doc(requested_url, http, cache_discovery, - cache) - except HttpError as e: - if e.resp.status == http_client.NOT_FOUND: - raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, - version)) - else: - raise e + for discovery_url in (discoveryServiceUrl, V2_DISCOVERY_URI,): + requested_url = uritemplate.expand(discovery_url, params) + + try: + content = _retrieve_discovery_doc(requested_url, http, cache_discovery, + cache) + return build_from_document(content, base=discovery_url, http=http, + developerKey=developerKey, model=model, requestBuilder=requestBuilder, + credentials=credentials) + except HttpError as e: + if e.resp.status == http_client.NOT_FOUND: + continue + else: + raise e - return build_from_document(content, base=discoveryServiceUrl, http=http, - developerKey=developerKey, model=model, requestBuilder=requestBuilder, - credentials=credentials) + raise UnknownApiNameOrVersion( + "name: %s version: %s" % (serviceName, version)) def _retrieve_discovery_doc(url, http, cache_discovery, cache=None): diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 0181bbb7e..dea563179 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -352,6 +352,7 @@ class DiscoveryErrors(unittest.TestCase): def test_unknown_api_name_or_version(self): http = HttpMockSequence([ ({'status': '404'}, open(datafile('zoo.json'), 'rb').read()), + ({'status': '404'}, open(datafile('zoo.json'), 'rb').read()), ]) with self.assertRaises(UnknownApiNameOrVersion): plus = build('plus', 'v1', http=http, cache_discovery=False) @@ -425,6 +426,13 @@ class DiscoveryFromHttp(unittest.TestCase): except HttpError as e: self.assertEqual(e.uri, 'http://example.com') + def test_discovery_loading_from_v2_discovery_uri(self): + http = HttpMockSequence([ + ({'status': '404'}, 'Not found'), + ({'status': '200'}, open(datafile('zoo.json'), 'rb').read()), + ]) + zoo = build('zoo', 'v1', http=http, cache_discovery=False) + self.assertTrue(hasattr(zoo, 'animals')) class DiscoveryFromAppEngineCache(unittest.TestCase): def test_appengine_memcache(self): |