Ë
    8ø»jDƒ  ã                  ó€  — d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
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 d dlmZ d d	lmZmZ d d
lmZmZmZm Z m!Z!m"Z"m#Z# d dl$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/ d dl0m1Z1 d dl2m3Z3 er(d dl4m5Z5m6Z6m7Z7m8Z8m9Z9 d dl:m;Z; d dlm<Z< d dl=m>Z> d dl?m@Z@mAZA  ej„                  e)«      ZC e'eC«      ZD ed«      ZE e%d«      e G d„ de«      «       «       ZF e%d«       G d„ d«      «       ZG e%d«       G d„ deG«      «       ZH e%d«       G d„ d eG«      «       ZIy)!é    )ÚannotationsN)Úsuppress)ÚUTCÚdatetimeÚ	timedelta)Ú
HTTPStatus)Úmetadata)ÚTYPE_CHECKINGÚAnyÚTypeVar)Ú	urlencode)ÚProtocolÚruntime_checkable)ÚDEFAULT_MAX_RETRIESÚ!DEFAULT_MIN_DELAY_BETWEEN_RETRIESÚDEFAULT_TIMEOUT_LONGÚDEFAULT_TIMEOUT_MAXÚDEFAULT_TIMEOUT_MEDIUMÚDEFAULT_TIMEOUT_SHORTÚMIN_COMPRESSION_SIZE)Ú
docs_group)Ú
LoggerOnceÚlog_contextÚlogger_name)ÚClientStatistics)Úis_compressible_content_type)Ú
to_seconds)ÚApifyApiError)ÚGzipHttpCompressor)ÚAsyncIteratorÚ	AwaitableÚCallableÚIteratorÚMapping)ÚTracebackType)ÚSelf)ÚHttpCompressor)ÚJsonSerializableÚTimeoutÚTzHTTP clientsc                  ó�   — e Zd ZdZedd„«       Zedd„«       Zedd„«       Zedd„«       Zdd„Z	dd„Z
dd„Zdd	„Zdd
„Zdd„Zdd„Zy)ÚHttpResponsea  Protocol for HTTP response objects returned by HTTP clients.

    Any object that has the required attributes and methods can be used as an HTTP response
    (e.g., `impit.Response`). This enables custom HTTP client implementations to return
    their own response types.
    c                 ó   — y)z!HTTP status code of the response.N© ©Úselfs    úV/var/www/html/GAP/venv/lib/python3.12/site-packages/apify_client/http_clients/_base.pyÚstatus_codezHttpResponse.status_code?   ó   � ó    c                 ó   — y)zResponse body decoded as text.Nr.   r/   s    r1   ÚtextzHttpResponse.textC   r3   r4   c                 ó   — y)zRaw response body as bytes.Nr.   r/   s    r1   ÚcontentzHttpResponse.contentG   r3   r4   c                 ó   — y)zResponse headers as a mapping.Nr.   r/   s    r1   ÚheaderszHttpResponse.headersK   r3   r4   c                 ó   — y)zParse response body as JSON.Nr.   r/   s    r1   ÚjsonzHttpResponse.jsonO   r3   r4   c                 ó   — y)zRead the entire response body.Nr.   r/   s    r1   ÚreadzHttpResponse.readR   r3   r4   c              ƒ  ó   K  — y­w)z-Read the entire response body asynchronously.Nr.   r/   s    r1   ÚareadzHttpResponse.areadU   ó   è ø� ùó   ‚c                 ó   — y)z.Close the response and release the connection.Nr.   r/   s    r1   ÚclosezHttpResponse.closeX   r3   r4   c              ƒ  ó   K  — y­w)z=Close the response and release the connection asynchronously.Nr.   r/   s    r1   ÚaclosezHttpResponse.aclose[   rA   rB   c                 ó   — y)z/Iterate over the response body in bytes chunks.Nr.   r/   s    r1   Ú
iter_byteszHttpResponse.iter_bytes^   r3   r4   c                 ó   — y)z>Iterate over the response body in bytes chunks asynchronously.Nr.   r/   s    r1   Úaiter_byteszHttpResponse.aiter_bytesa   r3   r4   N)ÚreturnÚint)rK   Ústr)rK   Úbytes)rK   zMapping[str, str])rK   r   ©rK   ÚNone)rK   zIterator[bytes])rK   zAsyncIterator[bytes])Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr2   r6   r8   r:   r<   r>   r@   rD   rF   rH   rJ   r.   r4   r1   r,   r,   5   s|   „ ñð ò0ó ð0ð ò-ó ð-ð ò*ó ð*ð ò-ó ð-ó+ó-ó<ó=óLó>ôMr4   r,   c                  ó  — e Zd ZdZdeeeeee	ddddœ
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z
dd„Zdd„Zdd„Zedd„«       Zedd	„«       Zedd
„«       Zedd„«       Zdd„Zdddddœ	 	 	 	 	 	 	 	 	 dd„Zddœdd„Zdd„Z	 	 	 	 	 	 	 	 dd„Zy)ÚHttpClientBaseaÌ  Shared configuration and utilities for HTTP clients.

    Provides common functionality for both sync and async HTTP clients including: header construction, parameter
    parsing, request body preparation, URL building, timeout calculation, and error classification.

    Subclasses should call `super().__init__()` to initialize shared configuration. The helpers are then used by the
    inherited `call`, and stay available to a client that replaces it.
    N)
