a
    ~g                     @  sx  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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mZmZmZmZmZ dd	lmZ dd
lmZmZmZmZ ddl m!Z!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, ddl-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z<m=Z= ddl>m?Z?m@Z@ ejArld dlBZBg dZCejDdddZEejDdddZFddddddZGddddd ZHdddd!d"d#ZIG d$d% d%ZJeJ ZKeLd&ZMd'e ZNd(Od)d* eP D ZQG d+d, d,ejRZSG d-d. d.e9ZTG d/d0 d0e.ZUejVd1ejWf ZXG d2d3 d3ZYG d4d deYZZG d5d deYZ[dS )6    )annotationsN)asynccontextmanagercontextmanager)TracebackType   )__version__)Auth	BasicAuthFunctionAuth)DEFAULT_LIMITSDEFAULT_MAX_REDIRECTSDEFAULT_TIMEOUT_CONFIGLimitsProxyTimeout)SUPPORTED_DECODERS)
InvalidURLRemoteProtocolErrorTooManyRedirectsrequest_context)CookiesHeadersRequestResponse)codes)AsyncBaseTransportBaseTransport)AsyncHTTPTransportHTTPTransport)AsyncByteStream	AuthTypes	CertTypesCookieTypesHeaderTypes
ProxyTypesQueryParamTypesRequestContentRequestDataRequestExtensionsRequestFilesSyncByteStreamTimeoutTypes)URLQueryParams)
URLPatternget_environment_proxies)USE_CLIENT_DEFAULTAsyncClientClientTr2   )boundUr1   r,   bool)urllocationreturnc                 C  s<   | j |j krdS | jdko:t| dko:|jdko:t|dkS )zA
    Return 'True' if 'location' is a HTTPS upgrade of 'url'
    FhttpP   https  )hostscheme_port_or_default)r7   r8    rA   R/var/www/html/emsaiapi.evdpl.com/venv/lib/python3.9/site-packages/httpx/_client.py_is_https_redirect>   s    


rC   z
int | Noner7   r9   c                 C  s"   | j d ur| j S ddd| jS )Nr;   r=   )r:   r<   )portgetr?   r7   rA   rA   rB   r@   M   s    
r@   )r7   otherr9   c                 C  s(   | j |j ko&| j|jko&t| t|kS )z@
    Return 'True' if the given URLs share the same origin.
    )r?   r>   r@   )r7   rH   rA   rA   rB   _same_originS   s
    
rI   c                   @  s   e Zd ZdZdS )UseClientDefaulta<  
    For some parameters such as `auth=...` and `timeout=...` we need to be able
    to indicate the default "unset" state, in a way that is distinctly different
    to using `None`.

    The default "unset" state indicates that whatever default is set on the
    client should be used. This is different to setting `None`, which
    explicitly disables the parameter, possibly overriding a client default.

    For example we use `timeout=USE_CLIENT_DEFAULT` in the `request()` signature.
    Omitting the `timeout` parameter will send a request using whatever default
    timeout has been configured on the client. Including `timeout=None` will
    ensure no timeout is used.

    Note that user code shouldn't need to use the `USE_CLIENT_DEFAULT` constant,
    but it is used internally when a parameter is not included.
    N)__name__
