U
    {f                    @  sT  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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' ddl(m)Z)m*Z* ddl+m,Z,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@ ddlAmBZBmCZC ddlDmEZEmFZFmGZGmHZHmIZI ejJdddZKejJdddZLG dd dZMeM ZNeOdZPde ZQdRdd eS D ZTG d d! d!ejUZVG d"d# d#e=ZWG d$d% d%e1ZXejYd&ejZf Z[G d'd( d(Z\G d)d de\Z]G d*d de\Z^dS )+    )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)ASGITransport)AsyncBaseTransportBaseTransport)AsyncHTTPTransportHTTPTransport)WSGITransport)AsyncByteStream	AuthTypes	CertTypesCookieTypesHeaderTypesProxiesTypes
ProxyTypesQueryParamTypesRequestContentRequestDataRequestExtensionsRequestFilesSyncByteStreamTimeoutTypesURLTypesVerifyTypes)URLQueryParams)Timer
URLPatternget_environment_proxiesis_https_redirectsame_originTClient)boundUAsyncClientc                   @  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__ rB   rB   L/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/httpx/_client.pyr=   C   s   r=   Zhttpxzpython-httpx/z, c                 C  s   g | ]}|d kr|qS )identityrB   ).0keyrB   rB   rC   
<listcomp>^   s      rG   c                   @  s   e Zd ZdZdZdZdS )ClientStater         N)r>   r?   r@   UNOPENEDOPENEDCLOSEDrB   rB   rB   rC   rH   b   s   rH   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   r3   Nonestreamresponsetimerreturnc                 C  s   || _ || _|| _d S N_stream	_response_timerselfrQ   rR   rS   rB   rB   rC   __init__v   s    zBoundSyncStream.__init__ztyping.Iterator[bytes]rT   c                 c  s   | j D ]
}|V  qd S rU   rW   r[   chunkrB   rB   rC   __iter__}   s    
zBoundSyncStream.__iter__c                 C  s(   | j  }tj|d| j_| j  d S N)seconds)rY   Zsync_elapseddatetime	timedeltarX   elapsedrW   closer[   rc   rB   rB   rC   rg      s    
zBoundSyncStream.closeN)r>   r?   r@   rA   r\   ra   rg   rB   rB   rB   rC   rN   p   s   rN   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   r3   rO   rP   c                 C  s   || _ || _|| _d S rU   rV   rZ   rB   rB   rC   r\      s    zBoundAsyncStream.__init__ztyping.AsyncIterator[bytes]r]   c                 C s    | j 2 z3 d H W }|V  q6 d S rU   r^   r_   rB   rB   rC   	__aiter__   s    zBoundAsyncStream.__aiter__c                   s4   | j  I d H }tj|d| j_| j I d H  d S rb   )rY   Zasync_elapsedrd   re   rX   rf   rW   acloserh   rB   rB   rC   rk      s    zBoundAsyncStream.acloseN)r>   r?   r@   rA   r\   rj   rk   rB   rB   rB   rC   ri      s   ri   .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drd
d
d@dVdWZdsd	d	d:dXdYZdtd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dS )u
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.   boolint+None | typing.Mapping[str, list[EventHook]]r/   #str | typing.Callable[[bytes], str]rO   )rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   rT   c                C  s   |d kri n|}|  t|	| _| || _t|| _t|| _t	|| _
t|| _|| _|| _t|dg t|dg d| _|
| _|| _tj| _d S NrequestrR   )r   rR   )_enforce_trailing_slashr1   	_base_url_build_auth_authr2   _paramsr   rr   r   _cookiesr   _timeoutru   rv   listget_event_hooks
_trust_env_default_encodingrH   rK   _state)r[   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   rz   rB   rB   rC   r\      s    



zBaseClient.__init__r]   c                 C  s   | j tjkS )z2
        Check if the client being closed
        )r   rH   rM   r[   rB   rB   rC   	is_closed   s    zBaseClient.is_closedc                 C  s   | j S rU   )r   r   rB   rB   rC   ry      s    zBaseClient.trust_envr1   urlrT   c                 C  s"   |j dr|S |j|j d dS )N   /raw_path)r   endswith	copy_withr[   r   rB   rB   rC   r      s    z"BaseClient._enforce_trailing_slashProxiesTypes | Nonezdict[str, Proxy | None])proxiesallow_env_proxiesrT   c                 C  s   |d kr$|r dd t   D S i S t|trpi }| D ]0\}}t|ttfrZt|dn|}||t|< q:|S t|ttfrt|dn|}d|iS d S )Nc                 S  s(   i | ] \}}||d krd nt |dqS )Nr   )r   )rE   rF   r   rB   rB   rC   