ÚtokenÚtimeout_shortÚtimeout_mediumÚtimeout_longÚtimeout_maxÚmax_retriesÚmin_delay_between_retriesÚ
statisticsr:   Úhttp_compressorc       
        ó`  — |
�|
n	t        «       | _        || _        || _        || _        || _        || _        || _        |xs
 t        «       | _	        ddi}t        j                  d«      }|�||d<   t        dt        j                  v «      j                  «       }dj                  t         j"                  dd D �cg c]  }t        |«      ‘Œ c}«      }t%        j&                  d	«      }d
|› dt         j(                  › d|› d|› �}||d<   |�d|› �|d<   | j+                  ||	«      | _        yc c}w )aÍ  Initialize the HTTP client base.

        Args:
            token: Apify API token for authentication.
            timeout_short: Default timeout for short-duration API operations (simple CRUD operations, ...).
            timeout_medium: Default timeout for medium-duration API operations (batch operations, listing, ...).
            timeout_long: Default timeout for long-duration API operations (long-polling, streaming, ...).
            timeout_max: Maximum timeout cap for any single request attempt, including tier and per-call timeouts.
            max_retries: Maximum number of retries for failed requests.
            min_delay_between_retries: Minimum delay between retries.
            statistics: Statistics tracker for API calls. Created automatically if not provided.
            headers: Additional HTTP headers to include in all requests.
            http_compressor: Compressor used to compress request bodies. Defaults to `GzipHttpCompressor`.
        NÚAcceptzapplication/json, */*ÚAPIFY_WORKFLOW_KEYzX-Apify-Workflow-KeyÚAPIFY_IS_AT_HOMEú.é   zapify-clientzApifyClient/z (z	; Python/z); isAtHome/z
User-AgentúBearer ÚAuthorization)r   Ú_http_compressorÚ_timeout_shortÚ_timeout_mediumÚ_timeout_longÚ_timeout_maxÚ_max_retriesÚ_min_delay_between_retriesr   Ú_statisticsÚosÚgetenvrM   ÚenvironÚlowerÚjoinÚsysÚversion_infor	   ÚversionÚplatformÚ_merge_headersÚ_headers)r0   rX   rY   rZ   r[   r\   r]   r^   r_   r:   r`   Údefault_headersÚworkflow_keyÚ
is_at_homeÚxÚpython_versionÚclient_versionÚ
user_agents                     r1   Ú__init__zHttpClientBase.__init__p   s?  € ð8 4CÐ3N¡ÔTfÓThˆÔØ+ˆÔØ-ˆÔØ)ˆÔØ'ˆÔØ'ˆÔØ*CˆÔ'Ø%Ò;Ô)9Ó);ˆÔð ,4Ð5LÐ*Mˆä—y‘yÐ!5Ó6ˆØÐ#Ø6BˆOÐ2Ñ3äÐ+¬r¯z©zÐ9Ó:×@Ñ@ÓBˆ
ØŸ™´3×3CÑ3CÀBÀQÐ3GÖ"H¨a¤3 q¥6Ò"HÓIˆÜ!×)Ñ)¨.Ó9ˆà# NÐ#3°2´c·l±l°^À9È^ÐL\Ð\hÐisÐhtÐuˆ
Ø(2ˆ˜Ñ%àÐØ18¸¸Ð/@ˆO˜OÑ,à×+Ñ+¨O¸WÓEˆ�ùò #Is   Â:D+c                ób   — | j                  | j                  d«      €d|› �| j                  d<   yy)zÉSet the `Authorization` header from the token, unless an authorization header is already configured.

        Args:
            token: The Apify API token to set as the `Bearer` authorization.
        ÚauthorizationNrg   rh   )Ú_get_headerr{   )r0   rX   s     r1   Úset_default_authorizationz(HttpClientBase.set_default_authorization¨   s6   € ð ×Ñ˜DŸM™M¨?Ó;ÐCØ/6°u°gÐ->ˆD�M‰M˜/Ò*ð Dr4   c                ó"   — t        |t        «      S )zÍReturn whether an exception represents a transport timeout.

        Recognizes Python's own `TimeoutError`. Transport adapters extend it with the timeout types their HTTP
        library defines.
        )Ú
isinstanceÚTimeoutError)r0   Úexcs     r1   Úis_timeout_errorzHttpClientBase.is_timeout_error±   s   € ô ˜#œ|Ó,Ð,r4   c                ó   — |}y)a!  Return whether an underlying HTTP-library exception is retryable.

        The default classifies nothing as retryable, so a transport that doesn't override it gives up on the first
        connection failure. Every transport adapter should map its own transient error types here.
        Fr.   )r0   r‹   Ú_s      r1   Úis_retryable_transport_errorz+HttpClientBase.is_retryable_transport_error¹   s   € ð ˆØr4   c                óä   — | rt        | «      ni }|xs i j                  «       D ]D  \  }}|D �cg c]&  }|j                  «       |j                  «       k(  sŒ%|‘Œ( c}D ]  }||= Œ |||<   ŒF |S c c}w )zíMerge two header dicts, treating header names case-insensitively.

        A header from `override` replaces a same-named header in `base` regardless of the casing
        of either name, and keeps the casing it was passed with.
        )ÚdictÚitemsrt   )ÚbaseÚoverrideÚmergedÚkeyÚvalueÚkÚexisting_keys          r1   rz   zHttpClientBase._merge_headersÂ   s|   € ñ  $”�d”¨ˆØ#š> r×0Ñ0Ó2ò 	 ‰JˆC�Ø,2Ö O q°a·g±g³iÀ3Ç9Á9Ã;Ó6N¢Ò Oò )�Ø˜<Ñ(ð)àˆF�3ŠKð	 ð ˆùò !Ps   ®&A-ÁA-c                óJ   ‡— t        ˆfd„| j                  «       D «       d«      S )zdLook up a header value by name, treated case-insensitively. Returns `None` if the header is not set.c              3  ól   •K  — | ]+  \  }}|j                  «       ‰j                  «       k(  sŒ(|–— Œ- y ­w©N)rt   )Ú.0r–   r—   Únames      €r1   ú	<genexpr>z-HttpClientBase._get_header.<locals>.<genexpr>Ó   s(   øè ø€ Ò\™z˜s EÀÇ	Á	ÃÈtÏzÉzË|Ó@[”UÑ\ùs   ƒ)4­4N)Únextr’   )r:   rž   s    `r1   r†   zHttpClientBase._get_headerÐ   s   ø€ ô Ó\¨W¯]©]«_Ô\Ð^bÓcÐcr4   c                óÖ  — | €yi }| j                  «       D ]Ð  \  }}t        |t        «      rt        |«      j	                  «       ||<   Œ3t        |t
        «      rdj                  |«      ||<   ŒXt        |t        «      ra|j                  €|j                  t        ¬«      n|}|j                  t        «      }|j                  d¬«      }|j                  dd«      ||<   ŒÉ|€ŒÌ|||<   ŒÒ |S )z·Convert request parameters to Apify API-compatible formats.

        Converts booleans to 'false'/'true', lists to comma-separated strings, datetimes to ISO 8601 Zulu format.
        Nú,)ÚtzinfoÚmilliseconds)Útimespecz+00:00ÚZ)r’   r‰   ÚboolrM   rt   Úlistru   r   r£   Úreplacer   Ú
