a
    |f;
                    @   sD  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l.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z= ddl>m?Z?m@Z@ ddlAmBZBmCZCmDZDmEZEmFZF ejGdddZHejGd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"e:ZTG d#d$ d$e/ZUejVd%ejWf ZXG d&d' d'ZYG d(d deYZZG d)d deYZ[dS )*    N)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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__ r@   r@   GD:\Projects\storyit_web\backend\venv\Lib\site-packages\httpx/_client.pyr;   @   s   r;   Zhttpxzpython-httpx/z, c                 C   s   g | ]}|d kr|qS )identityr@   ).0keyr@   r@   rA   
<listcomp>[       rE   c                   @   s   e Zd ZdZdZdZdS )ClientStater         N)r<   r=   r>   UNOPENEDOPENEDCLOSEDr@   r@   r@   rA   rG   _   s   rG   c                   @   sF   e Zd ZdZeeeddddZej	e
 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.
    Nstreamresponsetimerreturnc                 C   s   || _ || _|| _d S N_stream	_response_timerselfrO   rP   rQ   r@   r@   rA   __init__s   s    zBoundSyncStream.__init__rR   c                 c   s   | j D ]
}|V  qd S rS   rU   rY   chunkr@   r@   rA   __iter__z   s    
zBoundSyncStream.__iter__c                 C   s(   | j  }tj|d| j_| j  d S N)seconds)rW   Zsync_elapseddatetime	timedeltarV   elapsedrU   closerY   ra   r@   r@   rA   re   ~   s    
zBoundSyncStream.close)r<   r=   r>   r?   r+   r   r1   rZ   typingIteratorbytesr_   re   r@   r@   r@   rA   rM   m   s   rM   c                   @   sF   e Zd ZdZeeeddddZej	e
 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.
    NrN   c                 C   s   || _ || _|| _d S rS   rT   rX   r@   r@   rA   rZ      s    zBoundAsyncStream.__init__r[   c                 C  s    | j 2 z3 d H W }|V  q6 d S rS   r\   r]   r@   r@   rA   	__aiter__   s    zBoundAsyncStream.__aiter__c                    s4   | j  I d H }tj|d| j_| j I d H  d S r`   )rW   Zasync_elapsedrb   rc   rV   rd   rU   acloserf   r@   r@   rA   rl      s    zBoundAsyncStream.aclose)r<   r=   r>   r?   r    r   r1   rZ   rg   AsyncIteratorri   rk   rl   r@   r@   r@   rA   rj      s   rj   .c                   @   s  e Zd Zddddededddddeje eje eje	 eje
 eeeejejeeje f  eeejeejegef f dddd	Zeed
ddZeed
ddZeedddZeje eejeeje f dddZee d
ddZ!e!j"eddddZ!eejeeje f d
ddZ#e#j"ejeeje f ddddZ#eeje$ d
ddZ%e%j"eddd dZ%eed
d!d"Z&e&j"eddd#d"Z&ee'd
d$d%Z(e(j"e	dd&d'd%Z(ee)d
d(d)Z*e*j"e
dd*d+d)Z*ee+d
d,d-Z,e,j"edd.d/d-Z,ddddddde-dd0	eeeje. eje/ eje0 ejej1 eje eje	 eje
 ejee2f eje3 e4d1d2d3Z5eedd4d5Z6dNeje
 eje
 d*d6d7Z7dOeje	 eje	 d&d8d9Z8dPeje eje d.d:d;Z9eje eje$ dd<d=Z:e-fe4ejee2df e$d>d?d@Z;e4e<e4dAdBdCZ=e4e<edAdDdEZ>e4e<edAdFdGZ?e4eee'dHdIdJZ@e4eejejeAeBf  dKdLdMZCdS )Q
BaseClientNF Tutf-8authparamsheaderscookiestimeoutfollow_redirectsmax_redirectsevent_hooksbase_url	trust_envdefault_encoding)rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   rR   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requestrP   )r~   rP   )_enforce_trailing_slashr/   	_base_url_build_auth_authr0   _paramsr   rt   r   _cookiesr   _timeoutrw   rx   listget_event_hooks
_trust_env_default_encodingrG   rJ   _state)rY   rr   rs   rt   ru   rv   rw   rx   ry   rz   r{   r|   r@   r@   rA   rZ      s    



zBaseClient.__init__r[   c                 C   s   | j tjkS )z2
        Check if the client being closed
        )r   rG   rL   rY   r@   r@   rA   	is_closed   s    zBaseClient.is_closedc                 C   s   | j S rS   )r   r   r@   r@   rA   r{      s    zBaseClient.trust_envurlrR   c                 C   s"   |j dr|S |j|j d dS )N   /raw_path)r   endswith	copy_withrY   r   r@   r@   rA   r      s    z"BaseClient._enforce_trailing_slash)proxiesallow_env_proxiesrR   c                 C   s   |d u 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 u rd nt |dqS )Nr   )r   )rC   rD   r   r@   r@   rA   
<dictcomp>   s   z-BaseClient._get_proxy_map.<locals>.<dictcomp>r   zall://)r3   items
isinstancedictstrr/   r   )rY   r   r   new_proxiesrD   valueproxyr@   r@   rA   _get_proxy_map   s    
zBaseClient._get_proxy_mapc                 C   s   | j S rS   )r   r   r@   r@   rA   rv      s    zBaseClient.timeout)rv   rR   c                 C   s   t || _d S rS   )r   r   )rY   rv   r@   r@   rA   rv      s    c                 C   s   | j S rS   )r   r   r@   r@   rA   ry      s    zBaseClient.event_hooks)ry   rR   c                 C   s(   t |dg t |dg d| _d S r}   )r   r   r   )rY   ry   r@   r@   rA   ry      s    c                 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   r@   r@   rA   rr      s    	zBaseClient.auth)rr   rR   c                 C   s   |  || _d S rS   )r   r   rY   rr   r@   r@   rA   rr     s    c                 C   s   | j S )zK
        Base URL to use when sending requests with relative URLs.
        )r   r   r@   r@   rA   rz     s    zBaseClient.base_urlc                 C   s   |  t|| _d S rS   )r   r/   r   r   r@   r@   rA   rz     s    c                 C   s   | j S )z@
        HTTP headers to include when sending requests.
        )_headersr   r@   r@   rA   rt     s    zBaseClient.headers)rt   rR   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   )rY   rt   Zclient_headersr@   r@   rA   rt     s    
c                 C   s   | j S )zA
        Cookie values to include when sending requests.
        )r   r   r@   r@   rA   ru   *  s    zBaseClient.cookies)ru   rR   c                 C   s   t || _d S rS   )r   r   )rY   ru   r@   r@   rA   ru   1  s    c                 C   s   | j S )zO
        Query parameters to include in the URL when sending requests.
        )r   r   r@   r@   rA   rs   5  s    zBaseClient.params)rs   rR   c                 C   s   t || _d S rS   )r0   r   )rY   rs   r@   r@   rA   rs   <  s    )	contentdatafilesjsonrs   rt   ru   rv   
extensions)methodr   r   r   r   r   rs   rt   ru   rv   r   rR   c       	         C   s   |  |}| |}| |	}	| |}|du r4i n|}d|vrrt|
trP| jnt|
}
tf i |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
        Nrv   )r   r   r   r   rs   rt   ru   r   )
_merge_url_merge_headers_merge_cookies_merge_queryparamsr   r;   rv   r   r   as_dictr   )rY   r   r   r   r   r   r   rs   rt   ru   rv   r   r@   r@   rA   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   )r/   is_relative_urlrz   r   lstripr   )rY   r   Z	merge_urlZmerge_raw_pathr@   r@   rA   r   s  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.
        )ru   r   r   )rY   ru   merged_cookiesr@   r@   rA   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   rt   r   )rY   rt   Zmerged_headersr@   r@   rA   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.
        )rs   r0   merge)rY   rs   Zmerged_queryparamsr@   r@   rA   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   r@   r@   rA   r     s    


zBaseClient._build_auth)r~   rr   rR   c                 C   sR   t |tr| jn| |}|d ur&|S |jj|jj }}|s@|rLt||dS t S )Nr   )	r   r;   r   r   r   r   r   r   r   )rY   r~   rr   r   r   r@   r@   rA   _build_request_auth  s    zBaseClient._build_request_auth)r~   rP   rR   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   rt   ru   rO   r   )_redirect_method_redirect_url_redirect_headers_redirect_streamr   ru   r   r   )rY   r~   rP   r   r   rt   rO   ru   r@   r@   rA   _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)rY   r~   rP   r   r@   r@   rA   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.
        ZLocationz Invalid URL in location header: .r~   N)host)fragment)rt   r/   r   r   schemer   r   r   r   joinr   )rY   r~   rP   locationr   excr@   r@   rA   r     s     
zBaseClient._redirect_url)r~   r   r   rR   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   rt   r5   r   r4   popnetlocdecoder   )rY   r~   r   r   rt   r@   r@   rA   r     s    
zBaseClient._redirect_headers)r~   r   rR   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   rO   )rY   r~   r   r@   r@   rA   r   -  s    zBaseClient._redirect_stream)N)N)N)Dr<   r=   r>   r   r   rg   Optionalr!   r&   r$   r#   r,   boolintMappingr   List	EventHookr-   UnionCallableri   rZ   propertyr   r{   r/   r   r%   Dictr   r   r   rv   setterry   r   rr   rz   r   rt   r   ru   r0   rs   USE_CLIENT_DEFAULTr'   r(   r*   Anyr;   r)   r   r   r   r   r   r   r   r   r   r   r   r   r   r+   r    r   r@   r@   r@   rA   rn      s   $
 

3   rn   c                       s  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eje	 eje
 eje eje eeje eeeje ejejeef  eeeeejejeeje f  eeje ejejdejf  eejeejegef f dd	 fd
dZddddedddfeeje eeeeje ejejdejf  eed	ddZddddedfe eeje eeeeedddZ!e"edddZ#ddddddde$e$e$ddeeeje% eje& eje' ejej eje
 eje eje eje	e(df ejee(f ejee(f eje) e*dddZ+e,ddddddde$e$e$ddeeeje% eje& eje' ejej eje
 eje eje eje	e(df ejee(f ejee(f eje) ej-e* dddZ.de$e$de/eeje	e(df ejee(f e*dddZ0e/e1eeje* e*dd d!Z2e/eeje* e*d"d#d$Z3e/e*d%d&d'Z4ddde$e$e$dd(eeje
 eje eje eje	e(f ejee(f ejee(f eje) e*d)	d*d+Z5ddde$e$e$dd(eeje
 eje eje eje	e(f ejee(f ejee(f eje) e*d)	d,d-Z6ddde$e$e$dd(eeje
 eje eje eje	e(f ejee(f ejee(f eje) e*d)	d.d/Z7ddddddde$e$e$ddeeje% eje& eje' ejej eje
 eje eje eje	e(f ejee(f ejee(f eje) e*d0d1d2Z8ddddddde$e$e$ddeeje% eje& eje' ejej eje
 eje eje eje	e(f ejee(f ejee(f eje) e*d0d3d4Z9ddddddde$e$e$ddeeje% eje& eje' ejej eje
 eje eje eje	e(f ejee(f ejee(f eje) e*d0d5d6Z:ddde$e$e$dd(eeje
 eje eje eje	e(f ejee(f ejee(f eje) e*d)	d7d8Z;dd9d:d;Z<e=e=d<d=d>Z>dBejej?e@  eje@ ejeA dd?d@dAZB  ZCS )Cr7   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).
    * **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".
    NTFro   rp   rr   rs   rt   ru   verifycerthttp1http2r   mountsrv   rw   limitsrx   ry   rz   	transportappr{   r|   .rr   rs   rt   ru   r   r   r   r   r   r   rv   rw   r   rx   ry   rz   r   r   r{   r|   rR   c                   s   t  j||||||||||d rPzdd l}W n tyN   tdd Y n0 ob|d u ob|d u }|	|}j ||d_ fdd| D _|
d ur҈j	dd |
 D  t
tj _d S )Nrq   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   r{   c                    s:   i | ]2\}}t ||d u rd nj| dqS Nr   r   r   r   r   r{   r2   _init_proxy_transportrC   rD   r   r   r   r   r   rY   r{   r   r@   rA   r     s   z#Client.__init__.<locals>.<dictcomp>c                 S   s   i | ]\}}t ||qS r@   r2   rC   rD   r   r@   r@   rA   r     rF   superrZ   h2ImportErrorr   _init_transport
_transportr   _mountsr   r   sortedrY   rr   rs   rt   ru   r   r   r   r   r   r   rv   rw   r   rx   ry   rz   r   r   r{   r|   r   r   Z	proxy_map	__class__r   rA   rZ   l  sT    
zClient.__init__	r   r   r   r   r   r   r   r{   rR   c	           	      C   s2   |d ur|S |d urt |dS t||||||dS N)r   r   )r   r   	rY   r   r   r   r   r   r   r   r{   r@   r@   rA   r     s    
zClient._init_transportr   r   r   r   r   r   r{   rR   c              	   C   s   t |||||||dS )N)r   r   r   r   r   r{   r   )r   rY   r   r   r   r   r   r   r{   r@   r@   rA   r     s    
zClient._init_proxy_transportr   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  rY   r   patternr   r@   r@   rA   _transport_for_url  s    
zClient._transport_for_urlr   r   r   r   rs   rt   ru   rr   rw   rv   r   r   r   r   r   r   r   rs   rt   ru   rr   rw   rv   r   rR   c                C   sH   |	du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
        NzSetting 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   rs   rt   ru   rv   r   rr   rw   )warningswarnDeprecationWarningr   send)rY   r   r   r   r   r   r   rs   rt   ru   rr   rw   rv   r   messager~   r@   r@   rA   r~     s$    !zClient.requestc                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~   rr   rw   rO   N)r   r  re   rY   r   r   r   r   r   r   rs   rt   ru   rr   rw   rv   r   r~   rP   r@   r@   rA   rO   0  s,    zClient.streamrO   rr   rw   r~   rO   rr   rw   rR   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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/#request-instances
        5Cannot send a request, as the client has been closed.rr   rw   historyN)r   rG   rL   RuntimeErrorrK   r   r;   rw   r   _send_handling_authreadBaseExceptionre   rY   r~   rO   rr   rw   rP   r   r@   r@   rA   r  d  s*    zClient.sendr~   rr   rw   r!  rR   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)rw   r!  )Zsync_auth_flownext_send_handling_redirectsr  StopIterationre   r   r!  r$  appendr%  	rY   r~   rr   rw   r!  Z	auth_flowrP   next_requestr   r@   r@   rA   r#    s.    


zClient._send_handling_authr~   rw   r!  rR   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~   rP   )lenrx   r   r   _send_single_requestr   r!  has_redirect_locationr   r$  r.  r%  re   rY   r~   rw   r!  hookrP   r   r@   r@   rA   r*    s,    






zClient._send_handling_redirectsr~   rR   c                 C   s   |  |j}t }|  t|jts.tdt|d |	|}W d   n1 sX0    Y  t|jtsrJ ||_
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rP   rQ   HTTP Request: %s %s "%s %d %s")r  r   r1   Z
sync_startr   rO   r+   r"  r   handle_requestr~   rM   ru   extract_cookiesr   r|   loggerinfor   http_versionr   reason_phraserY   r~   r   rQ   rP   r@   r@   rA   r2    s2    (	zClient._send_single_requestrs   rt   ru   rr   rw   rv   r   	r   rs   rt   ru   rr   rw   rv   r   rR   c          	      C   s   | j d||||||||d	S )U
        Send a `GET` request.

        **Parameters**: See `httpx.request`.
        r   rA  r   	rY   r   rs   rt   ru   rr   rw   rv   r   r@   r@   rA   r      s    z
Client.getc          	      C   s   | j d||||||||d	S )Z
        Send an `OPTIONS` request.

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

        **Parameters**: See `httpx.request`.
        r   rA  r   rD  r@   r@   rA   head:  s    zClient.headr   r   r   r   r   rs   rt   ru   rr   rw   rv   r   rR   c                C   s$   | j d|||||||||	|
||dS )V
        Send a `POST` request.

        **Parameters**: See `httpx.request`.
        r   r  r   rY   r   r   r   r   r   rs   rt   ru   rr   rw   rv   r   r@   r@   rA   postW  s    zClient.postc                C   s$   | j d|||||||||	|
||dS )U
        Send a `PUT` request.

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

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

        **Parameters**: See `httpx.request`.
        DELETErA  r   rD  r@   r@   rA   delete  s    zClient.deleter[   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   rG   rL   r  re   r  values)rY   r   r@   r@   rA   re     s    
zClient.closerY   rR   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   rG   rJ   rK   rL   r"  r  	__enter__r  rX  )rY   msgr   r@   r@   rA   r[    s    

zClient.__enter__exc_type	exc_value	tracebackrR   c                 C   sB   t j| _| j||| | j D ]}|d ur"|||| q"d S rS   )rG   rL   r   r  __exit__r  rX  )rY   r^  r_  r`  r   r@   r@   rA   ra    s
    zClient.__exit__)NNN)Dr<   r=   r>   r?   r   r
   r   rg   r   r!   r&   r$   r#   r.   r"   r   r%   r   r   r   r,   r   r   r   r   r-   r   r   r   ri   rZ   r   r   r   r/   r  r   r'   r(   r*   r;   r)   r   r~   r   rh   rO   r   r  r   r#  r*  r2  r   rG  rI  rM  rP  rS  rV  re   r6   r[  Typer%  r   ra  __classcell__r@   r@   r  rA   r7   9  s  5U
