3535from gcloud .streaming .util import calculate_wait_for_retry
3636
3737
38+ _REDIRECTIONS = 5
3839# 308 and 429 don't have names in httplib.
3940RESUME_INCOMPLETE = 308
4041TOO_MANY_REQUESTS = 429
6263)
6364
6465
65- class _ExceptionRetryArgs (
66- collections .namedtuple (
67- '_ExceptionRetryArgs' ,
68- ['http' , 'http_request' , 'exc' , 'num_retries' , 'max_retry_wait' ])):
69- """Bundle of information for retriable exceptions.
70-
71- :type http: :class:`httplib2.Http` (or conforming alternative)
72- :param http: instance used to perform requests.
73-
74- :type http_request: :class:`Request`
75- :param http_request: the request whose response was a retriable error.
76-
77- :type exc: :class:`Exception` subclass
78- :param exc: the exception being raised.
79-
80- :type num_retries: integer
81- :param num_retries: Number of retries consumed; used for exponential
82- backoff.
83- """
84-
85-
8666@contextlib .contextmanager
8767def _httplib2_debug_level (http_request , level , http = None ):
8868 """Temporarily change the value of httplib2.debuglevel, if necessary.
@@ -328,8 +308,7 @@ def _reset_http_connections(http):
328308 del http .connections [conn_key ]
329309
330310
331- def _make_api_request_no_retry (http , http_request , redirections = 5 ,
332- check_response_func = _check_response ):
311+ def _make_api_request_no_retry (http , http_request , redirections = _REDIRECTIONS ):
333312 """Send an HTTP request via the given http instance.
334313
335314 This wrapper exists to handle translation between the plain httplib2
@@ -344,9 +323,6 @@ def _make_api_request_no_retry(http, http_request, redirections=5,
344323 :type redirections: integer
345324 :param redirections: Number of redirects to follow.
346325
347- :type check_response_func: function taking (response, content, url).
348- :param check_response_func: Function to validate the HTTP response.
349-
350326 :rtype: :class:`Response`
351327 :returns: an object representing the server's response
352328
@@ -374,16 +350,12 @@ def _make_api_request_no_retry(http, http_request, redirections=5,
374350 raise RequestError ()
375351
376352 response = Response (info , content , http_request .url )
377- check_response_func (response )
353+ _check_response (response )
378354 return response
379355
380356
381- def make_api_request (http , http_request ,
382- retries = 7 ,
383- max_retry_wait = 60 ,
384- redirections = 5 ,
385- check_response_func = _check_response ,
386- wo_retry_func = _make_api_request_no_retry ):
357+ def make_api_request (http , http_request , retries = 7 ,
358+ redirections = _REDIRECTIONS ):
387359 """Send an HTTP request via the given http, performing error/retry handling.
388360
389361 :type http: :class:`httplib2.Http`
@@ -396,19 +368,9 @@ def make_api_request(http, http_request,
396368 :param retries: Number of retries to attempt on retryable
397369 responses (such as 429 or 5XX).
398370
399- :type max_retry_wait: integer
400- :param max_retry_wait: Maximum number of seconds to wait when retrying.
401-
402371 :type redirections: integer
403372 :param redirections: Number of redirects to follow.
404373
405- :type check_response_func: function taking (response, content, url).
406- :param check_response_func: Function to validate the HTTP response.
407-
408- :type wo_retry_func: function taking
409- (http, request, redirections, check_response_func)
410- :param wo_retry_func: Function to make HTTP request without retries.
411-
412374 :rtype: :class:`Response`
413375 :returns: an object representing the server's response.
414376
@@ -418,48 +380,17 @@ def make_api_request(http, http_request,
418380 retry = 0
419381 while True :
420382 try :
421- return wo_retry_func (
422- http , http_request , redirections = redirections ,
423- check_response_func = check_response_func )
383+ return _make_api_request_no_retry (http , http_request ,
384+ redirections = redirections )
424385 except _RETRYABLE_EXCEPTIONS as exc :
425386 retry += 1
426387 if retry >= retries :
427388 raise
428389 retry_after = getattr (exc , 'retry_after' , None )
429390 if retry_after is None :
430- retry_after = calculate_wait_for_retry (retry , max_retry_wait )
391+ retry_after = calculate_wait_for_retry (retry )
431392
432393 _reset_http_connections (http )
433394 logging .debug ('Retrying request to url %s after exception %s' ,
434395 http_request .url , type (exc ).__name__ )
435396 time .sleep (retry_after )
436-
437-
438- _HTTP_FACTORIES = []
439-
440-
441- def _register_http_factory (factory ):
442- """Register a custom HTTP factory.
443-
444- :type factory: callable taking keyword arguments, returning an Http
445- instance (or an instance implementing the same API).
446- :param factory: the new factory (it may return ``None`` to defer to
447- a later factory or the default).
448- """
449- _HTTP_FACTORIES .append (factory )
450-
451-
452- def get_http (** kwds ):
453- """Construct an Http instance.
454-
455- :type kwds: dict
456- :param kwds: keyword arguments to pass to factories.
457-
458- :rtype: :class:`httplib2.Http` (or a workalike)
459- :returns: The HTTP object created.
460- """
461- for factory in _HTTP_FACTORIES :
462- http = factory (** kwds )
463- if http is not None :
464- return http
465- return httplib2 .Http (** kwds )
0 commit comments