astimezoneÚ	isoformat)ÚparamsÚparsed_paramsr–   r—   ÚawareÚutc_aware_dtÚiso_strs          r1   Ú_parse_paramszHttpClientBase._parse_paramsÕ   sÜ   € ð ˆ>Øà(*ˆØ Ÿ,™,›.ò 	+‰JˆC�Ü˜%¤Ô&Ü&)¨%£j×%7Ñ%7Ó%9�˜cÒ"Ü˜E¤4Ô(Ø%(§X¡X¨e£_�˜cÒ"Ü˜E¤8Ô,à5:·\±\Ð5I˜Ÿ™¬S˜Ô1Èu�Ø$×/Ñ/´Ó4�Ø&×0Ñ0¸.Ð0ÓI�Ø%,§_¡_°X¸sÓ%C�˜cÒ"ØÑ"Ø%*�˜cÒ"ð	+ð Ðr4   c                ó^   — t        | t        t        t        f«      rt	        | «      t
        k\  S y)a  Whether the body is large enough that `_prepare_request_call` may compress it.

        This gate only picks where the preparation runs (worker thread or inline), so it approximates rather
        than replicates the compression conditions: the content type and `Content-Encoding` checks are skipped,
        and a `str` is measured in characters instead of encoded bytes. A misjudged body costs either one
        needless thread hop or an inline preparation of a body under `MIN_COMPRESSION_SIZE` characters - both
        cheap.
        F)r‰   rM   rN   Ú	bytearrayÚlenr   )Údatas    r1   Ú_is_body_worth_compressingz)HttpClientBase._is_body_worth_compressingï   s(   € ô �dœS¤%¬Ð3Ô4Ü�t“9Ô 4Ñ4Ð4Ør4   c               ó¬  — |dk(  ry|dk(  r| j                   }n&|dk(  r| j                  }n|dk(  r| j                  }n|}|| j                  kD  r^t        j                  dt        |«      › dt        | j                  «      › d�d	t        |t        «      r|nd
› �t        j                  ¬«       t        |d|dz
  z  z  | j                  «      }t        |«      S )a°  Resolve a timeout tier and compute the timeout for a request attempt with exponential increase.

        For `no_timeout`, returns `None` to indicate no timeout. For tier literals and explicit `timedelta` values,
        doubles the timeout with each attempt but caps at `timeout_max`. A base timeout above `timeout_max` is
        capped too, which warns once per timeout kind since the requested value does not take effect in full.

        Args:
            timeout: The timeout specification to resolve (tier literal or explicit `timedelta`).
            attempt: Current attempt number (1-indexed).

        Returns:
            Timeout in seconds, or `None` for no timeout.
        Ú
no_timeoutNÚshortÚmediumÚlongzThe requested timeout of zs exceeds `timeout_max` (z[s) and is capped at it. Raise `timeout_max` on the client to allow longer request timeouts.ztimeout-capped-Úexplicit)r–   Úlevelé   é   )rj   rk   rl   rm   Úlogger_onceÚlogr   r‰   rM   ÚloggingÚWARNINGÚmin)r0   ÚtimeoutÚattemptÚresolvedÚnew_timeouts        r1   Ú_compute_timeoutzHttpClientBase._compute_timeoutý   sæ   € ð �lÒ"Øà�gÒØ×*Ñ*‰HØ˜Ò Ø×+Ñ+‰HØ˜ÒØ×)Ñ)‰HàˆHà�d×'Ñ'Ò'ä�O‰OØ+¬J°xÓ,@Ð+Að BÜ˜t×0Ñ0Ó1Ð2ð 34ð4ð &´¸GÄSÔ1I¡gÈzÐ%ZÐ[Ü—o‘oð ô ô ˜( a¨G°a©KÑ&8Ñ9¸4×;LÑ;LÓMˆÜ˜+Ó&Ð&r4   ©r:   r¬   rµ   r<   c               ó¾  — |�|�t        d«      ‚| j                  | j                  |«      }|�Ct        j                  |ddt
        ¬«      j                  d«      }| j                  |d«      €d|d<   t        |t
        t        t        f«      r¿t        |t
        «      r|j                  d«      }nt        |t        «      rt        |«      }| j                  |d«      €pt        |«      t        k\  r^t        | j                  |d«      «      rC| j                  j                  |«      }| j                  |d	| j                  j                   i«      }|| j#                  |«      |fS )