8
73$%(!!!
)
)
)   c                       s  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eje	 eje
 eje eje eeje eeeje ejejeef  eeeeejejeejejdejf  f  eeje ejejdejf  eejeejegef f dd	 fd
dZddddedddfeeje eeeeje ejejdejf  eed	ddZddddedfeeeje eeeeedddZ e!edddZ"ddddddde#e#e#ddeeeje$ eje% eje& ejej eje
 eje eje eje	e'df ejee'f ejee'f eje( e)dddZ*e+ddddddde#e#e#ddeeeje$ eje% eje& ejej eje
 eje eje eje	e'f ejee'f ejee'f eje( ej,e) dddZ-de#e#de.eeje	e'df ejee'f e)dddZ/e.e0eeje) e)dd d!Z1e.eeje) e)d"d#d$Z2e.e)d%d&d'Z3ddde#e#e#dd(eeje
 eje eje eje	e'df ejee'f ejee'f eje( e)d)	d*d+Z4ddde#e#e#dd(eeje
 eje eje eje	e'f ejee'f ejee'f eje( e)d)	d,d-Z5ddde#e#e#dd(eeje
 eje eje eje	e'f ejee'f ejee'f eje( e)d)	d.d/Z6ddddddde#e#e#ddeeje$ eje% eje& ejej eje
 eje eje eje	e'f ejee'f ejee'f eje( e)d0d1d2Z7ddddddde#e#e#ddeeje$ eje% eje& ejej eje
 eje eje eje	e'f ejee'f ejee'f eje( e)d0d3d4Z8ddddddde#e#e#ddeeje$ eje% eje& ejej eje
 eje eje eje	e'f ejee'f ejee'f eje( e)d0d5d6Z9ddde#e#e#dd(eeje
 eje eje eje	e'f ejee'f ejee'f eje( e)d)	d7d8Z:dd9d:d;Z;e<e<d<d=d>Z=dBejej>e?  eje? eje@ dd?d@dAZA  ZBS )Cr:   aC	  
    An asynchronous HTTP client, with connection pooling, HTTP/2, redirects,
    cookie persistence, etc.

    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`.
    * **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".
    NTFro   rp   r   .r   c                   s   t  j||||||||||d rPzdd l}W n tyN   tdd Y n0 ob|d u ob|d u }|	|}j ||d_ fdd| D _|
d ur҈j	dd |
 D  t
tj _d S )Nrq   r   r   r   c                    s:   i | ]2\}}t ||d u rd nj| dqS r   r   r   r   r@   rA   r     s   z(AsyncClient.__init__.<locals>.<dictcomp>c                 S   s   i | ]\}}t ||qS r@   r   r   r@   r@   rA   r     rF   r   r  r  r   rA   rZ   A  sT    zAsyncClient.__init__r  c	           	      C   s2   |d ur|S |d urt |dS t||||||dS r  )r   r   r	  r@   r@   rA   r     s    
zAsyncClient._init_transportr
  c                 C   s   t ||||||dS )N)r   r   r   r   r{   r   )r   r  r@   r@   rA   r     s    
z!AsyncClient._init_proxy_transportr   c                 C   s:   | j  D ](\}}||r
|du r*| jn|  S q
| jS r  r  r  r@   r@   rA   r    s    
zAsyncClient._transport_for_urlr  r  c                   s6   | 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
        r  r  N)r   r  )rY   r   r   r   r   r   r   rs   rt   ru   rr   rw   rv   r   r~   r@   r@   rA   r~     s    !zAsyncClient.requestc                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  rl   r  r@   r@   rA   rO     s,    zAsyncClient.streamr  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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/#request-instances
        r  r   N)r   rG   rL   r"  rK   r   r;   rw   r   r#  areadr%  rl   r&  r@   r@   rA   r  0  s*    zAsyncClient.sendr'  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 r(  )Zasync_auth_flow	__anext__r*  asendStopAsyncIterationrl   r   r!  rd  r,  r%  r-  r@   r@   rA   r#  a  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 r0  )r1  rx   r   r   r2  r   r!  r3  r   rd  r.  r%  rl   r4  r@   r@   rA   r*    s,    


z$AsyncClient._send_handling_redirectsr6  c                    s   |  |j}t }| I dH  t|jts4tdt|d  |	|I dH }W d   n1 sd0    Y  t|jts~J ||_
t|j||d|_| j| | j|_td|j|j|j|j|j |S )r7  Nz?Attempted to send an sync request with an AsyncClient instance.r   r8  r9  )r  r   r1   Zasync_startr   rO   r    r"  r   Zhandle_async_requestr~   rj   ru   r;  r   r|   r<  r=  r   r>  r   r?  r@  r@   r@   rA   r2    s2    .	z AsyncClient._send_single_requestrA  rB  c          	         s"   | j d||||||||d	I dH S )rC  r   rA  Nr   rD  r@   r@   rA   r     s    zAsyncClient.getc          	         s"   | j d||||||||d	I dH S )rE  rF  rA  Nr   rD  r@   r@   rA   rG    s    zAsyncClient.optionsc          	         s"   | j d||||||||d	I dH S )rH  r   rA  Nr   rD  r@   r@   rA   rI    s    zAsyncClient.headrJ  c                   s*   | j d|||||||||	|
||dI dH S )rK  r   r  Nr   rL  r@   r@   rA   rM  #  s    zAsyncClient.postc                   s*   | j d|||||||||	|
||dI dH S )rN  rO  r  Nr   rL  r@   r@   rA   rP  H  s    zAsyncClient.putc                   s*   | j d|||||||||	|
||dI dH S )rQ  rR  r  Nr   rL  r@   r@   rA   rS  m  s    zAsyncClient.patchc          	         s"   | j d||||||||d	I dH S )rT  rU  rA  Nr   rD  r@   r@   rA   rV    s    zAsyncClient.deleter[   c                    sN   | j tjkrJtj| _ | j I dH  | j D ]}|dur.| I dH  q.dS rW  )r   rG   rL   r  rl   r  rX  )rY   r   r@   r@   rA   rl     s    zAsyncClient.acloserY  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 rZ  )
r   rG   rJ   rK   rL   r"  r  
__aenter__r  rX  )rY   r\  r   r@   r@   rA   rh    s    zAsyncClient.__aenter__r]  c                    sN   t j| _| j|||I d H  | j D ] }|d ur(||||I d H  q(d S rS   )rG   rL   r   r  	__aexit__r  rX  )rY   r^  r_  r`  r   r@   r@   rA   ri    s
    zAsyncClient.__aexit__)NNN)Cr<   r=   r>   r?   r   r
   r   rg   r   r!   r&   r$   r#   r.   r"   r   r%   r   r   r   r,   r   r   r   r   r   r-   r   ri   rZ   r   r   r   r/   r  r   r'   r(   r*   r;   r)   r   r~   r   rm   rO   r   r  r   r#  r*  r2  r   rG  rI  rM  rP  rS  rV  rl   r9   rh  rb  r%  r   ri  rc  r@   r@   r  rA   r:     s  6U
0
73$&'!!!
)
)
)   )\rb   enumloggingrg   r  
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.   Z_urlsr/   r0   Z_utilsr1   r2   r3   r4   r5   TypeVarr6   r9   r;   r   	getLoggerr<  r   r   keysr   EnumrG   rM   rj   r   r   r   rn   r7   r:   r@   r@   r@   rA   <module>   sX    D


        Y