__module____qualname____doc__rA   rA   rA   rB   rJ   ^   s   rJ   httpxzpython-httpx/z, c                 C  s   g | ]}|d kr|qS )identityrA   ).0keyrA   rA   rB   
<listcomp>y       rS   c                   @  s   e Zd ZdZdZdZdS )ClientStater         N)rK   rL   rM   UNOPENEDOPENEDCLOSEDrA   rA   rA   rB   rU   }   s   rU   c                   @  s@   e Zd ZdZdddddddZd	d
ddZdd
ddZdS )BoundSyncStreamz
    A byte stream that is bound to a given response instance, and that
    ensures the `response.elapsed` is set once the response is closed.
    r*   r   floatNonestreamresponsestartr9   c                 C  s   || _ || _|| _d S N_stream	_response_startselfr_   r`   ra   rA   rA   rB   __init__   s    zBoundSyncStream.__init__ztyping.Iterator[bytes]r9   c                 c  s   | j D ]
}|V  qd S rb   rd   rh   chunkrA   rA   rB   __iter__   s    
zBoundSyncStream.__iter__c                 C  s,   t  | j }tj|d| j_| j  d S N)seconds)	timeperf_counterrf   datetime	timedeltare   elapsedrd   closerh   ru   rA   rA   rB   rv      s    zBoundSyncStream.closeN)rK   rL   rM   rN   ri   rn   rv   rA   rA   rA   rB   r[      s   r[   c                   @  s@   e Zd ZdZdddddddZd	d
ddZdd
ddZdS )BoundAsyncStreamz
    An async byte stream that is bound to a given response instance, and that
    ensures the `response.elapsed` is set once the response is closed.
    r   r   r\   r]   r^   c                 C  s   || _ || _|| _d S rb   rc   rg   rA   rA   rB   ri      s    zBoundAsyncStream.__init__ztyping.AsyncIterator[bytes]rj   c                 C s    | j 2 z3 d H W }|V  q6 d S rb   rk   rl   rA   rA   rB   	__aiter__   s    zBoundAsyncStream.__aiter__c                   s2   t  | j }tj|d| j_| j I d H  d S ro   )	rq   rr   rf   rs   rt   re   ru   rd   acloserw   rA   rA   rB   rz      s    zBoundAsyncStream.acloseN)rK   rL   rM   rN   ri   ry   rz   rA   rA   rA   rB   rx      s   rx   .c                   @  s  e Zd Zddddededddddddd	d
dddddddddddZeddddZeddddZdddddZ	dddd d!d"Z
ed#dd$d%Zejddd&d'd%Zed(dd)d*Zejd(dd+d,d*Zed-dd.d/Zejd0dd1d2d/Zeddd3d4Zejdddd5d4Zed6dd7d8Zejd9dd:d;d8Zed<dd=d>Zejd?dd@dAd>ZedBddCdDZejdEddFdGdDZdddddddeddH	dIddJdKdLdMdd	d
dNdOdPdQdRdSZddddTdUZdud
d
d@dVdWZdvd	d	d:dXdYZdwdddFdZd[Zdd-d1d\d]ZefdPd^d_d`dadbZdPdcdPdddedfZdPdcdIdddgdhZdPdcddddidjZdPddId6dkdldmZdPdIdndodpdqZdPddrdsdtZ dS )x
BaseClientNF Tutf-8authparamsheaderscookiestimeoutfollow_redirectsmax_redirectsevent_hooksbase_url	trust_envdefault_encodingAuthTypes | NoneQueryParamTypes | NoneHeaderTypes | NoneCookieTypes | Noner+   r6   int+None | typing.Mapping[str, list[EventHook]]	URL | str#str | typing.Callable[[bytes], str]r]   )r   r   r   r   r   r   r   r   r   r   r   r9   c                C  s   |d u ri n|}|  t|	| _| || _t|| _t|| _t	|| _
t|| _|| _|| _t|dg t|dg d| _|
| _|| _tj| _d S Nrequestr`   )r   r`   )_enforce_trailing_slashr,   	_base_url_build_auth_authr-   _paramsr   r   r   _cookiesr   _timeoutr   r   listrF   _event_hooks
_trust_env_default_encodingrU   rX   _state)rh   r   r   r   r   r   r   r   r   r   r   r   rA   rA   rB   ri      s    



zBaseClient.__init__rj   c                 C  s   | j tjkS )z2
        Check if the client being closed
        )r   rU   rZ   rh   rA   rA   rB   	is_closed   s    zBaseClient.is_closedc                 C  s   | j S rb   )r   r   rA   rA   rB   r      s    zBaseClient.trust_envr,   rD   c                 C  s"   |j dr|S |j|j d dS )N   /raw_path)r   endswith	copy_withrh   r7   rA   rA   rB   r      s    z"BaseClient._enforce_trailing_slashProxyTypes | Nonezdict[str, Proxy | None])proxyallow_env_proxiesr9   c                 C  sL   |d u r$|r dd t   D S i S t|ttfr<t|dn|}d|iS d S )Nc                 S  s(   i | ] \}}||d u rd nt |dqS )NrG   )r   )rQ   rR   r7   rA   rA   rB   
<dictcomp>   s   z-BaseClient._get_proxy_map.<locals>.<dictcomp>rG   zall://)r/   items
isinstancestrr,   r   )rh   r   r   rA   rA   rB   _get_proxy_map   s    zBaseClient._get_proxy_mapr   c                 C  s   | j S rb   )r   r   rA   rA   rB   r      s    zBaseClient.timeout)r   r9   c                 C  s   t || _d S rb   )r   r   )rh   r   rA   rA   rB   r     s    zdict[str, list[EventHook]]c                 C  s   | j S rb   )r   r   rA   rA   rB   r     s    zBaseClient.event_hooks)r   r9   c                 C  s(   t |dg t |dg d| _d S r   )r   rF   r   )rh   r   rA   rA   rB   r   	  s    zAuth | Nonec                 C  s   | j S )z
        Authentication class used when none is passed at the request-level.

        See also [Authentication][0].

        [0]: /quickstart/#authentication
        )r   r   rA   rA   rB   r     s    	zBaseClient.authr    )r   r9   c                 C  s   |  || _d S rb   )r   r   rh   r   rA   rA   rB   r     s    c                 C  s   | j S )zK
        Base URL to use when sending requests with relative URLs.
        )r   r   rA   rA   rB   r     s    zBaseClient.base_urlc                 C  s   |  t|| _d S rb   )r   r,   r   r   rA   rA   rB   r   &  s    r   c                 C  s   | j S )z@
        HTTP headers to include when sending requests.
        )_headersr   rA   rA   rB   r   *  s    zBaseClient.headersr#   )r   r9   c                 C  s2   t dtddtdd}|| || _d S )Ns   */*asciis
   keep-alive)s   Accepts   Accept-Encodings
   Connections
   User-Agent)r   ACCEPT_ENCODINGencode