aX  Prepare headers, params, and body for an HTTP request.

        Merges the client's default headers (including authorization) with per-request headers and serializes a
        JSON body. Header names are treated case-insensitively and per-request values win over the client
        defaults. For JSON bodies, a `Content-Type` header is set unless the caller supplied one.

        The body is compressed unless a `Content-Encoding` header is already set, the body is smaller than
        `MIN_COMPRESSION_SIZE`, or its content type says the payload is already compressed. A caller-supplied
        `Content-Encoding` is forwarded verbatim, which is how a pre-encoded body is uploaded - including one in
        an encoding the client ships no compressor for. `Content-Encoding: identity` therefore opts a single
        request out of compression.
        z?Cannot pass both "json" and "data" parameters at the same time!F)Úensure_asciiÚ	allow_nanÚdefaultzutf-8zcontent-typezapplication/jsonzContent-Typezcontent-encodingzContent-Encoding)Ú
ValueErrorrz   r{   ÚjsonlibÚdumpsrM   Úencoder†   r‰   rN   r³   r´   r   r   ri   ÚcompressÚcontent_encodingr±   )r0   r:   r¬   rµ   r<   s        r1   Ú_prepare_request_callz$HttpClientBase._prepare_request_call$  s=  € ð( Ð Ð 0ÜÐ^Ó_Ð_à×%Ñ% d§m¡m°WÓ=ˆð ÐÜ—=‘= °EÀUÔTWÔX×_Ñ_Ð`gÓhˆDØ×Ñ ¨Ó8Ð@Ø*<�˜Ñ'ä�dœS¤%¬Ð3Ô4Ü˜$¤Ô$Ø—{‘{ 7Ó+‘Ü˜D¤)Ô,Ü˜T“{�ð
 × Ñ  Ð*<Ó=ÐEÜ˜“IÔ!5Ò5Ü0°×1AÑ1AÀ'È>Ó1ZÔ[à×,Ñ,×5Ñ5°dÓ;�Ø×-Ñ-¨gÐ8JÈD×LaÑLa×LrÑLrÐ7sÓt�à˜×+Ñ+¨FÓ3°TÐ:Ð:r4   ©r¬   c               ó*  ‡— |s|S t        t        t        t        f      «       }|j                  «       D ]L  \  Š}t	        |t         «      r|j                  ˆfd„|D «       «       Œ1|j                  ‰t        |«      f«       ŒN t        |«      }|› d|› �S )zcBuild a URL with query parameters appended. List values are expanded into multiple key=value pairs.c              3  ó8   •K  — | ]  }‰t        |«      f–— Œ y ­wrœ   )rM   )r�   Úvr–   s     €r1   rŸ   z8HttpClientBase._build_url_with_params.<locals>.<genexpr>]  s   øè ø€ Ò"@°Q C¬¨Q«¤=Ñ"@ùs   ƒú?)r¨   ÚtuplerM   r’   r‰   ÚextendÚappendr   )r0   Úurlr¬   Úparam_pairsr—   Úquery_stringr–   s         @r1   Ú_build_url_with_paramsz%HttpClientBase._build_url_with_paramsU  s‹   ø€ áØˆJäœ5¤¤c ™?Ñ+Ó-ˆØ Ÿ,™,›.ò 	6‰JˆC�Ü˜%¤Ô&Ø×"Ñ"Ó"@¸%Ô"@Õ@à×"Ñ" C¬¨U«Ð#4Õ5ð		6ô ! Ó-ˆà��a˜�~Ð&Ð&r4   c               ó’   — t         j                  d|¬«       | j                  |«      st         j                  d|¬«        |«        yy)zEStop retrying when an exception is not a retryable transport failure.zRequest threw exception©Úexc_infoúException is not retryableN)ÚloggerÚdebugr�   )r0   r‹   Ústop_retryings      r1   Ú_handle_request_exceptionz(HttpClientBase._handle_request_exceptione  s<   € ä�‰Ð.¸ˆÔ=Ø×0Ñ0°Ô5Ü�L‰LÐ5ÀˆLÔDÙ�Oð 6r4   c               ó  — |j                   t        j                  k  r$t        j	                  dd|j                   i¬«       y|j                   t        j
                  k(  r| j                  j                  |«       t        j	                  dd|j                   i¬«       |j                   t        j                  k  rG|j                   t        j
                  k7  r*t        j	                  dd|j                   i¬«        |«        y)zÉRecord the response status and stop retrying unless it is a server error or a rate limit.

        Returns whether the response is a success, so the caller can hand it back instead of raising.
        zRequest successfulr2   )ÚextraTzRequest unsuccessfulzStatus code is not retryableF)	r2   r   ÚMULTIPLE_CHOICESræ   rç   ÚTOO_MANY_REQUESTSrp   Úadd_rate_limit_errorÚINTERNAL_SERVER_ERROR)r0   ÚresponserÆ   rè   s       r1   Ú_handle_response_statusz&HttpClientBase._handle_response_statusl  sÇ   € ð ×Ñ¤*×"=Ñ"=Ò=Ü�L‰LÐ-°mÀX×EYÑEYÐ5ZˆLÔ[Øà×Ñ¤:×#?Ñ#?Ò?Ø×Ñ×1Ñ1°'Ô:ä�‰Ð+°MÀ8×CWÑCWÐ3XˆÔYà× Ñ ¤:×#CÑ#CÒCØ×$Ñ$¬
×(DÑ(DÒDä�L‰LÐ7ÀÈx×OcÑOcÐ?dˆLÔeÙŒOàr4   )rX   ú
str | NonerY   r   rZ   r   r[   r   r\   r   r]   rL   r^   r   r_   zClientStatistics | Noner:   údict[str, str] | Noner`   zHttpCompressor | NonerK   rP   )rX   rM   rK   rP   )r‹   Ú	ExceptionrK   r§   )r“   ró   r”   ró   rK   údict[str, str])r:   rõ   rž   rM   rK   rò   )r¬   údict[str, Any] | NonerK   rö   )rµ   ústr | bytes | bytearray | NonerK   r§   )rÅ   r)   rÆ   rL   rK   zint | float | None)
r:   ró   r¬   rö   rµ   r÷   r<   úJsonSerializable | NonerK   z:tuple[dict[str, str], dict[str, Any] | None, bytes | None])rÞ   rM   r¬   rö   rK   rM   )r‹   rô   rè   úCallable[[], None]rK   rP   )rð   r,   rÆ   rL   rè   rù   rK   r§   )rQ   rR   rS   rT   r   r   r   r   r   r   rƒ   r‡   rŒ   r�   Ústaticmethodrz   r†   r±   r¶   rÉ   rÕ   rá   ré   rñ   r.   r4   r1   rW   rW   e   s‘  „ ñð !Ø#8Ø$:Ø"6Ø!4Ø.Ø/PØ.2Ø)-Ø15ñ6Fð ð6Fð !ð	6Fð
 "ð6Fð  ð6Fð ð6Fð ð6Fð $-ð6Fð ,ð6Fð 'ð6Fð /ð6Fð 
