@@ -330,7 +330,13 @@ def _discovery_service_uri_options(discoveryServiceUrl, version):
330
330
331
331
332
332
def _retrieve_discovery_doc (
333
- url , http , cache_discovery , cache = None , developerKey = None , num_retries = 1
333
+ url ,
334
+ http ,
335
+ cache_discovery ,
336
+ cache = None ,
337
+ developerKey = None ,
338
+ num_retries = 1 ,
339
+ static_discovery = True
334
340
):
335
341
"""Retrieves the discovery_doc from cache or the internet.
336
342
@@ -345,6 +351,9 @@ def _retrieve_discovery_doc(
345
351
from the API Console.
346
352
num_retries: Integer, number of times to retry discovery with
347
353
randomized exponential backoff in case of intermittent/connection issues.
354
+ static_discovery: Boolean, whether or not to use the static discovery docs
355
+ included in the package when the discovery doc is not available in the
356
+ cache.
348
357
349
358
Returns:
350
359
A unicode string representation of the discovery document.
@@ -359,21 +368,29 @@ def _retrieve_discovery_doc(
359
368
if content :
360
369
return content
361
370
362
- actual_url = url
363
- # REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
364
- # variable that contains the network address of the client sending the
365
- # request. If it exists then add that to the request for the discovery
366
- # document to avoid exceeding the quota on discovery requests.
367
- if "REMOTE_ADDR" in os .environ :
368
- actual_url = _add_query_parameter (url , "userIp" , os .environ ["REMOTE_ADDR" ])
369
- if developerKey :
370
- actual_url = _add_query_parameter (url , "key" , developerKey )
371
- logger .debug ("URL being requested: GET %s" , actual_url )
372
-
373
- # Execute this request with retries build into HttpRequest
374
- # Note that it will already raise an error if we don't get a 2xx response
375
- req = HttpRequest (http , HttpRequest .null_postproc , actual_url )
376
- resp , content = req .execute (num_retries = num_retries )
371
+ # At this point, the discovery document was not found in the cache so
372
+ # we can attempt to retreive the static discovery document from the library.
373
+ if static_discovery :
374
+ content = discovery_cache .get_static_doc (url )
375
+
376
+ # If the content is None, retrieve the discovery doc from the internet
377
+ # because it is not in the cache or the static doc directory.
378
+ if content is None :
379
+ actual_url = url
380
+ # REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
381
+ # variable that contains the network address of the client sending the
382
+ # request. If it exists then add that to the request for the discovery
383
+ # document to avoid exceeding the quota on discovery requests.
384
+ if "REMOTE_ADDR" in os .environ :
385
+ actual_url = _add_query_parameter (url , "userIp" , os .environ ["REMOTE_ADDR" ])
386
+ if developerKey :
387
+ actual_url = _add_query_parameter (url , "key" , developerKey )
388
+ logger .debug ("URL being requested: GET %s" , actual_url )
389
+
390
+ # Execute this request with retries build into HttpRequest
391
+ # Note that it will already raise an error if we don't get a 2xx response
392
+ req = HttpRequest (http , HttpRequest .null_postproc , actual_url )
393
+ resp , content = req .execute (num_retries = num_retries )
377
394
378
395
try :
379
396
content = content .decode ("utf-8" )
0 commit comments