Skip to content

Commit

Permalink
Merge pull request googleapis#131 from tmatsuo/discovery-cache
Browse files Browse the repository at this point in the history
Fixed a NameError on exception; added a unit test.
  • Loading branch information
nathanielmanistaatgoogle committed Sep 4, 2015
2 parents a1fef46 + 3772f9d commit 075a05a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 10 additions & 4 deletions googleapiclient/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
]

from six import StringIO
from six.moves import http_client
from six.moves.urllib.parse import urlencode, urlparse, urljoin, \
urlunparse, parse_qsl

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
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
Expand Down Expand Up @@ -347,6 +348,13 @@ def test_failed_to_parse_discovery_json(self):
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):

Expand Down

0 comments on commit 075a05a

Please sign in to comment.