USER_AGENTupdater   )rh   r   Zclient_headersrA   rA   rB   r   1  s    
r   c                 C  s   | j S )zA
        Cookie values to include when sending requests.
        )r   r   rA   rA   rB   r   >  s    zBaseClient.cookiesr"   )r   r9   c                 C  s   t || _d S rb   )r   r   )rh   r   rA   rA   rB   r   E  s    r-   c                 C  s   | j S )zO
        Query parameters to include in the URL when sending requests.
        )r   r   rA   rA   rB   r   I  s    zBaseClient.paramsr%   )r   r9   c                 C  s   t || _d S rb   )r-   r   )rh   r   rA   rA   rB   r   P  s    )	contentdatafilesjsonr   r   r   r   
extensionsr   RequestContent | NoneRequestData | NoneRequestFiles | Nonetyping.Any | NoneTimeoutTypes | UseClientDefaultRequestExtensions | Noner   )methodr7   r   r   r   r   r   r   r   r   r   r9   c       	         C  s   |  |}| |}| |	}	| |}|du r4i n|}d|vrrt|
trP| jnt|
}
tf i |d|
	 i}t
|||||||||	|d
S )aR  
        Build and return a request instance.

        * The `params`, `headers` and `cookies` arguments
        are merged with any values set on the client.
        * The `url` argument is merged with any `base_url` set on the client.

        See also: [Request instances][0]

        [0]: /advanced/clients/#request-instances
        Nr   )r   r   r   r   r   r   r   r   )
_merge_url_merge_headers_merge_cookies_merge_queryparamsr   rJ   r   r   dictas_dictr   )rh   r   r7   r   r   r   r   r   r   r   r   r   rA   rA   rB   build_requestT  s.    



zBaseClient.build_requestc                 C  s4   t |}|jr0| jj|jd }| jj|dS |S )z
        Merge a URL argument together with any 'base_url' on the client,
        to create the URL used for the outgoing request.
        r   r   )r,   is_relative_urlr   r   lstripr   )rh   r7   Z	merge_urlZmerge_raw_pathrA   rA   rB   r     s
    zBaseClient._merge_urlc                 C  s&   |s
| j r"t| j }|| |S |S )z
        Merge a cookies argument together with any cookies on the client,
        to create the cookies used for the outgoing request.
        )r   r   r   )rh   r   Zmerged_cookiesrA   rA   rB   r     s
    


zBaseClient._merge_cookiesc                 C  s   t | j}|| |S )z
        Merge a headers argument together with any headers on the client,
        to create the headers used for the outgoing request.
        )r   r   r   )rh   r   Zmerged_headersrA   rA   rB   r     s    

zBaseClient._merge_headersc                 C  s"   |s
| j rt| j }||S |S )z
        Merge a queryparams argument together with any queryparams on the client,
        to create the queryparams used for the outgoing request.
        )r   r-   merge)rh   r   Zmerged_queryparamsrA   rA   rB   r     s    


zBaseClient._merge_queryparamsc                 C  s\   |d u rd S t |tr*t|d |d dS t |tr8|S t|rJt|dS td|d S )Nr   r   usernamepassword)funczInvalid "auth" argument: )r   tupler	   r   callabler
   	TypeErrorr   rA   rA   rB   r     s    


zBaseClient._build_auth#AuthTypes | UseClientDefault | Noner   )r   r   r9   c                 C  sR   t |tr| jn| |}|d ur&|S |jj|jj }}|s@|rLt||dS t S )Nr   )	r   rJ   r   r   r7   r   r   r	   r   )rh   r   r   r   r   rA   rA   rB   _build_request_auth  s    zBaseClient._build_request_authr   )r   r`   r9   c                 C  sR   |  ||}| ||}| |||}| ||}t| j}t||||||jdS )z
        Given a request and a redirect response, return a new request that
        should be used to effect the redirect.
        )r   r7   r   r   r_   r   )_redirect_method_redirect_url_redirect_headers_redirect_streamr   r   r   r   )rh   r   r`   r   r7   r   r_   r   rA   rA   rB   _build_redirect_request  s    
z"BaseClient._build_redirect_requestc                 C  sR   |j }|jtjkr|dkrd}|jtjkr6|dkr6d}|jtjkrN|dkrNd}|S )z
        When being redirected we may want to change the method of the request
        based on certain specs or browser behavior.
        HEADGETPOST)r   status_coder   	SEE_OTHERFOUNDMOVED_PERMANENTLY)rh   r   r`   r   rA   rA   rB   r     s    zBaseClient._redirect_methodc              
   C  s   |j d }zt|}W n: tyP } z"td| d|ddW Y d}~n