<dictcomp>   s    z-BaseClient._get_proxy_map.<locals>.<dictcomp>r   zall://)r5   items
isinstancedictstrr1   r   )r[   r   r   new_proxiesrF   valueproxyrB   rB   rC   _get_proxy_map   s    
zBaseClient._get_proxy_mapr   c                 C  s   | j S rU   )r   r   rB   rB   rC   rt      s    zBaseClient.timeout)rt   rT   c                 C  s   t || _d S rU   )r   r   )r[   rt   rB   rB   rC   rt      s    zdict[str, list[EventHook]]c                 C  s   | j S rU   )r   r   rB   rB   rC   rw      s    zBaseClient.event_hooks)rw   rT   c                 C  s(   t |dg t |dg d| _d S r   )r   r   r   )r[   rw   rB   rB   rC   rw      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   rB   rB   rC   rp      s    	zBaseClient.authr"   )rp   rT   c                 C  s   |  || _d S rU   )r   r   r[   rp   rB   rB   rC   rp     s    c                 C  s   | j S )zK
        Base URL to use when sending requests with relative URLs.
        )r   r   rB   rB   rC   rx   
  s    zBaseClient.base_urlc                 C  s   |  t|| _d S rU   )r   r1   r   r   rB   rB   rC   rx     s    r   c                 C  s   | j S )z@
        HTTP headers to include when sending requests.
        )_headersr   rB   rB   rC   rr     s    zBaseClient.headersr%   )rr   rT   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   )r[   rr   Zclient_headersrB   rB   rC   rr     s    
r   c                 C  s   | j S )zA
        Cookie values to include when sending requests.
        )r   r   rB   rB   rC   rs   )  s    zBaseClient.cookiesr$   )rs   rT   c                 C  s   t || _d S rU   )r   r   )r[   rs   rB   rB   rC   rs   0  s    r2   c                 C  s   | j S )zO
        Query parameters to include in the URL when sending requests.
        )r   r   rB   rB   rC   rq   4  s    zBaseClient.paramsr(   )rq   rT   c                 C  s   t || _d S rU   )r2   r   )r[   rq   rB   rB   rC   rq   ;  s    )	contentdatafilesjsonrq   rr   rs   rt   
extensionsr   RequestContent | NoneRequestData | NoneRequestFiles | Nonetyping.Any | NoneTimeoutTypes | UseClientDefaultRequestExtensions | Noner   )methodr   r   r   r   r   rq   rr   rs   rt   r   rT   c       	         C  s   |  |}| |}| |	}	| |}|dkr4i n|}d|krnt|
trP| jnt|
}
tf |d|
	 i}t
|||||||||	|d
S )aJ  
        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/#request-instances
        Nrt   )r   r   r   r   rq   rr   rs   r   )
