Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d779cc

Browse files
arunpn123busunkim96
authored andcommitted
Fix the client to respect the passed in developerKey and credentials (googleapis#593)
* Fix googleapiclient to respect the passed in developerKey and credentials * Fix tests to consider developerKey and credentials
1 parent 1b010a3 commit 9d779cc

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

googleapiclient/discovery.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def build(serviceName,
219219

220220
try:
221221
content = _retrieve_discovery_doc(
222-
requested_url, discovery_http, cache_discovery, cache)
222+
requested_url, discovery_http, cache_discovery, cache, developerKey)
223223
return build_from_document(content, base=discovery_url, http=http,
224224
developerKey=developerKey, model=model, requestBuilder=requestBuilder,
225225
credentials=credentials)
@@ -233,7 +233,8 @@ def build(serviceName,
233233
"name: %s version: %s" % (serviceName, version))
234234

235235

236-
def _retrieve_discovery_doc(url, http, cache_discovery, cache=None):
236+
def _retrieve_discovery_doc(url, http, cache_discovery, cache=None,
237+
developerKey=None):
237238
"""Retrieves the discovery_doc from cache or the internet.
238239
239240
Args:
@@ -264,6 +265,8 @@ def _retrieve_discovery_doc(url, http, cache_discovery, cache=None):
264265
# document to avoid exceeding the quota on discovery requests.
265266
if 'REMOTE_ADDR' in os.environ:
266267
actual_url = _add_query_parameter(url, 'userIp', os.environ['REMOTE_ADDR'])
268+
if developerKey:
269+
actual_url = _add_query_parameter(url, 'key', developerKey)
267270
logger.info('URL being requested: GET %s', actual_url)
268271

269272
resp, content = http.request(actual_url)
@@ -360,7 +363,9 @@ def build_from_document(
360363
# The credentials need to be scoped.
361364
credentials = _auth.with_scopes(credentials, scopes)
362365

363-
# Create an authorized http instance
366+
# If credentials are provided, create an authorized http instance;
367+
# otherwise, skip authentication.
368+
if credentials:
364369
http = _auth.authorized_http(credentials)
365370

366371
# If the service doesn't require scopes then there is no need for

tests/test_discovery.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def test_building_with_optional_http_with_no_authorization(self):
449449

450450
plus = build_from_document(
451451
discovery, base="https://www.googleapis.com/",
452-
credentials=self.MOCK_CREDENTIALS)
452+
credentials=None)
453453
# plus service requires Authorization
454454
self.assertIsInstance(plus._http, httplib2.Http)
455455
self.assertIsInstance(plus._http.timeout, int)
@@ -487,7 +487,7 @@ def test_userip_is_added_to_discovery_uri(self):
487487
http = HttpMockSequence([
488488
({'status': '400'}, open(datafile('zoo.json'), 'rb').read()),
489489
])
490-
zoo = build('zoo', 'v1', http=http, developerKey='foo',
490+
zoo = build('zoo', 'v1', http=http, developerKey=None,
491491
discoveryServiceUrl='http://example.com')
492492
self.fail('Should have raised an exception.')
493493
except HttpError as e:
@@ -506,6 +506,19 @@ def test_userip_missing_is_not_added_to_discovery_uri(self):
506506
except HttpError as e:
507507
self.assertEqual(e.uri, 'http://example.com')
508508

509+
def test_key_is_added_to_discovery_uri(self):
510+
# build() will raise an HttpError on a 400, use this to pick the request uri
511+
# out of the raised exception.
512+
try:
513+
http = HttpMockSequence([
514+
({'status': '400'}, open(datafile('zoo.json'), 'rb').read()),
515+
])
516+
zoo = build('zoo', 'v1', http=http, developerKey='foo',
517+
discoveryServiceUrl='http://example.com')
518+
self.fail('Should have raised an exception.')
519+
except HttpError as e:
520+
self.assertEqual(e.uri, 'http://example.com?key=foo')
521+
509522
def test_discovery_loading_from_v2_discovery_uri(self):
510523
http = HttpMockSequence([
511524
({'status': '404'}, 'Not found'),

0 commit comments

Comments
 (0)