ó6Fóp?ó-óð òó ðð òdó ðdð òó ðð2 òó ðó%'ðT *.Ø(,Ø/3Ø(,ñ/;ð 'ð/;ð &ð	/;ð
 -ð/;ð &ð/;ð 
Dó/;ðb SWõ 'ó ðàðð ð	ð
 *ðð 
ôr4   rW   c                  ó  — e Zd ZdZdd„Z	 	 	 	 	 	 	 	 dd„Zdddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	„Ze	d
 e
d¬«      dddœ	 	 	 	 	 	 	 	 	 	 	 dd„«       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zy)Ú
HttpClientaW  Base class for synchronous HTTP clients used by `ApifyClient`.

    Concrete clients inherit its request preparation, retry, and error handling, and implement the transport,
    error-classification, and lifecycle hooks. Only `send_request` has to be implemented, the other hooks have
    defaults. Replacing `call` itself also remains supported, and then the transport and retry-classification hooks
    are bypassed. Helper methods from `HttpClientBase` remain available for request preparation, URL building, and
    parameter parsing.

    The client's default headers from `self._headers` have to go out with every request, otherwise the
    `Authorization` header never reaches the API. The inherited `call` merges them into the per-request headers
    through `_prepare_request_call`, so only a client that replaces `call` has to do it itself.
    c                ó   — | S )z7Return this client and close it when the context exits.r.   r/   s    r1   Ú	__enter__zHttpClient.__enter__˜  s   € àˆr4   c                ó$   — | j                  «        y)z)Close resources owned by the HTTP client.N)rD   ©r0   Úexc_typeÚ	exc_valueÚ	tracebacks       r1   Ú__exit__zHttpClient.__exit__œ  s   € ð 	�
‰
�r4   Nrº   ©r:   r¬   rµ   r<   ÚstreamrÅ   c          
     ób  ‡ ‡‡‡‡‡	‡
‡— t         j                  j                  ‰«       t         j                  j                  ‰«       ‰ j                  xj
                  dz  c_        ‰ j                  ||||¬«      \  Š
ŠŠ	‰ j                  ˆ	ˆˆ
ˆˆ ˆˆˆfd„‰ j                  ‰ j                  ¬«      S )á(  Make an HTTP request with automatic retry and exponential backoff.

        Args:
            method: HTTP method (GET, POST, PUT, DELETE, etc.).
            url: Full URL to make the request to.
            headers: Additional headers to include.
            params: Query parameters to append to the URL.
            data: Raw request body data. Cannot be used together with json.
            json: JSON-serializable data for the request body. Cannot be used together with data.
            stream: Whether to stream the response body.
            timeout: Timeout for the API HTTP request. Use `short`, `medium`, or `long` tier literals for
                preconfigured timeouts. A `timedelta` overrides it for this call (capped at `timeout_max`), and
                `no_timeout` disables the timeout entirely.

        Returns:
            The HTTP response object.

        Raises:
            ApifyApiError: If the request fails after all retries or returns a non-retryable error status.
            ValueError: If both json and data are provided.
        r¿   rÊ   c                ó8   •— ‰j                  | |‰‰	‰‰‰‰‰¬«	      S ©N)	rè   rÆ   ÚmethodrÞ   r:   r¬   r8   r  rÅ   ©Ú_make_request©