_merge_url_merge_headers_merge_cookies_merge_queryparamsr   r=   rt   r   r   as_dictr   )r[   r   r   r   r   r   r   rq   rr   rs   rt   r   rB   rB   rC   build_request?  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   )r1   is_relative_urlrx   r   lstripr   )r[   r   Z	merge_urlZmerge_raw_pathrB   rB   rC   r   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.
        )rs   r   r   )r[   rs   merged_cookiesrB   rB   rC   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   rr   r   )r[   rr   Zmerged_headersrB   rB   rC   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.
        )rq   r2   merge)r[   rq   Zmerged_queryparamsrB   rB   rC   r     s    


zBaseClient._merge_queryparamsc                 C  s\   |d k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   rB   rB   rC   r     s    


zBaseClient._build_auth#AuthTypes | UseClientDefault | Noner   )r   rp   rT   c                 C  sR   t |tr| jn| |}|d k	r&|S |jj|jj }}|s@|rLt||dS t S )Nr   )	r   r=   r   r   r   r   r   r	   r   )r[   r   rp   r   r   rB   rB   rC   _build_request_auth  s    zBaseClient._build_request_authr   )r   rR   rT   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   r   rr   rs   rQ   r   )_redirect_method_redirect_url_redirect_headers_redirect_streamr   rs   r   r   )r[   r   rR   r   r   rr   rQ   rs   rB   rB   rC   _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)r[   r   rR   r   rB   rB   rC   r     s    zBaseClient._redirect_methodc              
   C  s   |j d }zt|}W n8 tk
rN } ztd| d|ddW 5 d}~X Y nX |jrl|jsl|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.
        ZLocationz Invalid URL in location header: .r   N)host)fragment)rr   r1   r   r   schemer   r   r   r   joinr   )r[   r   rR   locationr   excrB   rB   rC   r     s"    

 zBaseClient._redirect_url)r   r   r   rT   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   rr   r7   r   r6   popnetlocdecoder   )r[   r   r   r   rr   rB   rB   rC   r     s    
zBaseClient._redirect_headersz'SyncByteStream | AsyncByteStream | None)r   r   rT   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   rQ   )r[   r   r   rB   rB   rC   r   (  s    zBaseClient._redirect_stream)N)N)N) r>   r?   r@   r   r   r\   propertyr   ry   r   r   rt   setterrw   rp   rx   rr   rs   rq   USE_CLIENT_DEFAULTr   r   r   r   r   r   r   r   r   r   r   r   rB   rB   rB   rC   rl      s   ("
(3
 rl   c                      s~  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dddd fddZddddedddfdddddddddd	d d!Zdddded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d0d1d2d3d4d5d6d7Zedddddddeeedd*d+dd,d-d.d/d	d
dd0d1d2d3d8d5d9d:Zdeed;d<dd0d1d4d=d>d?Zd<d@ddAd4dBdCdDZd<ddAd4dEdFdGZd<d4dHdIdJZdddeeeddKdd	d
ddLd1d2d3d4dM	dNdOZdddeeeddKdd	d
ddLd1d2d3d4dM	dPdQZdddeeeddKdd	d
ddLd1d2d3d4dM	dRdSZdddddddeeedd*dd,d-d.d/d	d
ddLd1d2d3d4dTdUdVZdddddddeeedd*dd,d-d.d/d	d
ddLd1d2d3d4dTdWdXZdddddddeeedd*dd,d-d.d/d	d
ddLd1d2d3d4dTdYdZZdddeeeddKdd	d
ddLd1d2d3d4dM	d[d\Zdd]d^d_Zd`d`dadbdcZdjdddedfddgdhdiZ  ZS )kr9   a	  
    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)* SSL certificates (a.k.a CA bundle) used to
    verify the identity of requested hosts. Either `True` (default CA bundle),
    a path to an SSL certificate file, an `ssl.SSLContext`, or `False`
    (which will disable verification).
    * **cert** - *(optional)* An SSL certificate used by the requested host
    to authenticate the client. Either a path to an SSL certificate file, or
    two-tuple of (certificate file, key file), or a three-tuple of (certificate
    file, key file, password).
    * **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.
    * **proxies** - *(optional)* A dictionary mapping proxy keys to proxy
    URLs.
    * **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.
    * **app** - *(optional)* An WSGI application to send requests to,
    rather than sending actual network requests.
    * **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".
    NTFrm   rn   rp   rq   rr   rs   verifycerthttp1http2r   r   mountsrt   ru   limitsrv   rw   rx   	transportappry   rz   r{   r|   r}   r~   r0   CertTypes | Noner   ProxyTypes | Noner   z0None | typing.Mapping[str, BaseTransport | None]r.   r   r   r   r/   zBaseTransport | None'typing.Callable[..., typing.Any] | Noner   rO   rp   rq   rr   rs   r   r   r   r   r   r   r   rt   ru   r   rv   rw   rx   r   r   ry   rz   rT   c                  s&  t  j||||||||||d rRzdd l}W n tk
