
                         r    d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
 ddlmZ dd	lmZ d
 ZddZy)z,Utilities for retrying requests on failures.    )absolute_import)division)unicode_literalsN)http_wrapper)errors)
properties)retryc                     d }|| _         y)a  Sets the retry function for the apitools transfer object.

  Replaces the Apitools' default retry function
  HandleExceptionsAndRebuildHttpConnections with a custom one which calls
  HandleExceptionsAndRebuildHttpConnections and then raise a custom exception.
  This is useful when we don't want MakeRequest method in Apitools to retry
  the http request directly and instead let the caller decide the next action.

  Args:
    apitools_transfer_object (apitools.base.py.transfer.Transfer): The
    Apitools' transfer object.
  c                     t        j                  |        t        | j                  t              r3| j                  j
                  t
        j                  k(  r| j                  t        j                         )N)	apitools_http_wrapper)HandleExceptionsAndRebuildHttpConnections
isinstanceexcOSErrorerrnoENOSPCr   RetryableApiError)
retry_argss    0lib/googlecloudsdk/api_lib/storage/retry_util.py_handle_error_and_raisez/set_retry_func.<locals>._handle_error_and_raise*   sS     CCJO
 	:>>7+NN  ELL0NN 
"
"
$$    N)
retry_func)apitools_transfer_objectr   s     r   set_retry_funcr      s    %  )@%r   c                    t        j                  t        j                  j                  j
                  j                         t        j                  j                  j                  j                         dz  t        j                  j                  j                  j                               j                  | ||t        j                  j                  j                  j                         dz  |      S )af  Retries the target with specific default value.

  This function is intended to be used for all gcloud storage's API requests
  that require custom retry handling (e.g downloads and uploads).

  Args:
    target (Callable): The function to call and retry.
    should_retry_if (Callable): func(exc_type, exc_value, exc_traceback, state)
        that returns True or False.
    target_args (Sequence|None): A sequence of positional arguments to be passed
        to the target.
    target_kwargs (Dict|None): A dict of keyword arguments to be passed
        to target.

  Returns:
    Whatever the target returns.
  i  )max_retrialswait_ceiling_msexponential_sleep_multiplier)argskwargssleep_msshould_retry_if)r	   Retryerr   VALUESstoragemax_retriesGetIntmax_retry_delayr   RetryOnExceptionbase_retry_delay)targetr"   target_argstarget_kwargss       r   retryerr.   =   s    & 
$$,,88??A ''//??FFH4O



#
#
@
@
G
G
I	
	
 
	
$$,,==DDFM) 
 
+
+r   )NN)__doc__
__future__r   r   r   r   apitools.base.pyr   r   googlecloudsdk.api_lib.storager   googlecloudsdk.corer   googlecloudsdk.core.utilr	   r   r.    r   r   <module>r6      s.    3 &  '  B 1 * *@@+r   