d}~0 0 |jrn|jsn|j|jjd}|jr|j	|}|jj
r|j
s|j|jj
d}|S )z<
        Return the URL for the redirect to follow.
        Locationz Invalid URL in location header: .r   N)r>   )fragment)r   r,   r   r   r?   r>   r   r7   r   joinr   )rh   r   r`   r8   r7   excrA   rA   rB   r     s     
zBaseClient._redirect_url)r   r7   r   r9   c                 C  sx   t |j}t||js>t|j|s.|dd |jd|d< ||jkrh|dkrh|dd |dd |dd |S )	zR
        Return the headers that should be used for the redirect request.
        AuthorizationNr   Hostr   zContent-LengthzTransfer-EncodingCookie)	r   r   rI   r7   rC   popnetlocdecoder   )rh   r   r7   r   r   rA   rA   rB   r   "  s    
zBaseClient._redirect_headersz'SyncByteStream | AsyncByteStream | None)r   r   r9   c                 C  s   ||j kr|dkrdS |jS )zO
        Return the body that should be used for the redirect request.
        r   N)r   r_   )rh   r   r   rA   rA   rB   r   =  s    zBaseClient._redirect_streamr   r9   c                 C  sH   d|j vrDt| jtr| jnt| j}tf i |j d| i|_ d S )Nr   )r   r   r   rJ   r   r   r   )rh   r   r   rA   rA   rB   _set_timeoutH  s    

zBaseClient._set_timeout)N)N)N)!rK   rL   rM   r   r   ri   propertyr   r   r   r   r   setterr   r   r   r   r   r   r0   r   r   r   r   r   r   r   r   r   r   r   r   r   rA   rA   rA   rB   r{      s   ("
(3
 r{   c                      sr  e Zd ZdZdddddddddddedeeddddddd	d
dddddddddddddddddd fddZdddddedfdddddddddddZdddddefd dddddddd!d"d#Z	d$dd%d&d'Z
dddddddeeedd(d)dd*d+d,d-d	d
dd.d/d0d1d2d3d4d5Zedddddddeeedd(d)dd*d+d,d-d	d
dd.d/d0d1d6d3d7d8Zdeed9d:dd.d/d2d;d<d=Zd:d>dd?d2d@dAdBZd:dd?d2dCdDdEZd:d2dFdGdHZdddeeeddIdd	d
dd.d/d0d1d2dJ	dKdLZdddeeeddIdd	d
ddMd/d0d1d2dJ	dNdOZdddeeeddIdd	d
ddMd/d0d1d2dJ	dPdQZdddddddeeedd(dd*d+d,d-d	d
ddMd/d0d1d2dRdSdTZdddddddeeedd(dd*d+d,d-d	d
ddMd/d0d1d2dRdUdVZdddddddeeedd(dd*d+d,d-d	d
ddMd/d0d1d2dRdWdXZdddeeeddIdd	d
ddMd/d0d1d2dJ	dYdZZdd[d\d]Zd^d^d_d`daZdhdbdcddddedfdgZ  ZS )ir2   av  
    An HTTP client, with connection pooling, HTTP/2, redirects, cookie persistence, etc.

    It can be shared between threads.

    Usage:

    ```python
    >>> client = httpx.Client()
    >>> response = client.get('https://example.org')
    ```

    **Parameters:**

    * **auth** - *(optional)* An authentication class to use when sending
    requests.
    * **params** - *(optional)* Query parameters to include in request URLs, as
    a string, dictionary, or sequence of two-tuples.
    * **headers** - *(optional)* Dictionary of HTTP headers to include when
    sending requests.
    * **cookies** - *(optional)* Dictionary of Cookie items to include when
    sending requests.
    * **verify** - *(optional)* Either `True` to use an SSL context with the
    default CA bundle, `False` to disable verification, or an instance of
    `ssl.SSLContext` to use a custom context.
    * **http2** - *(optional)* A boolean indicating if HTTP/2 support should be
    enabled. Defaults to `False`.
    * **proxy** - *(optional)* A proxy URL where all the traffic should be routed.
    * **timeout** - *(optional)* The timeout configuration to use when sending
    requests.
    * **limits** - *(optional)* The limits configuration to use.
    * **max_redirects** - *(optional)* The maximum number of redirect responses
    that should be followed.
    * **base_url** - *(optional)* A URL to use as the base when building
    request URLs.
    * **transport** - *(optional)* A transport class to use for sending requests
    over the network.
    * **trust_env** - *(optional)* Enables or disables usage of environment
    variables for configuration.
    * **default_encoding** - *(optional)* The default encoding to use for decoding
    response text, if no charset information is included in a response Content-Type
    header. Set to a callable for automatic character set detection. Default: "utf-8".
    NTFr|   r}   )r   r   r   r   verifycertr   http1http2r   mountsr   r   limitsr   r   r   	transportr   r   r   r   r   ssl.SSLContext | str | boolCertTypes | Noner6   r   z0None | typing.Mapping[str, BaseTransport | None]r+   r   r   r   r   zBaseTransport | Noner   r]   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r9   c                  s   t  j||||||||||d rPzdd l}W n tyN   tdd Y n0 oZ|d u }|
