o
    Mf                  	   @   s   d 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	 ej
ejejejejeejejejf	ZdZdd ZejedZ	 G d	d
 d
eZdd Zdd Zdd Zdd ZeeedgZ	 eeedgZ	 eeedgZdS )zHelpers for configuring retries with exponential back-off.

See [Retry Strategy for Google Cloud Storage](https://cloud.google.com/storage/docs/retry-strategy#client-libraries)
    N)
exceptions)retry)i  c                 C   sB   t | trdS t | tjr| jtv S t | tjrt| j	d S dS )z(Predicate for determining when to retry.Tr   F)

isinstance_RETRYABLE_TYPESapi_exceptionsGoogleAPICallErrorcode"_ADDITIONAL_RETRYABLE_STATUS_CODESauth_exceptionsTransportError_should_retryargs)exc r   \/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/google/cloud/storage/retry.pyr   -   s   

r   )	predicatec                   @   s    e Zd ZdZdd Zdd ZdS )ConditionalRetryPolicyaN  A class for use when an API call is only conditionally safe to retry.

    This class is intended for use in inspecting the API call parameters of an
    API call to verify that any flags necessary to make the API call idempotent
    (such as specifying an ``if_generation_match`` or related flag) are present.

    It can be used in place of a ``retry.Retry`` object, in which case
    ``_http.Connection.api_request`` will pass the requested api call keyword
    arguments into the ``conditional_predicate`` and return the ``retry_policy``
    if the conditions are met.

    :type retry_policy: class:`google.api_core.retry.Retry`
    :param retry_policy: A retry object defining timeouts, persistence and which
        exceptions to retry.

    :type conditional_predicate: callable
    :param conditional_predicate: A callable that accepts exactly the number of
        arguments in ``required_kwargs``, in order, and returns True if the
        arguments have sufficient data to determine that the call is safe to
        retry (idempotent).

    :type required_kwargs: list(str)
    :param required_kwargs:
        A list of keyword argument keys that will be extracted from the API call
        and passed into the ``conditional predicate`` in order. For example,
        ``["query_params"]`` is commmonly used for preconditions in query_params.
    c                 C   s   || _ || _|| _d S )N)retry_policyconditional_predicaterequired_kwargs)selfr   r   r   r   r   r   __init__d   s   
zConditionalRetryPolicy.__init__c                    s$   | j  fdd| jD  r| jS d S )Nc                    s   g | ]} | qS r   r   ).0keykwargsr   r   
<listcomp>j   s    zMConditionalRetryPolicy.get_retry_policy_if_conditions_met.<locals>.<listcomp>)r   r   r   )r   r   r   r   r   "get_retry_policy_if_conditions_meti   s   z9ConditionalRetryPolicy.get_retry_policy_if_conditions_metN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   G   s    r   c                 C   s$   |  ddu}|  ddu}|p|S )z>Return True if generation or if_generation_match is specified.
generationNifGenerationMatchget)query_paramsr"   if_generation_matchr   r   r   is_generation_specifiedo   s   r(   c                 C   s   |  ddu}|S )z4Return True if if_metageneration_match is specified.ifMetagenerationMatchNr$   )r&   if_metageneration_matchr   r   r   is_metageneration_specifiedv   s   r+   c                 C   s   | duod| v S )zReturn True if an etag is contained in the request body.

    :type data: dict or None
    :param data: A dict representing the request JSON body. If not passed, returns False.
    Netagr   datar   r   r   is_etag_in_data|   s   r/   c                 C   s   t | S )z
    ``is_etag_in_json`` is supported for backwards-compatibility reasons only;
    please use ``is_etag_in_data`` instead.
    )r/   r-   r   r   r   is_etag_in_json   s   r0   r&   r.   ) r!   requestsrequests.exceptionsr   requests_exceptionsgoogle.api_corer   r   google.authr
   TooManyRequestsInternalServerError
BadGatewayServiceUnavailableGatewayTimeoutConnectionErrorChunkedEncodingErrorTimeoutr   r	   r   RetryDEFAULT_RETRYobjectr   r(   r+   r/   r0   %DEFAULT_RETRY_IF_GENERATION_SPECIFIED)DEFAULT_RETRY_IF_METAGENERATION_SPECIFIEDDEFAULT_RETRY_IF_ETAG_IN_JSONr   r   r   r   <module>   sJ   (	