rè   rÆ   r8   r  Úprepared_headersÚprepared_paramsr0   r  rÅ   rÞ   s
     €€€€€€€€r1   ú<lambda>z!HttpClient.call.<locals>.<lambda>Ó  ó2   ø€ ¨4×+=Ñ+=Ø+ØØØØ(Ø&ØØØð ,>ó 
,€ r4   ©r]   Úbackoff_base)
r   r  ÚsetrÞ   rp   ÚcallsrÕ   Ú_retry_with_exp_backoffrn   ro   ©r0   r  rÞ   r:   r¬   rµ   r<   r  rÅ   r8   r  r  s   ```    ``@@@r1   ÚcallzHttpClient.call¥  s¥   ÿ€ ôB 	×Ñ×Ñ˜vÔ&Ü�‰×Ñ˜CÔ à×Ñ×Ò !Ñ#Õà59×5OÑ5OØØØØð	 6Pó 6
Ñ2Ð˜/¨7ð ×+Ñ+÷
ò 
ð ×)Ñ)Ø×8Ñ8ð ,ó 
ð 	
r4   c                 ó   — y)z•Close resources owned by the HTTP client.

        Transports that own a connection pool or a session override it. The default does nothing.
        Nr.   r/   s    r1   rD   zHttpClient.closeâ  r3   r4   c               ó   — t        d«      ‚©aÈ  Send one request through the underlying HTTP library.

        Required by the inherited `call`, so a transport adapter must implement it. Overriding `call` itself
        bypasses it entirely. Let the HTTP library's exceptions propagate unwrapped: `call` classifies them
        through `is_retryable_transport_error` and `is_timeout_error`.

        Args:
            method: HTTP method (GET, POST, PUT, DELETE, etc.).
            url: Full request URL, with the query parameters already encoded into it.
            headers: Final request headers, with the client's default headers already merged in.
            content: Request body, already serialized and compressed, or None for a request without a body.
            timeout: Timeout for this attempt in seconds, or None for no timeout at all.
            stream: Whether to return the response with the body unread, so the caller can stream it.

        Returns:
            The HTTP response object.
        zMImplement `send_request` to provide a transport, or override `call` entirely.©ÚNotImplementedError©r0   r  rÞ   r:   r8   rÅ   r  s          r1   Úsend_requestzHttpClient.send_requestè  s   € ô6 "Ð"qÓrÐrr4   é   éô  ©r¤   r¾   r¿   ©r]   r  Úbackoff_factorÚrandom_factorc               óœ  ‡
— |dk  rt        d|› �«      ‚t        t        d|«      d«      }t        t        d|«      d«      }dŠ
dˆ
fd„}t        d|dz   «      D ]  }	  | ||«      c S   | ||dz   «      S # t        $ r ‰
s‚ Y nw xY wt        j                  dd|z   «      }t        |«      }||dz
  z  }	t        j                  ||z  |	z  «       Œv)z5Retry a function with exponential backoff and jitter.r¿   ú$max_retries must be at least 1, got r   é
   Tc                 ó
   •— dŠ y ©NFr.   ©Úswallows   €r1   rè   z9HttpClient._retry_with_exp_backoff.<locals>.stop_retrying  ó   ø€ à‰Gr4   rO   )
rÏ   rÄ   ÚmaxÚrangerô   ÚrandomÚuniformr   ÚtimeÚsleep©Úfuncr]   r  r%  r&  rè   rÆ   Úrandom_sleep_factorÚbackoff_base_secsÚbackoff_exp_factorr-  s             @r1   r  z"HttpClient._retry_with_exp_backoff  sù   ø€ ð ˜Š?ÜÐCÀKÀ=ÐQÓRÐRäœC  =Ó1°1Ó5ˆÜœS  NÓ3°RÓ8ˆØˆõ	ô ˜Q ¨a¡Ó0ò 
	UˆGðÙ˜M¨7Ó3Ò3ð
	Uñ �M ;°¡?Ó3Ð3øô ò ÙØñ ðúô #)§.¡.°°A¸Ñ4EÓ"FÐÜ *¨<Ó 8ÐØ!/°G¸a±KÑ!@ÐÜ�J‰JÐ*Ð->Ñ>ÐASÑSÕTs   ÁA4Á4BÂBc       	   
     óò  — t         j                  j                  |«       t        j	                  d«       | j
                  xj                  dz  c_        	 | j                  || j                  ||¬«      ||| j                  |	|¬«      |xs d¬«      }
| j                  |
||¬	«      r|
S 	 |
j                  «        t#        |
||¬«      ‚# t        $ r}| j                  ||¬«       ‚ d}~ww xY w# t        $ r�}t        j	                  d
|¬«       t        t        «      5  |
j                  «        ddd«       n# 1 sw Y   nxY w| j!                  |«      st        j	                  d|¬«        |«        ‚ d}~ww xY w)ú:Execute one request attempt through the transport adapter.úSending requestr¿   rÖ   ©rÆ   F©r  rÞ   r:   r8   rÅ   r  ©rè   N©rÆ   rè   ú!Reading the error response failedrã   rå   ©r  )r   rÆ   r  ræ   rç   rp   Úrequestsr   rá   rÉ   rô   ré   rñ   r>   r   rD   r�   r   ©r0   rè   rÆ   r  rÞ   r:   r¬   r8   r  rÅ   rð   r‹   s               r1   r  zHttpClient._make_request(  s`  € ô 	×Ñ×Ñ Ô(Ü�‰Ð&Ô'à×Ñ×!Ò! QÑ&Õ!ð	Ø×(Ñ(ØØ×/Ñ/°¸FÐ/ÓCØØØ×-Ñ-¨g¸wÐ-ÓGØ’ ð )ó ˆHð ×'Ñ'¨¸'ÐQ^Ð'Ô_ØˆOð		Ø�M‰MŒOô ˜H g°fÔ=Ð=øô) ò 	Ø×*Ñ*¨3¸mÐ*ÔLØûð	ûô ò 	Ü�L‰LÐ<ÀsˆLÔKÜœ)Ó$ñ !Ø—‘Ô ÷!÷ !ñ !úà×4Ñ4°SÔ9Ü—‘Ð9ÀC�ÔHÙ”Øûð	úsH   Á=C Â)C, Ã	C)ÃC$Ã$C)Ã,	E6Ã5&E1ÄD5Ä,	E1Ä5D>	Ä:7E1Å1E6©rK   r&   ©r  ztype[BaseException] | Noner  zBaseException | Noner  zTracebackType | NonerK   rP   ©r  rM   rÞ   rM   r:   ró   r¬   rö   rµ   r÷   r<   rø   r  úbool | NonerÅ   r)   rK   r,   rO   ©r  rM   rÞ   rM   r:   rõ   r8   úbytes | NonerÅ   zfloat | Noner  r§   rK   r,   )r6  z&Callable[[Callable[[], None], int], T]r]   rL   r  r   r%  Úfloatr&  rK  rK   r*   ©rè   rù   rÆ   rL   r  rM   rÞ   rM   r:   rõ   r¬   rö   r8   rJ  r  rH  rÅ   r)   rK   r,   )rQ   rR   rS   rT   rþ   r  r  rD   r   rú   r   r  r  r.   r4   r1   rü   rü   ‰  sÎ  „ ñóðà,ðð (ðð (ð	ð
 
óð *.Ø(,Ø/3Ø(,Ø"Ø#ñ;
ð ð;
ð ð	;
ð
 'ð;
ð &ð;
ð -ð;
ð &ð;
ð ð;
ð ð;
ð 
ó;
ózðsð ðsð ð	sð
  ðsð ðsð ðsð ðsð 
ósð: ð Ù"+¸Ô"=Ø !Ø ñ 4Ø4ð 4ð ð 4ð  ð	 4ð
 ð 4ð ð 4ð 
ò 4ó ð 4ðD0>ð *ð0>ð ð	0>ð
 ð0>ð ð0>ð  ð0>ð &ð0>ð ð0>ð ð0>ð ð0>ð 
ô0>r4   rü   c                  ó  — e Zd ZdZdd„Z	 	 	 	 	 	 	 	 dd„Zdddddddœ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zdd„Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	„Ze	d
 e
d¬«      dddœ	 	 	 	 	 	 	 	 	 	 	 dd„«       Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Zy)ÚHttpClientAsynczÈBase class for asynchronous HTTP clients used by `ApifyClientAsync`.

    Extend this class to create a custom asynchronous HTTP client. See `HttpClient`
    for details on the expected behavior.
    c              ƒ  ó   K  — | S ­w)z=Return this client and close it when the async context exits.r.   r/   s    r1   Ú
__aenter__zHttpClientAsync.__aenter__c  s   è ø€ àˆùs   ‚c              ƒ  ó@   K  — | j                  «       ƒ d{  –—†  y7 Œ­w)z6Close resources owned by the asynchronous HTTP client.N)rF   r   s       r1   Ú	__aexit__zHttpClientAsync.__aexit__g  s   è ø€ ð �k‰k‹m×Òús   ‚–—Nrº   r  c          
   ƒ  ó
  ‡ ‡‡‡‡‡	‡