|}j |d_ fdd| D _|d urȈj	dd | D  t
tj _d S )Nr~   r   uUsing http2=True, but the 'h2' package is not installed. Make sure to install httpx using `pip install httpx[http2]`.r   r   r   r   r   r   r   c                   s:   i | ]2\}}t ||d u rd nj| dqS N)r   r   r   r   r   r   r.   _init_proxy_transportrQ   rR   r   r   r   r   r   rh   r   r   rA   rB   r     s   z#Client.__init__.<locals>.<dictcomp>c                 S  s   i | ]\}}t ||qS rA   r.   rQ   rR   r   rA   rA   rB   r     rT   superri   h2ImportErrorr   _init_transport
_transportr   _mountsr   r   sorted)rh   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   	proxy_map	__class__r  rB   ri     sR    	zClient.__init__r   r   r   r   r   r   r   r   r9   c                 C  s    |d ur|S t ||||||dS r   r   rh   r   r   r   r   r   r   r   rA   rA   rB   r    s    
zClient._init_transportr   r   r   r   r   r   r   r   r9   c              	   C  s   t |||||||dS N)r   r   r   r   r   r   r   r  rh   r   r   r   r   r   r   r   rA   rA   rB   r     s    
zClient._init_proxy_transportr,   rD   c                 C  s:   | j  D ](\}}||r
|du r*| jn|  S q
| jS z
        Returns the transport instance that should be used for a given URL.
        This will either be the standard connection pool, or a proxy.
        Nr
  r   matchesr	  rh   r7   patternr   rA   rA   rB   _transport_for_url  s    
zClient._transport_for_urlr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   bool | UseClientDefaultr   r   r   r   r7   r   r   r   r   r   r   r   r   r   r   r   r9   c                C  sL   |	durd}t j|tdd | j|||||||||	||d}| j||
|dS )a  
        Build and send a request.

        Equivalent to:

        ```python
        request = client.build_request(...)
        response = client.send(request, ...)
        ```

        See `Client.build_request()`, `Client.send()` and
        [Merging of configuration][0] for how the various parameters
        are merged with client-level configuration.

        [0]: /advanced/clients/#merging-of-configuration
        NSetting per-request cookies=<...> is being deprecated, because the expected behaviour on cookie persistence is ambiguous. Set cookies directly on the client instance instead.rV   
stacklevelr   r7   r   r   r   r   r   r   r   r   r   r   r   warningswarnDeprecationWarningr   sendrh   r   r7   r   r   r   r   r   r   r   r   r   r   r   messager   rA   rA   rB   r     s$    !zClient.requestztyping.Iterator[Response]c                c  sT   | j |||||||||	||d}| j||
|dd}z|V  W |  n
|  0 dS a  
        Alternative to `httpx.request()` that streams the response body
        instead of loading it into memory at once.

        **Parameters**: See `httpx.request`.

        See also: [Streaming Responses][0]

        [0]: /quickstart#streaming-responses
        r!  T)r   r   r   r_   N)r   r'  rv   rh   r   r7   r   r   r   r   r   r   r   r   r   r   r   r   r`   rA   rA   rB   r_   ;  s,    zClient.streamr_   r   r   r   r   r_   r   r   r9   c             
   C  s   | j tjkrtdtj| _ t|tr,| jn|}| | | 	||}| j
|||g d}z|sf|  |W S  ty } z|  |W Y d}~n
d}~0 0 dS )a  
        Send a request.

        The request is sent as-is, unmodified.

        Typically you'll want to build one with `Client.build_request()`
        so that any client-level configuration is merged into the request,
        but passing an explicit `httpx.Request()` is supported as well.

        See also: [Request instances][0]

        [0]: /advanced/clients/#request-instances
        5Cannot send a request, as the client has been closed.r   r   historyN)r   rU   rZ   RuntimeErrorrY   r   rJ   r   r   r   _send_handling_authreadBaseExceptionrv   rh   r   r_   r   r   r`   r   rA   rA   rB   r'  o  s,    
zClient.sendr   list[Response]r   r   r   r0  r9   c           	   
   C  s   | |}zt|}| j|||d}zVz||}W n" tyV   | Y W W |  S 0 t||_|  |}|	| W q t
y } z|  |W Y d }~qd }~0 0 qW |  n
|  0 d S N)r   r0  )Zsync_auth_flownext_send_handling_redirectsr'  StopIterationrv   r   r0  r3  appendr4  	rh   r   r   r   r0  Z	auth_flowr`   next_requestr   rA   rA   rB   r2    s.    


zClient._send_handling_authr   r   r0  r9   c              
   C  s   t || jkrtd|d| jd D ]}|| q$| |}zb| jd D ]}|| qHt||_|jsl|W S | ||}||g }|r|	  n||_