rP   tdd Y nX |
rrd}t|t |	rrtd|rd}t|t o|d ko|d k}|
p|	|}j	 ||d_
 fdd	| D _|d k	rjd
d	 | D  ttj _d S )Nro   r   uUsing http2=True, but the 'h2' package is not installed. Make sure to install httpx using `pip install httpx[http2]`.JThe 'proxies' argument is now deprecated. Use 'proxy' or 'mounts' instead.*Use either `proxy` or 'proxies', not both.zhThe 'app' shortcut is now deprecated. Use the explicit style 'transport=WSGITransport(app=...)' instead.r   r   r   r   r   r   r   ry   c                   s:   i | ]2\}}t ||d krd nj| dqS Nr   r   r   r   r   ry   r4   _init_proxy_transportrE   rF   r   r   r   r   r   r[   ry   r   rB   rC   r     s   z#Client.__init__.<locals>.<dictcomp>c                 S  s   i | ]\}}t ||qS rB   r4   rE   rF   r   rB   rB   rC   r     s      superr\   h2ImportErrorwarningswarnDeprecationWarningRuntimeErrorr   _init_transport
_transportr   _mountsr   r   sortedr[   rp   rq   rr   rs   r   r   r   r   r   r   r   rt   ru   r   rv   rw   rx   r   r   ry   rz   r  messager   Z	proxy_map	__class__r  rC   r\   j  sh    

zClient.__init__r   	r   r   r   r   r   r   r   ry   rT   c	           	      C  s2   |d k	r|S |d k	rt |dS t||||||dS N)r   r   )r    r   	r[   r   r   r   r   r   r   r   ry   rB   rB   rC   r    s    
zClient._init_transportr   r   r   r   r   r   r   ry   rT   c              	   C  s   t |||||||dS N)r   r   r   r   r   ry   r   )r   r[   r   r   r   r   r   r   ry   rB   rB   rC   r     s    
zClient._init_proxy_transportr1   r   c                 C  s:   | j  D ](\}}||r
|dk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  r[   r   patternr   rB   rB   rC   _transport_for_url  s    
zClient._transport_for_urlr   r   r   r   rq   rr   rs   rp   ru   rt   r   r   r   r   r   r   r   bool | UseClientDefaultr   r   r   r   r   r   r   r   r   rq   rr   rs   rp   ru   rt   r   rT   c                C  sH   |	dk	rd}t |t | 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/#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.r   r   r   r   r   r   rq   rr   rs   rt   r   rp   ru   r	  r
  r  r   sendr[   r   r   r   r   r   r   rq   rr   rs   rp   ru   rt   r   r  r   rB   rB   rC   r     s$    !zClient.requestztyping.Iterator[Response]c                c  sL   | j |||||||||	||d}| j||
|dd}z
|V  W 5 |  X 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   rp   ru   rQ   N)r   r(  rg   r[   r   r   r   r   r   r   rq   rr   rs   rp   ru   rt   r   r   rR   rB   rB   rC   rQ   =  s,    
zClient.streamrQ   rp   ru   r   r   rQ   rp   ru   rT   c             
   C  s   | j tjkrtdtj| _ t|tr,| jn|}| ||}| j	|||g d}z|s\|
  |W S  tk
r } z|  |W 5 d}~X Y nX 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/#request-instances
        5Cannot send a request, as the client has been closed.rp   ru   historyN)r   rH   rM   r  rL   r   r=   ru   r   _send_handling_authreadBaseExceptionrg   r[   r   rQ   rp   ru   rR   r   rB   rB   rC   r(  q  s*    zClient.sendr   list[Response]r   rp   ru   r0  rT   c           	   
   C  s   | |}zt|}| j|||d}zRz||}W n tk
