diff options
-rw-r--r-- | googleapiclient/discovery.py | 14 | ||||
-rw-r--r-- | tests/test_discovery.py | 8 |
2 files changed, 18 insertions, 4 deletions
diff --git a/googleapiclient/discovery.py b/googleapiclient/discovery.py index b7c884ecc..be62cf73e 100644 --- a/googleapiclient/discovery.py +++ b/googleapiclient/discovery.py @@ -29,6 +29,7 @@ __all__ = [ ] from six import StringIO +from six.moves import http_client from six.moves.urllib.parse import urlencode, urlparse, urljoin, \ urlunparse, parse_qsl @@ -190,7 +191,15 @@ def build(serviceName, requested_url = uritemplate.expand(discoveryServiceUrl, params) - content = _retrieve_discovery_doc(requested_url, http, cache_discovery, cache) + 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 return build_from_document(content, base=discoveryServiceUrl, http=http, developerKey=developerKey, model=model, requestBuilder=requestBuilder, @@ -232,9 +241,6 @@ def _retrieve_discovery_doc(url, http, cache_discovery, cache=None): resp, content = http.request(actual_url) - if resp.status == 404: - raise UnknownApiNameOrVersion("name: %s version: %s" % (serviceName, - version)) if resp.status >= 400: raise HttpError(resp, content, uri=actual_url) diff --git a/tests/test_discovery.py b/tests/test_discovery.py index 2e13e59e6..45962f9f5 100644 --- a/tests/test_discovery.py +++ b/tests/test_discovery.py @@ -59,6 +59,7 @@ from googleapiclient.errors import InvalidJsonError from googleapiclient.errors import MediaUploadSizeError from googleapiclient.errors import ResumableUploadError from googleapiclient.errors import UnacceptableMimeTypeError +from googleapiclient.errors import UnknownApiNameOrVersion from googleapiclient.http import BatchHttpRequest from googleapiclient.http import HttpMock from googleapiclient.http import HttpMockSequence @@ -347,6 +348,13 @@ class DiscoveryErrors(unittest.TestCase): except InvalidJsonError: pass + def test_unknown_api_name_or_version(self): + http = HttpMockSequence([ + ({'status': '404'}, open(datafile('zoo.json'), 'rb').read()), + ]) + with self.assertRaises(UnknownApiNameOrVersion): + plus = build('plus', 'v1', http=http, cache_discovery=False) + class DiscoveryFromDocument(unittest.TestCase): |