@@ -187,6 +187,7 @@ def build(
187187 client_options = None ,
188188 adc_cert_path = None ,
189189 adc_key_path = None ,
190+ num_retries = 1 ,
190191):
191192 """Construct a Resource for interacting with an API.
192193
@@ -223,6 +224,8 @@ def build(
223224 adc_key_path: str, client encrypted private key file path to save the
224225 application default client encrypted private key for mTLS. This field is
225226 required if you want to use the default client certificate.
227+ num_retries: Integer, number of times to retry discovery with
228+ randomized exponential backoff in case of intermittent/connection issues.
226229
227230 Returns:
228231 A Resource object with methods for interacting with the service.
@@ -243,7 +246,8 @@ def build(
243246
244247 try :
245248 content = _retrieve_discovery_doc (
246- requested_url , discovery_http , cache_discovery , cache , developerKey
249+ requested_url , discovery_http , cache_discovery , cache ,
250+ developerKey , num_retries = num_retries
247251 )
248252 return build_from_document (
249253 content ,
@@ -266,7 +270,8 @@ def build(
266270 raise UnknownApiNameOrVersion ("name: %s version: %s" % (serviceName , version ))
267271
268272
269- def _retrieve_discovery_doc (url , http , cache_discovery , cache = None , developerKey = None ):
273+ def _retrieve_discovery_doc (url , http , cache_discovery ,
274+ cache = None , developerKey = None , num_retries = 1 ):
270275 """Retrieves the discovery_doc from cache or the internet.
271276
272277 Args:
@@ -276,13 +281,16 @@ def _retrieve_discovery_doc(url, http, cache_discovery, cache=None, developerKey
276281 cache_discovery: Boolean, whether or not to cache the discovery doc.
277282 cache: googleapiclient.discovery_cache.base.Cache, an optional cache
278283 object for the discovery documents.
284+ developerKey: string, Key for controlling API usage, generated
285+ from the API Console.
286+ num_retries: Integer, number of times to retry discovery with
287+ randomized exponential backoff in case of intermittent/connection issues.
279288
280289 Returns:
281290 A unicode string representation of the discovery document.
282291 """
283292 if cache_discovery :
284293 from . import discovery_cache
285- from .discovery_cache import base
286294
287295 if cache is None :
288296 cache = discovery_cache .autodetect ()
@@ -302,10 +310,10 @@ def _retrieve_discovery_doc(url, http, cache_discovery, cache=None, developerKey
302310 actual_url = _add_query_parameter (url , "key" , developerKey )
303311 logger .debug ("URL being requested: GET %s" , actual_url )
304312
305- resp , content = http . request ( actual_url )
306-
307- if resp . status >= 400 :
308- raise HttpError ( resp , content , uri = actual_url )
313+ # Execute this request with retries build into HttpRequest
314+ # Note that it will already raise an error if we don't get a 2xx response
315+ req = HttpRequest ( http , HttpRequest . null_postproc , actual_url )
316+ resp , content = req . execute ( num_retries = num_retries )
309317
310318 try :
311319 content = content .decode ("utf-8" )
0 commit comments