o
    QDi!                     @   s   d dl Z d dlmZmZ d dlmZmZmZmZmZ d dl	m
Z
mZ d dlmZmZmZ d dlmZ ddlmZ dd	lmZmZmZmZmZ G d
d deZG dd deZeZG dd deZdS )    N)datetimetimezone)OptionalSequenceTupleTypeUnion)PositiveFloatPositiveInt)RetryCallStatewait_exponentialwait_random_exponential)	wait_base   )HTTP_DATE_FORMAT)"get_default_http_status_exceptionsget_default_network_errorsget_default_timeoutsis_rate_limitedis_server_errorc                	   @   sd   e Zd ZdZde fdedeeedf de	e
 ddfdd	Zd
edefddZd
edefddZdS )wait_from_headera  Wait strategy that derives the wait value from an HTTP header.

    Value may be either an integer representing the number of seconds to wait
    before retrying, or a future datetime in HTTP-date format, indicating when it is
    acceptable to retry the request.

    More info on HTTP-date format: https://httpwg.org/specs/rfc9110.html#http.date

    Args:
        header (str): Header to attempt to derive wait value from.
        wait_max (float): Maximum time to wait, in seconds. Defaults to 120.0s. If
            `None` is given, will wait indefinitely. Use `Non` with caution, as your
            program will hang if the server responds with an excessive wait value.
        fallback (wait_base): Wait strategy to use if `header` is not present,
            or unable to parse to a `float` value, or if value parsed from header
            exceeds `wait_max`. Defaults to `tenacity.wait_exponential`.

    Raises:
        ValueError: If `fallback` is `None`, and any one of the following is true:
            * header is not present;
            * the value cannot be parsed to a `float`;
            * the value exceeds `wait_max`;
            * the value is a date in the past.

          ^@headerwait_maxNfallbackreturnc                 C   s"   || _ |r	t|nd | _|| _d S N)r   floatr   r   )selfr   r   r    r   H/var/www/Datamplify/venv/lib/python3.10/site-packages/retryhttp/_wait.py__init__.   s   
zwait_from_header.__init__retry_statec                 C   s   |j r`|j  }|du rdS t|t r`t|dr`|jj| j}|du r-t	d| j t
d|r7t|S t|t}|jtjd}ttj}||k rYt	d| j d| t|| jS t	d	| j d
)a=  Attempts parse a wait value from header.

        Args:
            retry_state (RetryCallState): The retry call state of the request.

        Returns:
            float: Seconds to wait, as derived from `self.header`.

        Raises:
            ValueError: If unable to parse a float from `self.header`.

        Nr   responsezHeader not present: z^\d+$)tzinfozDate provided in header "z" is in the past: z(Unable to parse wait time from header: "")outcome	exception
isinstancer   hasattrr#   headersgetr   
ValueErrorrematchr   r   strptimer   replacer   utcnowseconds)r   r"   excvalueretry_afterr2   r   r   r    _get_wait_value8   s.   

z wait_from_header._get_wait_valuec                 C   s   | j r+z| j|d}| jr|| jkr| j |dW S |W S  ty*   | j |d Y S w | j|d}| jrI|| jkrItd| j d| d| j d|S )N)r"   zWait value parsed from header "z" (z) is greater than `wait_max` ())r   r7   r   r,   r   )r   r"   r5   r   r   r    __call__]   s"   zwait_from_header.__call__)__name__
__module____qualname____doc__r   strr   r	   r
   r   r   r!   r   r   r7   r9   r   r   r   r    r      s    

%r   c                       sD   e Zd ZdZde fdeeedf dee	 ddf fddZ
  ZS )	wait_retry_aftera  Wait strategy to use when the server responds with a `Retry-After` header.

    The header value may provide a date for when you may retry the request, or an
    integer, indicating the number of seconds to wait before retrying.

    Args:
        wait_max (float): Maximum time to wait, in seconds. Defaults to 120.0s. If
            `None` is given, will wait indefinitely. Use `Non` with caution, as your
            program will hang if the server responds with an excessive wait value.
        fallback (wait_base): Wait strategy to use if `header` is not present,
            or unable to parse to a `float` value, or if value parsed from header
            exceeds `wait_max`. Defaults to `tenacity.wait_exponential()`.

    Raises:
        ValueError: If `fallback` is `None`, and any one of the following is true:
            * `Retry-After` header is not present;
            * the value cannot be parsed to a `float`;
            * the value exceeds `wait_max`;
            * the value is a date in the past.

    r   r   Nr   r   c                    s   t  jd||d d S )NzRetry-After)r   r   r   )superr!   )r   r   r   	__class__r   r    r!      s   zwait_retry_after.__init__)r:   r;   r<   r=   r   r   r	   r
   r   r   r!   __classcell__r   r   rA   r    r?   p   s    r?   c                   @   s   e Zd ZdZee de e ee ddddfdedededed	ee	e
 e
f d
eee eee df df deee eee df df ddfddZdedefddZdS )wait_context_awarea  Uses a different wait strategy based on the type of HTTP error.

    Args:
        wait_server_errors: Wait strategy to use with server errors.
        wait_network_errors: Wait strategy to use with network errors.
        wait_timeouts: Wait strategy to use with timeouts.
        wait_rate_limited: Wait strategy to use when rate limited.
        server_error_codes: One or more 5xx HTTP status codes that will trigger
            `wait_server_errors`.
        network_errors: One or more exceptions that will trigger `wait_network_errors`.
            If omitted, defaults to:

            - `httpx.ConnectError`
            - `httpx.ReadError`
            - `httpx.WriteError`
            - `requests.ConnectionError`
            - `requests.exceptions.ChunkedEncodingError`
        timeouts: One or more exceptions that will trigger `wait_timeouts`. If omitted,
            defaults to:

            - `httpx.ConnectTimeout`
            - `httpx.ReadTimeout`
            - `httpx.WriteTimeout`
            - `requests.Timeout`

    )r   )i  i  i  i  Nwait_server_errorswait_network_errorswait_timeoutswait_rate_limitedserver_error_codesnetwork_errors.timeoutsr   c                 C   sJ   |d u rt  }|d u rt }|| _|| _|| _|| _|| _|| _|| _d S r   )	r   r   rE   rF   rG   rH   rI   rJ   rK   )r   rE   rF   rG   rH   rI   rJ   rK   r   r   r    r!      s   
zwait_context_aware.__init__r"   c                 C   sj   |j r3|j  }t|| jdr| |S t|| jr| |S t|| jr*| 	|S t
|r3| |S dS )N)r4   status_codesr   )r&   r'   r   rI   rE   r(   rJ   rF   rK   rG   r   rH   )r   r"   r4   r   r   r    r9      s   




zwait_context_aware.__call__)r:   r;   r<   r=   r?   r   r   r   r   r   intr   BaseExceptionr   r!   r   r   r9   r   r   r   r    rD      sF    

rD   )r-   r   r   typingr   r   r   r   r   pydanticr	   r
   tenacityr   r   r   tenacity.waitr   
_constantsr   _utilsr   r   r   r   r   r   r?   rH   rD   r   r   r   r    <module>   s    	] 