rR   | Y W W \S X t||_|  |}|	| W q t
k
r } z|  |W 5 d }~X Y qX qW 5 |  X d S N)ru   r0  )Zsync_auth_flowrg   next_send_handling_redirectsr(  StopIterationr   r0  r2  appendr3  	r[   r   rp   ru   r0  Z	auth_flowrR   next_requestr   rB   rB   rC   r1    s*    

zClient._send_handling_authr   ru   r0  rT   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k
r } z|  |W 5 d }~X Y q X q d S Nz#Exceeded maximum allowed redirects.r   r   rR   )lenrv   r   r   _send_single_requestr   r0  has_redirect_locationr   r2  r=  r3  rg   r[   r   ru   r0  hookrR   r   rB   rB   rC   r9    s.     






zClient._send_handling_redirectsr   rT   c              	   C  s   |  |j}t }|  t|jts.tdt|d |	|}W 5 Q R X t|jts^t
||_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   rR   rS   HTTP Request: %s %s "%s %d %s")r   r   r3   Z
sync_startr   rQ   r-   r  r   handle_requestAssertionErrorr   rN   rs   extract_cookiesr   rz   loggerinfor   http_versionr   reason_phraser[   r   r   rS   rR   rB   rB   rC   rA    s6      	zClient._send_single_requestrq   rr   rs   rp   ru   rt   r   AuthTypes | UseClientDefault	r   rq   rr   rs   rp   ru   rt   r   rT   c          	      C  s   | j d||||||||d	S )U
        Send a `GET` request.

        **Parameters**: See `httpx.request`.
        r   rQ  r   	r[   r   rq   rr   rs   rp   ru   rt   r   rB   rB   rC   r     s    z
Client.getc          	      C  s   | j d||||||||d	S )Z
        Send an `OPTIONS` request.

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

        **Parameters**: See `httpx.request`.
        r   rQ  r   rU  rB   rB   rC   headG  s    zClient.headr   r   r   r   r   rq   rr   rs   rp   ru   rt   r   rT   c                C  s$   | j d|||||||||	|