|W S W q  ty } z|  |W Y d }~q d }~0 0 q d S Nz#Exceeded maximum allowed redirects.r   r   r`   )lenr   r   r   _send_single_requestr   r0  has_redirect_locationr   r3  r>  r4  rv   rh   r   r   r0  hookr`   r   rA   rA   rB   r:    s,    






zClient._send_handling_redirectsr   c                 C  s   |  |j}t }t|jts(tdt|d |	|}W d   n1 sR0    Y  t|jtslJ ||_
t|j||d|_| j| | j|_td|j|j|j|j|j |S )L
        Sends a single request, without handling any redirections.
        z?Attempted to send an async request with a sync Client instance.r   Nr`   ra   HTTP Request: %s %s "%s %d %s")r  r7   rq   rr   r   r_   r*   r1  r   handle_requestr   r[   r   extract_cookiesr   r   loggerinfor   http_versionr   reason_phraserh   r   r   ra   r`   rA   rA   rB   rB    s0    (	zClient._send_single_requestr   r   r   r   r   r   r   	r7   r   r   r   r   r   r   r   r9   c          	      C  s   | j d||||||||d	S )U
        Send a `GET` request.

        **Parameters**: See `httpx.request`.
        r   rP  r   	rh   r7   r   r   r   r   r   r   r   rA   rA   rB   rF     s    z
Client.getAuthTypes | UseClientDefaultc          	      C  s   | j d||||||||d	S )Z
        Send an `OPTIONS` request.

        **Parameters**: See `httpx.request`.
        OPTIONSrP  r   rS  rA   rA   rB   options)  s    zClient.optionsc          	      C  s   | j d||||||||d	S )V
        Send a `HEAD` request.

        **Parameters**: See `httpx.request`.
        r   rP  r   rS  rA   rA   rB   headF  s    zClient.headr7   r   r   r   r   r   r   r   r   r   r   r   r9   c                C  s$   | j d|||||||||	|
||dS )V
        Send a `POST` request.

        **Parameters**: See `httpx.request`.
        r   r  r   rh   r7   r   r   r   r   r   r   r   r   r   r   r   rA   rA   rB   postc  s    zClient.postc                C  s$   | j d|||||||||	|
||dS )U
        Send a `PUT` request.

        **Parameters**: See `httpx.request`.
        PUTr  r   r\  rA   rA   rB   put  s    z
Client.putc                C  s$   | j d|||||||||	|
||dS )W
        Send a `PATCH` request.

        **Parameters**: See `httpx.request`.
        PATCHr  r   r\  rA   rA   rB   patch  s    zClient.patchc          	      C  s   | j d||||||||d	S )X
        Send a `DELETE` request.

        **Parameters**: See `httpx.request`.
        DELETErP  r   rS  rA   rA   rB   delete  s    zClient.deleterj   c                 C  sB   | j tjkr>tj| _ | j  | j D ]}|dur(|  q(dS z.
        Close transport and proxies.
        N)r   rU   rZ   r	  rv   r
  values)rh   r   rA   rA   rB   rv     s    
zClient.closer3   rh   r9   c                 C  s`   | j tjkr*tjdtjdi| j  }t|tj| _ | j  | j	 D ]}|d urF|  qF| S Nz-Cannot open a client instance more than once.z9Cannot reopen a client instance, once it has been closed.)
r   rU   rX   rY   rZ   r1  r	  	__enter__r
  rh  )rh   msgr   rA   rA   rB   rk    s    

zClient.__enter__type[BaseException] | NoneBaseException | NoneTracebackType | Noneexc_type	exc_value	tracebackr9   c                 C  sB   t j| _| j||| | j D ]}|d ur"|||| q"d S rb   )rU   rZ   r   r	  __exit__r
  rh  )rh   rq  rr  rs  r   rA   rA   rB   rt    s
    zClient.__exit__)NNN)rK   rL   rM   rN   r   r   r   ri   r  r   r  r0   r   r   r_   r'  r2  r:  rB  rF   rW  rY  r]  r`  rc  rf  rv   rk  rt  __classcell__rA   rA   r  rB   r2   R  s  /<Q,8.73"%'"!"!"!*)*)*)"   c                      sr  e Zd ZdZddddddddddedeedddddddd	d