‡K  — t         j                  j                  ‰«       t         j                  j                  ‰«       ‰ j                  xj
                  dz  c_        |€‰ j                  |«      r1t        j                  ‰ j                  ||||¬«      ƒ d{  –—† \  Š
ŠŠ	n‰ j                  ||||¬«      \  Š
ŠŠ	‰ j                  ˆ	ˆˆ
ˆˆ ˆˆˆfd„‰ j                  ‰ j                  ¬«      ƒ d{  –—† S 7 Œ\7 Œ­w)r  r¿   NrÊ   c                ó8   •— ‰j                  | |‰‰	‰‰‰‰‰¬«	      S r
  r  r  s
     €€€€€€€€r1   r  z&HttpClientAsync.call.<locals>.<lambda>«  r  r4   r  )r   r  r  rÞ   rp   r  r¶   ÚasyncioÚ	to_threadrÕ   r  rn   ro   r  s   ```    ``@@@r1   r  zHttpClientAsync.callp  s  ÿè ø€ ôB 	×Ñ×Ñ˜vÔ&Ü�‰×Ñ˜CÔ à×Ñ×Ò !Ñ#Õð Ð˜t×>Ñ>¸tÔDÜ?F×?PÑ?PØ×*Ñ*ØØØØô@÷ :Ñ6Ð˜o©wð :>×9SÑ9SØØØØð	 :Tó :Ñ6Ð˜o¨wð ×1Ñ1÷
ò 
ð ×)Ñ)Ø×8Ñ8ð 2ó 
÷ 
ð 	
ð:øð
ús%   ŠBDÂ"C?Â#ADÃ:DÃ;DÄDc              ƒ  ó   K  — y­w)z¢Close resources owned by the asynchronous HTTP client.

        Transports that own a connection pool or a session override it. The default does nothing.
        Nr.   r/   s    r1   rF   zHttpClientAsync.acloseº  rA   rB   c             ƒ  ó    K  — t        d«      ‚­wr  r  r  s          r1   r   zHttpClientAsync.send_requestÀ  s   è ø€ ô6 "Ð"qÓrÐrùs   ‚r!  r"  r#  r¾   r¿   r$  c             ƒ  óÞ  ‡
K  — |dk  rt        d|› �«      ‚t        t        d|«      d«      }t        t        d|«      d«      }dŠ
dˆ
fd„}t        d|dz   «      D ]  }	  | ||«      ƒ d{  –—† c S   | ||dz   «      ƒ d{  –—† S 7 Œ# t        $ r ‰
s‚ Y nw xY wt        j                  dd|z   «      }t        |«      }||dz
  z  }	t        j                  ||z  |	z  «      ƒ d{  –—†7   Œ‘7 Œi­w)	z<Retry an async function with exponential backoff and jitter.r¿   r(  r   r)  Tc                 ó
   •— dŠ y r+  r.   r,  s   €r1   rè   z>HttpClientAsync._retry_with_exp_backoff.<locals>.stop_retryingî  r.  r4   NrO   )
rÏ   rÄ   r/  r0  rô   r1  r2  r   rU  r4  r5  s             @r1   r  z'HttpClientAsync._retry_with_exp_backoffÝ  s  øè ø€ ð ˜Š?ÜÐCÀKÀ=ÐQÓRÐRäœC  =Ó1°1Ó5ˆÜœS  NÓ3°RÓ8ˆØˆõ	ô ˜Q ¨a¡Ó0ò 
	^ˆGðÙ! -°Ó9×9Ò9ð
	^ñ ˜-¨°q©Ó9×9Ð9ð :ùÜò ÙØñ ðúô #)§.¡.°°A¸Ñ4EÓ"FÐÜ *¨<Ó 8ÐØ!/°G¸a±KÑ!@ÐÜ—-‘-Ð 3Ð6GÑ GÐJ\Ñ \Ó]×]Ò]à9úsU   ƒAC-ÁBÁ*BÁ+BÁ.C-ÂC+ÂC-ÂBÂBÂC-ÂBÂAC-Ã$C'Ã%C-c       	   
   ƒ  ó4  K  — t         j                  j                  |«       t        j	                  d«       | j
                  xj                  dz  c_        	 | j                  || j                  ||¬«      ||| j                  |	|¬«      |xs d¬«      ƒ d{  –—† }
| j                  |
||¬	«      r|
S 	 |
j                  «       ƒ d{  –—†  t#        |
||¬«      ‚7 ŒA# t        $ r}| j                  ||¬«       ‚ d}~ww xY w7 Œ9# t        $ rŠ}t        j	                  d
|¬«       t        t        «      5  |
j                  «       ƒ d{  –—†7   ddd«       n# 1 sw Y   nxY w| j!                  |«      st        j	                  d|¬«        |«        ‚ d}~ww xY w­w)r;  r<  r¿   rÖ   r=  Fr>  Nr?  r@  rA  rã   rå   rB  )r   rÆ   r  ræ   rç   rp   rC  r   rá   rÉ   rô   ré   rñ   r@   r   rF   r�   r   rD  s               r1   r  zHttpClientAsync._make_request   s~  è ø€ ô 	×Ñ×Ñ Ô(Ü�‰Ð&Ô'à×Ñ×!Ò! QÑ&Õ!ð	Ø!×.Ñ.ØØ×/Ñ/°¸FÐ/ÓCØØØ×-Ñ-¨g¸wÐ-ÓGØ’ ð /ó ÷ ˆHð ×'Ñ'¨¸'ÐQ^Ð'Ô_ØˆOð		Ø—.‘.Ó"×"Ð"ô ˜H g°fÔ=Ð=ð9ùô ò 	Ø×*Ñ*¨3¸mÐ*ÔLØûð	úð #ùÜò 	Ü�L‰LÐ<ÀsˆLÔKÜœ)Ó$ñ (Ø—o‘oÓ'×'Ñ'÷(÷ (ñ (úà×4Ñ4°SÔ9Ü—‘Ð9ÀC�ÔHÙ”Øûð	üs—   ‚AFÁA C ÂCÂC ÂFÂ3D ÃD ÃD ÃFÃC Ã	C=Ã$C8Ã8C=Ã=FÄ D Ä	FÄ&FÄ1EÅEÅEÅ	FÅE	Å7FÆFÆFrE  rF  rG  rO   rI  )r6  z1Callable[[Callable[[], None], int], Awaitable[T]]r]   rL   r  r   r%  rK  r&  rK  rK   r*   rL  )rQ   rR   rS   rT   rP  rR  r  rF   r   rú   r   r  r  r.   r4   r1   rN  rN  [  sØ  „ ñóðà,ðð (ðð (ð	ð
 
óð *.Ø(,Ø/3Ø(,Ø"Ø#ñH
ð ðH
ð ð	H
ð
 'ðH
ð &ðH
ð -ðH
ð &ðH
ð ðH
ð ðH
ð 
óH
óTðsð ðsð ð	sð
  ðsð ðsð ðsð ðsð 
ósð: ð Ù"+¸Ô"=Ø !Ø ñ :Ø?ð :ð ð :ð  ð	 :ð
 ð :ð ð :ð 
ò :ó ð :ðD0>ð *ð0>ð ð	0>ð
 ð0>ð ð0>ð  ð0>ð &ð0>ð ð0>ð ð0>ð ð0>ð 
ô0>r4   rN  )JÚ
__future__r   rU  r<   rÐ   rÂ   rq   r1  rv   r3  Ú
contextlibr   r   r   r   Úhttpr   Ú	importlibr	   Útypingr
   r   r   Úurllib.parser   Útyping_extensionsr   r   Úapify_client._constsr   r   r   r   r   r   r   Úapify_client._docsr   Úapify_client._loggingr   r   r   Úapify_client._statisticsr   Úapify_client._utils.httpr   Úapify_client._utils.timer   Úapify_client.errorsr   Ú#apify_client.http_compressors._gzipr   Úcollections.abcr    r!   r"   r#   r$   Útypesr%   r&   Ú#apify_client.http_compressors._baser'   Úapify_client.typesr(   r)   Ú	getLoggerræ   rÀ   r*   r,   rW   rü   rN  r.   r4   r1   ú<module>rp     s2  ðÝ "ã Û Û Û 	Û Û 
Û Ý ß -Ñ -Ý Ý ß .Ñ .Ý "÷
 :÷÷ ñ õ *ß FÑ FÝ 5Ý AÝ /Ý -Ý BáßUÕUÝ#ÝåBß<à	ˆ×	Ñ	˜;Ó	'€Ù˜Ó €áˆCƒL€ñ ˆNÓØô+M�8ó +Mó ó ð+Mñ\ ˆNÓ÷`ð `ó ð`ñF	 ˆNÓôN>�ó N>ó ðN>ñb ˆNÓôT>�nó T>ó ñT>r4   