||dS )V
        Send a `POST` request.

        **Parameters**: See `httpx.request`.
        r   r!  r   r[   r   r   r   r   r   rq   rr   rs   rp   ru   rt   r   rB   rB   rC   postd  s    zClient.postc                C  s$   | j d|||||||||	|
||dS )U
        Send a `PUT` request.

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

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

        **Parameters**: See `httpx.request`.
        DELETErQ  r   rU  rB   rB   rC   delete  s    zClient.deleter]   c                 C  sB   | j tjkr>tj| _ | j  | j D ]}|dk	r(|  q(dS z.
        Close transport and proxies.
        N)r   rH   rM   r  rg   r  values)r[   r   rB   rB   rC   rg     s    
zClient.closer8   r[   rT   c                 C  s`   | j tjkr*tjdtjdi| j  }t|tj| _ | j  | j	 D ]}|d k	rF|  qF| S Nz-Cannot open a client instance more than once.z9Cannot reopen a client instance, once it has been closed.)
r   rH   rK   rL   rM   r  r  	__enter__r  ri  )r[   msgr   rB   rB   rC   rl    s     

zClient.__enter__type[BaseException] | NoneBaseException | NoneTracebackType | Noneexc_type	exc_value	tracebackrT   c                 C  sB   t j| _| j||| | j D ]}|d k	r"|||| q"d S rU   )rH   rM   r   r  __exit__r  ri  )r[   rr  rs  rt  r   rB   rB   rC   ru    s
    zClient.__exit__)NNN)r>   r?   r@   rA   r   r   r   r\   r  r   r   r   r   r   rQ   r(  r1  r9  rA  r   rX  rZ  r^  ra  rd  rg  rg   rl  ru  __classcell__rB   rB   r  rC   r9   4  s$  8@d ,8.71"%("!"!"!*)*)*)"   c                      s~  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dddd fddZddddedddfdddddddddd	d d!Zdddded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d0d1d2d3d4d5d6d7Zedddddddeeedd*d+dd,d-d.d/d	d
dd8d1d2d3d9d5d:d;Zdeed<d=dd0d1d4d>d?d@Zd=dAddBd4dCdDdEZd=ddBd4dFdGdHZd=d4dIdJdKZdddeeeddLdd	d
dd0d1d2d3d4dM	dNdOZdddeeeddLdd	d
dd8d1d2d3d4dM	dPdQZdddeeeddLdd	d
dd8d1d2d3d4dM	dRdSZdddddddeeedd*dd,d-d.d/d	d
dd8d1d2d3d4dTdUdVZdddddddeeedd*dd,d-d.d/d	d
dd8d1d2d3d4dTdWdXZdddddddeeedd*dd,d-d.d/d	d
dd8d1d2d3d4dTdYdZZdddeeeddLdd	d
dd8d1d2d3d4dM	d[d\Zdd]d^d_Zd`d`dadbdcZdjdddedfddgdhdiZ  ZS )kr<   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)* SSL certificates (a.k.a CA bundle) used to
    verify the identity of requested hosts. Either `True` (default CA bundle),
    a path to an SSL certificate file, an `ssl.SSLContext`, or `False`
    (which will disable verification).
    * **cert** - *(optional)* An SSL certificate used by the requested host
    to authenticate the client. Either a path to an SSL certificate file, or
    two-tuple of (certificate file, key file), or a three-tuple of (certificate
    file, key file, password).
    * **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.
    * **proxies** - *(optional)* A dictionary mapping HTTP protocols to proxy
    URLs.
    * **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.
    * **app** - *(optional)* An ASGI application to send requests to,
    rather than sending actual network requests.
    * **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".
    NTFrm   rn   r   r{   r|   r}   r~   r0   r   r   r   r   z5None | typing.Mapping[str, AsyncBaseTransport | None]r.   r   r   zBNone | typing.Mapping[str, list[typing.Callable[..., typing.Any]]]r/   zAsyncBaseTransport | Noner   r   rO   r   c                  s  t  j||||||||||d rRzdd l}W n tk
rP   tdd Y nX |
rrd}t|t |	rrtd|rd}t|t o|d k}|
p|	|}j	 ||d_
 fdd	| D _|d k	rjd
d	 | D  ttj _d S )Nro   r   r   r   r   zhThe 'app' shortcut is now deprecated. Use the explicit style 'transport=ASGITransport(app=...)' instead.r   c                   s:   i | ]2\}}t ||d krd nj| dqS r   r   r  r  rB   rC   r     s   z(AsyncClient.__init__.<locals>.<dictcomp>c                 S  s   i | ]\}}t ||qS rB   r  r  rB   rB   rC   r     s      r  r  r  r  rC   r\   S  sh    
zAsyncClient.__init__r   r  c	           	      C  s2   |d k	r|S |d k	rt |dS t||||||dS r  )r   r   r  rB   rB   rC   r    s    
zAsyncClient._init_transportr   r  c              	   C  s   t |||||||dS r  )r   r  rB   rB   rC   r     s    
z!AsyncClient._init_proxy_transportr1   r   c                 C  s:   | j  D ](\}}||r
|dkr*| jn|  S q
| jS r  r  r  rB   rB   rC   r     s    
zAsyncClient._transport_for_urlr!  r   r   r   r   r   r   r"  r   r   r   r#  c                  sN   |	dk	rd}t |t | 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/#merging-of-configuration
        Nr$  r%  r&  r'  r)  rB   rB   rC   r     s$    "zAsyncClient.requestrR  ztyping.AsyncIterator[Response]c                C sX   | j |||||||||	||d}| j||
|ddI dH }z
|V  W 5 | I dH  X dS r*  )r   r(  rk   r+  rB   rB   rC   rQ   (  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|sh|
 I dH  |W S  tk
r } z| I dH  |W 5 d}~X Y nX 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/#request-instances
        r.  r/  N)r   rH   rM   r  rL   r   r=   ru   r   r1  areadr3  rk   r4  rB   rB   rC   r(  \  s*    zAsyncClient.sendr   r5  r6  c           	   
     s   | |}z| I d H }| j|||dI d H }z^z||I d H }W n tk
rd   | Y W W hS X t||_| I d H  |}|	| W q t
k
r } z| I d H  |W 5 d }~X Y qX qW 5 | I d H  X d S r7  )Zasync_auth_flowrk   	__anext__r9  asendStopAsyncIterationr   r0  rw  r;  r3  r<  rB   rB   rC   r1    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k
r } z| I d H  |W 5 d }~X Y q X q d S r?  )r@  rv   r   r   rA  r   r0  rB  r   rw  r=  r3  rk   rC  rB   rB   rC   r9    s.     


z$AsyncClient._send_handling_redirectsrE  c              	     s   |  |j}t }| I dH  t|jts4tdt|d |	|I dH }W 5 Q R X t|jtsjt
||_t|j||d|_| j| | j|_td|j|j|j|j|j |S )rF  Nz?Attempted to send an sync request with an AsyncClient instance.r   rG  rH  )r   r   r3   Zasync_startr   rQ   r!   r  r   Zhandle_async_requestrJ  r   ri   rs   rK  r   rz   rL  rM  r   rN  r   rO  rP  rB   rB   rC   rA    s6      	z AsyncClient._send_single_requestrQ  rS  c          	        s"   | j d||||||||d	I dH S )rT  r   rQ  Nr   rU  rB   rB   rC   r     s    zAsyncClient.getc          	        s"   | j d||||||||d	I dH S )rV  rW  rQ  Nr   rU  rB   rB   rC   rX    s    zAsyncClient.optionsc          	        s"   | j d||||||||d	I dH S )rY  r   rQ  Nr   rU  rB   rB   rC   rZ  2  s    zAsyncClient.headr[  c                  s*   | j d|||||||||	|
||dI dH S )r\  r   r!  Nr   r]  rB   rB   rC   r^  O  s    zAsyncClient.postc                  s*   | j d|||||||||	|
||dI dH S )r_  r`  r!  Nr   r]  rB   rB   rC   ra  t  s    zAsyncClient.putc                  s*   | j d|||||||||	|
||dI dH S )rb  rc  r!  Nr   r]  rB   rB   rC   rd    s    zAsyncClient.patchc          	        s"   | j d||||||||d	I dH S )re  rf  rQ  Nr   rU  rB   rB   rC   rg    s    zAsyncClient.deleter]   c                   sN   | j tjkrJtj| _ | j I dH  | j D ]}|dk	r.| I dH  q.dS rh  )r   rH   rM   r  rk   r  ri  )r[   r   rB   rB   rC   rk     s    zAsyncClient.acloser;   rj  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 k	rL| I d H  qL| S rk  )
r   rH   rK   rL   rM   r  r  
__aenter__r  ri  )r[   rm  r   rB   rB   rC   r{    s     zAsyncClient.__aenter__rn  ro  rp  rq  c                   sN   t j| _| j|||I d H  | j D ] }|d k	r(||||I d H  q(d S rU   )rH   rM   r   r  	__aexit__r  ri  )r[   rr  rs  rt  r   rB   rB   rC   r|    s
    zAsyncClient.__aexit__)NNN)r>   r?   r@   rA   r   r   r   r\   r  r   r   r   r   r   rQ   r(  r1  r9  rA  r   rX  rZ  r^  ra  rd  rg  rk   r{  r|  rv  rB   rB   r  rC   r<     s$  9@e ,9.71"&'"!"!"!*)*)*)"   )_
__future__r   rd   enumloggingtypingr	  
contextlibr   r   typesr   r   r   r   r	   r
   _configr   r   r   r   r   r   	_decodersr   _exceptionsr   r   r   r   Z_modelsr   r   r   r   Z_status_codesr   Z_transports.asgir   Z_transports.baser   r   Z_transports.defaultr   r   Z_transports.wsgir    _typesr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   Z_urlsr1   r2   _utilsr3   r4   r5   r6   r7   TypeVarr8   r;   r=   r   	getLoggerrL  r   r   keysr   EnumrH   rN   ri   CallableAnyZ	EventHookrl   r9   r<   rB   rB   rB   rC   <module>   sZ    H


        m