o |nhH@sddlmZddlZddlZddlZddlZddlZddlZddlm Z ddl m Z ddl m Z mZmZmZmZmZmZddlmZejrWdd lmZdd lmZdd lmZeeZGd d d ej Z!GdddZ"e"de"_#dS)) annotationsN) takewhile) TracebackType)ConnectTimeoutError InvalidHeader MaxRetryError ProtocolError ProxyErrorReadTimeoutError ResponseError)reraise)Self)ConnectionPool)BaseHTTPResponsec@s6eZdZUded<ded<ded<ded<ded<d S) RequestHistory str | NonemethodurlException | Noneerror int | Nonestatusredirect_locationN)__name__ __module__ __qualname____annotations__rrJ/usr/local/bin/dhwp/env/lib/python3.10/site-packages/urllib3/util/retry.pyr!s  rc@s"eZdZUdZegdZegdZegdZdZde d<dd d d d d ed d ed d d d ed fdbd'd(Z dcd,d-Z e ddded1d2Z dfd3d4Zdgd7d8Zdhdd?Zdjd@dAZdkdldCdDZdmdGdHZdmdIdJZdndLdMZ NdodpdRdSZdqdTdUZ drdsd^d_Zdtd`daZd S)uRetryaRetry configuration. Each retry attempt will create a new Retry object with updated values, so they can be safely reused. Retries can be defined as a default for a pool: .. code-block:: python retries = Retry(connect=5, read=2, redirect=5) http = PoolManager(retries=retries) response = http.request("GET", "https://example.com/") Or per-request (which overrides the default for the pool): .. code-block:: python response = http.request("GET", "https://example.com/", retries=Retry(10)) Retries can be disabled by passing ``False``: .. code-block:: python response = http.request("GET", "https://example.com/", retries=False) Errors will be wrapped in :class:`~urllib3.exceptions.MaxRetryError` unless retries are disabled, in which case the causing exception will be raised. :param int total: Total number of retries to allow. Takes precedence over other counts. Set to ``None`` to remove this constraint and fall back on other counts. Set to ``0`` to fail on the first retry. Set to ``False`` to disable and imply ``raise_on_redirect=False``. :param int connect: How many connection-related errors to retry on. These are errors raised before the request is sent to the remote server, which we assume has not triggered the server to process the request. Set to ``0`` to fail on the first retry of this type. :param int read: How many times to retry on read errors. These errors are raised after the request was sent to the server, so the request may have side-effects. Set to ``0`` to fail on the first retry of this type. :param int redirect: How many redirects to perform. Limit this to avoid infinite redirect loops. A redirect is a HTTP response with a status code 301, 302, 303, 307 or 308. Set to ``0`` to fail on the first retry of this type. Set to ``False`` to disable and imply ``raise_on_redirect=False``. :param int status: How many times to retry on bad status codes. These are retries made on responses, where status code matches ``status_forcelist``. Set to ``0`` to fail on the first retry of this type. :param int other: How many times to retry on other errors. Other errors are errors that are not connect, read, redirect or status errors. These errors might be raised after the request was sent to the server, so the request might have side-effects. Set to ``0`` to fail on the first retry of this type. If ``total`` is not set, it's a good idea to set this to 0 to account for unexpected edge cases and avoid infinite retry loops. :param Collection allowed_methods: Set of uppercased HTTP method verbs that we should retry on. By default, we only retry on methods which are considered to be idempotent (multiple requests with the same parameters end with the same state). See :attr:`Retry.DEFAULT_ALLOWED_METHODS`. Set to a ``None`` value to retry on any verb. :param Collection status_forcelist: A set of integer HTTP status codes that we should force a retry on. A retry is initiated if the request method is in ``allowed_methods`` and the response status code is in ``status_forcelist``. By default, this is disabled with ``None``. :param float backoff_factor: A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a second try without a delay). urllib3 will sleep for:: {backoff factor} * (2 ** ({number of previous retries})) seconds. If `backoff_jitter` is non-zero, this sleep is extended by:: random.uniform(0, {backoff jitter}) seconds. For example, if the backoff_factor is 0.1, then :func:`Retry.sleep` will sleep for [0.0s, 0.2s, 0.4s, 0.8s, ...] between retries. No backoff will ever be longer than `backoff_max`. By default, backoff is disabled (factor set to 0). :param bool raise_on_redirect: Whether, if the number of redirects is exhausted, to raise a MaxRetryError, or to return a response with a response code in the 3xx range. :param bool raise_on_status: Similar meaning to ``raise_on_redirect``: whether we should raise an exception, or return a response, if status falls in ``status_forcelist`` range and retries have been exhausted. :param tuple history: The history of the request encountered during each call to :meth:`~Retry.increment`. The list is in the order the requests occurred. Each list item is of class :class:`RequestHistory`. :param bool respect_retry_after_header: Whether to respect Retry-After header on status codes defined as :attr:`Retry.RETRY_AFTER_STATUS_CODES` or not. :param Collection remove_headers_on_redirect: Sequence of headers to remove from the request when a response indicating a redirect is returned before firing off the redirected request. )HEADGETPUTDELETEOPTIONSTRACE)iii)Cookie AuthorizationzProxy-Authorizationxztyping.ClassVar[Retry]DEFAULT NrTtotalbool | int | Noneconnectrreadredirectrotherallowed_methodstyping.Collection[str] | Nonestatus_forcelisttyping.Collection[int] | Nonebackoff_factorfloat backoff_maxraise_on_redirectboolraise_on_statushistory!tuple[RequestHistory, ...] | Nonerespect_retry_after_headerremove_headers_on_redirecttyping.Collection[str]backoff_jitterreturnNonecCs||_||_||_||_||_|dus|durd}d} ||_|p"t|_||_| |_ | |_ | |_ | |_ | p6d|_ ||_tdd|D|_||_dS)NFrrcss|]}|VqdSN)lower).0hrrr s z!Retry.__init__..)r.r0r1rr3r2setr6r4r8r:r;r=r>r@ frozensetrArC)selfr.r0r1r2rr3r4r6r8r:r;r=r>r@rArCrrr __init__s*   zRetry.__init__kw typing.AnyrcKstdid|jd|jd|jd|jd|jd|jd|jd|jd |j d |j d |j d |j d |j d|jd|jd|j}||t|di|S)Nr.r0r1r2rr3r4r6r8r:r;r=r>rAr@rCr)dictr.r0r1r2rr3r4r6r8r:r;r=r>rAr@rCupdatetype)rMrOparamsrrr newsF      z Retry.newretriesRetry | bool | int | NonedefaultcCsR|dur |dur |n|j}t|tr|St|od}|||d}td|||S)z3Backwards-compatibility for the old retries format.N)r2z!Converted retries value: %r -> %r)r+ isinstancer!r<logdebug)clsrVr2rX new_retriesrrr from_ints   zRetry.from_intcCsltttddt|j}|dkrdS|jd|d}|jdkr+|t|j7}tt dt |j |S)zIFormula for computing the current backoff :rtype: float cSs |jduSrF)r)xrrr )s z(Retry.get_backoff_time..r rrr-) lenlistrreversedr>r8rCrandomr9maxminr:)rMconsecutive_errors_len backoff_valuerrr get_backoff_time!s zRetry.get_backoff_time retry_afterstrcCs^td|r t|}ntj|}|durtd|tj|}|t}t |d}|S)Nz^\s*[0-9]+\s*$zInvalid Retry-After header: r) rematchintemailutils parsedate_tzr mktime_tztimere)rMrjsecondsretry_date_tuple retry_daterrr parse_retry_after4s      zRetry.parse_retry_afterresponser float | NonecCs"|jd}|dur dS||S)z(Get the value of Retry-After in seconds.z Retry-AfterN)headersgetrwrMrxrjrrr get_retry_afterEs  zRetry.get_retry_aftercCs ||}|rt|dSdS)NTF)r}rssleepr|rrr sleep_for_retryOs  zRetry.sleep_for_retrycCs"|}|dkr dSt|dS)Nr)rirsr~)rMbackoffrrr _sleep_backoffWszRetry._sleep_backoffBaseHTTPResponse | NonecCs(|jr|r||}|rdS|dS)aBSleep between retry attempts. This method will respect a server's ``Retry-After`` response header and sleep the duration of the time requested. If that is not present, it will use an exponential backoff. By default, the backoff factor is 0 and this method will return immediately. N)r@rr)rMrxsleptrrr r~]s  z Retry.sleeperr ExceptioncCst|tr|j}t|tS)zzErrors when we're fairly sure that the server did not receive the request, so it should be safe to retry. )rYr original_errorrrMrrrr _is_connection_errorms  zRetry._is_connection_errorcCst|ttfS)zErrors that occur after the request has been started, so we should assume that the server began processing it. )rYr r rrrr _is_read_erroruszRetry._is_read_errorrcCs|jr ||jvr dSdS)zyChecks if a given HTTP method should be retried upon, depending if it is included in the allowed_methods FT)r4upper)rMrrrr _is_method_retryable{szRetry._is_method_retryableF status_codernhas_retry_aftercCs@||sdS|jr||jvrdSt|jo|jo|o||jvS)awIs this method/status code retryable? (Based on allowlists and control variables such as the number of total retries to allow, whether to respect the Retry-After header, whether this header is present, and whether the returned status code is on the list of status codes to be retried upon on the presence of the aforementioned header) FT)rr6r<r.r@RETRY_AFTER_STATUS_CODES)rMrrrrrr is_retrys zRetry.is_retrycCs:dd|j|j|j|j|j|jfD}|sdSt|dkS)zAre we out of retries?cSsg|]}|r|qSrr)rHr_rrr s z&Retry.is_exhausted..Fr)r.r0r1r2rr3rf)rM retry_countsrrr is_exhausteds  zRetry.is_exhaustedrrrr_poolConnectionPool | None _stacktraceTracebackType | Nonec Cs|jdur|rtt||||j}|dur|d8}|j}|j} |j} |j} |j} d} d}d}|rK||rK|durBtt||||durJ|d8}ng|rp| |rp| dus_|dus_| |sgtt|||| duro| d8} nB|r{| durz| d8} n7|r| r| dur| d8} d} | }|r|}|j}nt j } |r|jr| dur| d8} t jj|jd} |j}|jt|||||f}|j||| | | | |d}|r|pt | }t||||td|||S) aReturn a new Retry object with incremented retry counters. :param response: A response object, or None, if the server did not return a response. :type response: :class:`~urllib3.response.BaseHTTPResponse` :param Exception error: An error encountered during the request, or None if the response was received successfully. :return: A new ``Retry`` object. FNr unknownztoo many redirects)r)r.r0r1r2rr3r>z$Incremented Retry for (url='%s'): %r)r.rrSr0r1r2rr3rrrget_redirect_locationr GENERIC_ERRORSPECIFIC_ERRORformatr>rrUrrrZr[)rMrrrxrrrr.r0r1r2 status_countr3causerrresponse_redirect_locationr> new_retryreasonrrr incrementsx   zRetry.incrementc Cs8t|jd|jd|jd|jd|jd|jd S)Nz(total=z , connect=z, read=z , redirect=z , status=))rSrr.r0r1r2r)rMrrr __repr__ szRetry.__repr__)"r.r/r0rr1rr2r/rrr3rr4r5r6r7r8r9r:r9r;r<r=r<r>r?r@r<rArBrCr9rDrE)rOrPrDr)TN)rVrWr2r/rXrWrDr!)rDr9)rjrkrDr9)rxrrDry)rxrrDr<)rDrErF)rxrrDrE)rrrDr<)rrkrDr<)F)rrkrrnrr<rDr<)rDr<)NNNNNN)rrrrrxrrrrrrrrDr)rDrk)rrr__doc__rLDEFAULT_ALLOWED_METHODSr"DEFAULT_REMOVE_HEADERS_ON_REDIRECTDEFAULT_BACKOFF_MAXrrNrU classmethodr^rirwr}rrr~rrrrrrrrrrr r!)sl   -          ar!)$ __future__rrologgingrdrlrstyping itertoolsrtypesr exceptionsrrrr r r r utilr TYPE_CHECKINGtyping_extensionsrconnectionpoolrrxr getLoggerrrZ NamedTuplerr!r+rrrr s,   $     o