dddddddddddddddddd fddZdddddedfdddddddddddZdddddefd dddddddd!d"d#Z	d$dd%d&d'Z
dddddddeeedd(d)dd*d+d,d-d	d
dd.d/d0d1d2d3d4d5Zedddddddeeedd(d)dd*d+d,d-d	d
dd.d/d0d1d6d3d7d8Zdeed9d:dd.d/d2d;d<d=Zd:d>dd?d2d@dAdBZd:dd?d2dCdDdEZd:d2dFdGdHZdddeeeddIdd	d
dd.d/d0d1d2dJ	dKdLZdddeeeddIdd	d
ddMd/d0d1d2dJ	dNdOZdddeeeddIdd	d
ddMd/d0d1d2dJ	dPdQZdddddddeeedd(dd*d+d,d-d	d
ddMd/d0d1d2dRdSdTZdddddddeeedd(dd*d+d,d-d	d
ddMd/d0d1d2dRdUdVZdddddddeeedd(dd*d+d,d-d	d
ddMd/d0d1d2dRdWdXZdddeeeddIdd	d
ddMd/d0d1d2dJ	dYdZZdd[d\d]Zd^d^d_d`daZdhdbdcddddedfdgZ  ZS )ir1   a  
    An asynchronous HTTP client, with connection pooling, HTTP/2, redirects,
    cookie persistence, etc.

    It can be shared between tasks.

    Usage:

    ```python
    >>> async with httpx.AsyncClient() as client:
    >>>     response = await client.get('https://example.org')
    ```

    **Parameters:**

    * **auth** - *(optional)* An authentication class to use when sending
    requests.
    * **params** - *(optional)* Query parameters to include in request URLs, as
    a string, dictionary, or sequence of two-tuples.
    * **headers** - *(optional)* Dictionary of HTTP headers to include when
    sending requests.
    * **cookies** - *(optional)* Dictionary of Cookie items to include when
    sending requests.
    * **verify** - *(optional)* Either `True` to use an SSL context with the
    default CA bundle, `False` to disable verification, or an instance of
    `ssl.SSLContext` to use a custom context.
    * **http2** - *(optional)* A boolean indicating if HTTP/2 support should be
    enabled. Defaults to `False`.
    * **proxy** - *(optional)* A proxy URL where all the traffic should be routed.
    * **timeout** - *(optional)* The timeout configuration to use when sending
    requests.
    * **limits** - *(optional)* The limits configuration to use.
    * **max_redirects** - *(optional)* The maximum number of redirect responses
    that should be followed.
    * **base_url** - *(optional)* A URL to use as the base when building
    request URLs.
    * **transport** - *(optional)* A transport class to use for sending requests
    over the network.
    * **trust_env** - *(optional)* Enables or disables usage of environment
    variables for configuration.
    * **default_encoding** - *(optional)* The default encoding to use for decoding
    response text, if no charset information is included in a response Content-Type
    header. Set to a callable for automatic character set detection. Default: "utf-8".
    NTFr|   r}   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r   z5None | typing.Mapping[str, AsyncBaseTransport | None]r+   r   r   r   r   zAsyncBaseTransport | Noner   r]   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r9   c                  s   t  j||||||||||d rPzdd l}W n tyN   tdd Y n0 oZ|d u }|	|}j |d_ fdd| D _|
d urȈj	dd |
 D  t
tj _d S )Nr~   r   r   r   c                   s:   i | ]2\}}t ||d u rd nj| dqS r   r   r   r  rA   rB   r     s   z(AsyncClient.__init__.<locals>.<dictcomp>c                 S  s   i | ]\}}t ||qS rA   r  r  rA   rA   rB   r     rT   r  )rh   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r  r  r  rB   ri   I  sR    
zAsyncClient.__init__r   r  c                 C  s    |d ur|S t ||||||dS r   r   r  rA   rA   rB   r    s    
zAsyncClient._init_transportr   r  c              	   C  s   t |||||||dS r  rv  r  rA   rA   rB   r     s    
z!AsyncClient._init_proxy_transportr,   rD   c                 C  s:   | j  D ](\}}||r
|du r*| jn|  S q
| jS r  r  r  rA   rA   rB   r    s    
zAsyncClient._transport_for_urlr  r   r   r   r   r   r   r  r   r   r   r  c                  sR   |	durd}t j|tdd | j|||||||||	||d}| j||
|dI dH S )a  
        Build and send a request.

        Equivalent to:

        ```python
        request = client.build_request(...)
        response = await client.send(request, ...)
        ```

        See `AsyncClient.build_request()`, `AsyncClient.send()`
        and [Merging of configuration][0] for how the various parameters
        are merged with client-level configuration.

        [0]: /advanced/clients/#merging-of-configuration
        Nr  rV   r  r!  r"  r#  r(  rA   rA   rB   r     s$    "zAsyncClient.requestztyping.AsyncIterator[Response]c                C sf   | j |||||||||	||d}| j||
|ddI dH }z|V  W | I dH  n| I dH  0 dS r*  )r   r'  rz   r+  rA   rA   rB   r_     s,    zAsyncClient.streamr,  r   r-  c             
     s   | j tjkrtdtj| _ t|tr,| jn|}| | | 	||}| j
|||g dI dH }z|sr| I dH  |W S  ty } z| I dH  |W Y d}~n
d}~0 0 dS )a  
        Send a request.

        The request is sent as-is, unmodified.

        Typically you'll want to build one with `AsyncClient.build_request()`
        so that any client-level configuration is merged into the request,
        but passing an explicit `httpx.Request()` is supported as well.

        See also: [Request instances][0]

        [0]: /advanced/clients/#request-instances
        r.  r/  N)r   rU   rZ   r1  rY   r   rJ   r   r   r   r2  areadr4  rz   r5  rA   rA   rB   r'  :  s,    
zAsyncClient.sendr   r6  r7  c           	   
     s   | |}z| I d H }| j|||dI d H }zhz||I d H }W n( tyn   | Y W W | I d H  S 0 t||_| I d H  |}|	| W q t
y } z| I d H  |W Y d }~qd }~0 0 qW | I d H  n| I d H  0 d S r8  )Zasync_auth_flow	__anext__r:  asendStopAsyncIterationrz   r   r0  rw  r<  r4  r=  rA   rA   rB   r2  m  s.    


zAsyncClient._send_handling_authr?  c              
     s   t || jkrtd|d| jd D ]}||I d H  q$| |I d H }zn| jd D ]}||I d H  qTt||_|js~|W S | ||}||g }|r|	 I d H  n||_
|W S W q  ty } z| I d H  |W Y d }~q d }~0 0 q d S r@  )rA  r   r   r   rB  r   r0  rC  r   rw  r>  r4  rz   rD  rA   rA   rB   r:    s,    


z$AsyncClient._send_handling_redirectsr   c                   s   |  |j}t }t|jts(tdt|d  |	|I dH }W d   n1 sX0    Y  t|jtsrJ ||_
t|j||d|_| j| | j|_td|j|j|j|j|j |S )rF  z?Attempted to send an sync request with an AsyncClient instance.r   NrG  rH  )r  r7   rq   rr   r   r_   r   r1  r   Zhandle_async_requestr   rx   r   rJ  r   r   rK  rL  r   rM  r   rN  rO  rA   rA   rB   rB    s0    .	z AsyncClient._send_single_requestrP  rQ  c          	        s"   | j d||||||||d	I dH S )rR  r   rP  Nr   rS  rA   rA   rB   rF     s    zAsyncClient.getrT  c          	        s"   | j d||||||||d	I dH S )rU  rV  rP  Nr   rS  rA   rA   rB   rW    s    zAsyncClient.optionsc          	        s"   | j d||||||||d	I dH S )rX  r   rP  Nr   rS  rA   rA   rB   rY    s    zAsyncClient.headrZ  c                  s*   | j d|||||||||	|
||dI dH S )r[  r   r  Nr   r\  rA   rA   rB   r]  .  s    zAsyncClient.postc                  s*   | j d|||||||||	|
||dI dH S )r^  r_  r  Nr   r\  rA   rA   rB   r`  S  s    zAsyncClient.putc                  s*   | j d|||||||||	|
||dI dH S )ra  rb  r  Nr   r\  rA   rA   rB   rc  x  s    zAsyncClient.patchc          	        s"   | j d||||||||d	I dH S )rd  re  rP  Nr   rS  rA   rA   rB   rf    s    zAsyncClient.deleterj   c                   sN   | j tjkrJtj| _ | j I dH  | j D ]}|dur.| I dH  q.dS rg  )r   rU   rZ   r	  rz   r
  rh  )rh   r   rA   rA   rB   rz     s    zAsyncClient.acloser5   ri  c                   sl   | j tjkr*tjdtjdi| j  }t|tj| _ | j I d H  | j	 D ]}|d urL| I d H  qL| S rj  )
