@@ -193,6 +193,7 @@ def build(
193
193
adc_cert_path = None ,
194
194
adc_key_path = None ,
195
195
num_retries = 1 ,
196
+ static_discovery = True ,
196
197
):
197
198
"""Construct a Resource for interacting with an API.
198
199
@@ -246,6 +247,9 @@ def build(
246
247
https://google.aip.dev/auth/4114
247
248
num_retries: Integer, number of times to retry discovery with
248
249
randomized exponential backoff in case of intermittent/connection issues.
250
+ static_discovery: Boolean, whether or not to use the static discovery docs
251
+ included in the package when the discovery doc is not available in the
252
+ cache.
249
253
250
254
Returns:
251
255
A Resource object with methods for interacting with the service.
@@ -274,6 +278,7 @@ def build(
274
278
cache ,
275
279
developerKey ,
276
280
num_retries = num_retries ,
281
+ static_discovery = static_discovery ,
277
282
)
278
283
service = build_from_document (
279
284
content ,
@@ -330,7 +335,13 @@ def _discovery_service_uri_options(discoveryServiceUrl, version):
330
335
331
336
332
337
def _retrieve_discovery_doc (
333
- url , http , cache_discovery , cache = None , developerKey = None , num_retries = 1
338
+ url ,
339
+ http ,
340
+ cache_discovery ,
341
+ cache = None ,
342
+ developerKey = None ,
343
+ num_retries = 1 ,
344
+ static_discovery = True
334
345
):
335
346
"""Retrieves the discovery_doc from cache or the internet.
336
347
@@ -345,35 +356,48 @@ def _retrieve_discovery_doc(
345
356
from the API Console.
346
357
num_retries: Integer, number of times to retry discovery with
347
358
randomized exponential backoff in case of intermittent/connection issues.
359
+ static_discovery: Boolean, whether or not to use the static discovery docs
360
+ included in the package when the discovery doc is not available in the
361
+ cache.
348
362
349
363
Returns:
350
364
A unicode string representation of the discovery document.
351
365
"""
352
- if cache_discovery :
353
- from . import discovery_cache
366
+ from . import discovery_cache
367
+
368
+ content = None
354
369
370
+ if cache_discovery :
355
371
if cache is None :
356
372
cache = discovery_cache .autodetect ()
357
373
if cache :
358
374
content = cache .get (url )
359
375
if content :
360
376
return content
361
377
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 )
378
+ # At this point, the discovery document was not found in the cache so
379
+ # we can attempt to retreive the static discovery document from the library.
380
+ if static_discovery :
381
+ content = discovery_cache .get_static_doc (url )
382
+
383
+ # If the content is None, retrieve the discovery doc from the internet
384
+ # because it is not in the cache or the static doc directory.
385
+ if content is None :
386
+ actual_url = url
387
+ # REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
388
+ # variable that contains the network address of the client sending the
389
+ # request. If it exists then add that to the request for the discovery
390
+ # document to avoid exceeding the quota on discovery requests.
391
+ if "REMOTE_ADDR" in os .environ :
392
+ actual_url = _add_query_parameter (url , "userIp" , os .environ ["REMOTE_ADDR" ])
393
+ if developerKey :
394
+ actual_url = _add_query_parameter (url , "key" , developerKey )
395
+ logger .debug ("URL being requested: GET %s" , actual_url )
396
+
397
+ # Execute this request with retries build into HttpRequest
398
+ # Note that it will already raise an error if we don't get a 2xx response
399
+ req = HttpRequest (http , HttpRequest .null_postproc , actual_url )
400
+ resp , content = req .execute (num_retries = num_retries )
377
401
378
402
try :
379
403
content = content .decode ("utf-8" )
0 commit comments