r   rU   rX   rY   rZ   r1  r	  
__aenter__r
  rh  )rh   rl  r   rA   rA   rB   r{    s    zAsyncClient.__aenter__rm  rn  ro  rp  c                   sN   t j| _| j|||I d H  | j D ] }|d ur(||||I d H  q(d S rb   )rU   rZ   r   r	  	__aexit__r
  rh  )rh   rq  rr  rs  r   rA   rA   rB   r|    s
    zAsyncClient.__aexit__)NNN)rK   rL   rM   rN   r   r   r   ri   r  r   r  r0   r   r   r_   r'  r2  r:  rB  rF   rW  rY  r]  r`  rc  rf  rz   r{  r|  ru  rA   rA   r  rB   r1     s  0<Q,9.73"&&"!"!"!*)*)*)"   )\
__future__r   rs   enumloggingrq   typingr$  
contextlibr   r   typesr   r   r   r   r	   r
   _configr   r   r   r   r   r   Z	_decodersr   _exceptionsr   r   r   r   _modelsr   r   r   r   _status_codesr   Z_transports.baser   r   Z_transports.defaultr   r   _typesr   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   _urlsr,   r-   _utilsr.   r/   TYPE_CHECKINGssl__all__TypeVarr3   r5   rC   r@   rI   rJ   r0   	getLoggerrK  r   r   keysr   EnumrU   r[   rx   CallableAnyZ	EventHookr{   r2   r1   rA   rA   rA   rB   <module>   sd    <

        N