o
    i                     @  s`  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jArd dlBZBg dZCejDdddZEejDdddZFd7ddZGd8d d!ZHd9d#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 d0d1 d1e.ZUejVd2ejWf ZXG d3d4 d4ZYG d5d deYZZG d6d deY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)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   urlr,   locationreturnboolc                 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)r6   r7    rA   G/home/kim/smarthome/.venv/lib/python3.10/site-packages/httpx/_client.py_is_https_redirect>   s   


rC   
int | Nonec                 C  s"   | j d ur| j S ddd| jS )Nr;   r=   )r:   r<   )portgetr?   r6   rA   rA   rB   r@   M   s   
r@   otherc                 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@   )r6   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   Zhttpxzpython-httpx/z, c                 C  s   g | ]}|d kr|qS )identityrA   ).0keyrA   rA   rB   
<listcomp>y   s    rR   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   rS   }   s    rS   c                   @  .   e Zd ZdZdd
dZdddZ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.
    streamr*   responser   startfloatr8   Nonec                 C     || _ || _|| _d S N_stream	_response_startselfr[   r\   r]   rA   rA   rB   __init__      
zBoundSyncStream.__init__typing.Iterator[bytes]c                 c  s    | j D ]}|V  qd S ra   rc   rg   chunkrA   rA   rB   __iter__   s   
zBoundSyncStream.__iter__c                 C  s,   t  | j }tj|d| j_| j  d S N)seconds)	timeperf_counterre   datetime	timedeltard   elapsedrc   closerg   ru   rA   rA   rB   rv      s   zBoundSyncStream.closeN)r[   r*   r\   r   r]   r^   r8   r_   )r8   rj   r8   r_   )rK   rL   rM   rN   rh   rn   rv   rA   rA   rA   rB   rZ      
    

rZ   c                   @  rY   )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]   r^   r8   r_   c                 C  r`   ra   rb   rf   rA   rA   rB   rh      ri   zBoundAsyncStream.__init__typing.AsyncIterator[bytes]c                 C s"   | j 2 z	3 d H W }|V  q6 d S ra   rk   rl   rA   rA   rB   	__aiter__   s   zBoundAsyncStream.__aiter__c                   s4   t  | j }tj|d| j_| j I d H  d S ro   )	rq   rr   re   rs   rt   rd   ru   rc   acloserw   rA   rA   rB   r}      s   zBoundAsyncStream.acloseN)r[   r   r\   r   r]   r^   r8   r_   )r8   r{   rx   )rK   rL   rM   rN   rh   r|   r}   rA   rA   rA   rB   rz      ry   rz   .c                   @  s  e Zd Zddddededddddd|ddZed}d d!Zed}d"d#Zd~d&d'Z	dd,d-Z
edd/d0Zejdd1d0Zedd3d4Zejdd5d4Zedd7d8Zejdd:d8Zedd;d<Zejdd=d<Zedd?d@ZejddBd@ZeddDdEZejddGdEZeddIdJZejddLdJZdddddddeddM	dd\d]Zdd^d_Zddd`daZdddbdcZ	dddddeZddfdgZefddkdlZddodpZddqdrZddsdtZddudvZddxdyZddzd{Z dS )
BaseClientNF Tutf-8authparamsheaderscookiestimeoutfollow_redirectsmax_redirectsevent_hooksbase_url	trust_envdefault_encodingr   AuthTypes | Noner   QueryParamTypes | Noner   HeaderTypes | Noner   CookieTypes | Noner   r+   r   r9   r   intr   +None | typing.Mapping[str, list[EventHook]]r   	URL | strr   r   #str | typing.Callable[[bytes], str]r8   r_   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_encodingrS   rV   _state)rg   r   r   r   r   r   r   r   r   r   r   r   rA   rA   rB   rh      s   



zBaseClient.__init__c                 C  s   | j tjkS )z2
        Check if the client being closed
        )r   rS   rX   rg   rA   rA   rB   	is_closed   s   zBaseClient.is_closedc                 C     | j S ra   )r   r   rA   rA   rB   r         zBaseClient.trust_envr6   r,   c                 C  s"   |j dr|S |j|j d dS )N   /raw_path)r   endswith	copy_withrg   r6   rA   rA   rB   r      s   z"BaseClient._enforce_trailing_slashproxyProxyTypes | Noneallow_env_proxiesdict[str, Proxy | None]c                 C  sH   |d u r|rdd t   D S i S t|ttfrt|dn|}d|iS )Nc                 S  s(   i | ]\}}||d u rd nt |dqS )NrG   )r   )rP   rQ   r6   rA   rA   rB   
<dictcomp>   s    z-BaseClient._get_proxy_map.<locals>.<dictcomp>rG   zall://)r/   items
isinstancestrr,   r   )rg   r   r   rA   rA   rB   _get_proxy_map   s   zBaseClient._get_proxy_mapr   c                 C  r   ra   )r   r   rA   rA   rB   r      r   zBaseClient.timeoutc                 C     t || _d S ra   )r   r   )rg   r   rA   rA   rB   r        dict[str, list[EventHook]]c                 C  r   ra   )r   r   rA   rA   rB   r     r   zBaseClient.event_hooksc                 C  s(   t |dg t |dg d| _d S r   )r   rF   r   )rg   r   rA   rA   rB   r   	  s   Auth | Nonec                 C  r   )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    c                 C  s   |  || _d S ra   )r   r   rg   r   rA   rA   rB   r     s   c                 C  r   )zK
        Base URL to use when sending requests with relative URLs.
        )r   r   rA   rA   rB   r        zBaseClient.base_urlc                 C  s   |  t|| _d S ra   )r   r,   r   r   rA   rA   rB   r   &  s   r   c                 C  r   )z@
        HTTP headers to include when sending requests.
        )_headersr   rA   rA   rB   r   *  r   zBaseClient.headersr#   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   )rg   r   Zclient_headersrA   rA   rB   r   1  s   

r   c                 C  r   )zA
        Cookie values to include when sending requests.
        )r   r   rA   rA   rB   r   >  r   zBaseClient.cookiesr"   c                 C  r   ra   )r   r   )rg   r   rA   rA   rB   r   E  r   r-   c                 C  r   )zO
        Query parameters to include in the URL when sending requests.
        )r   r   rA   rA   rB   r   I  r   zBaseClient.paramsr%   c                 C  r   ra   )r-   r   )rg   r   rA   rA   rB   r   P  r   )	contentdatafilesjsonr   r   r   r   
extensionsmethodr   r   RequestContent | Noner   RequestData | Noner   RequestFiles | Noner   typing.Any | NoneTimeoutTypes | UseClientDefaultr   RequestExtensions | Noner   c       	         C  s   |  |}| |}| |	}	| |}|du ri n|}d|vr9t|
tr(| jnt|
}
td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   rA   )
_merge_url_merge_headers_merge_cookies_merge_queryparamsr   rJ   r   r   dictas_dictr   )rg   r   r6   r   r   r   r   r   r   r   r   r   rA   rA   rB   build_requestT  s.   



zBaseClient.build_requestc                 C  s4   t |}|jr| 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   )rg   r6   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   )rg   r   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   )rg   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)rg   r   Zmerged_queryparamsrA   rA   rB   r     s   


zBaseClient._merge_queryparamsc                 C  sX   |d u rd S t |trt|d |d dS t |tr|S t|r%t|dS td|)Nr   r   usernamepassword)funczInvalid "auth" argument: )r   tupler	   r   callabler
   	TypeErrorr   rA   rA   rB   r     s   


zBaseClient._build_authr   #AuthTypes | UseClientDefault | Noner   c                 C  sR   t |tr| jn| |}|d ur|S |jj|jj}}|s |r&t||dS t S )Nr   )	r   rJ   r   r   r6   r   r   r	   r   )rg   r   r   r   r   rA   rA   rB   _build_request_auth  s   zBaseClient._build_request_authr\   r   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   r6   r   r   r[   r   )_redirect_method_redirect_url_redirect_headers_redirect_streamr   r   r   r   )rg   r   r\   r   r6   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r|dkrd}|jtjkr'|dkr'd}|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)rg   r   r\   r   rA   rA   rB   r     s   zBaseClient._redirect_methodc              
   C  s   |j d }zt|}W n ty" } ztd| d|ddd}~ww |jr1|js1|j|jjd}|jr:|j	|}|jj
rI|j
sI|j|jj
d}|S )z<
        Return the URL for the redirect to follow.
        ZLocationz Invalid URL in location header: .r   N)r>   )fragment)r   r,   r   r   r?   r>   r   r6   r   joinr   )rg   r   r\   r7   r6   excrA   rA   rB   r     s$   
zBaseClient._redirect_urlc                 C  sx   t |j}t||jst|j|s|dd |jd|d< ||jkr4|dkr4|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   r6   rC   popnetlocdecoder   )rg   r   r6   r   r   rA   rA   rB   r   "  s   
zBaseClient._redirect_headers'SyncByteStream | AsyncByteStream | Nonec                 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[   )rg   r   r   rA   rA   rB   r   =  s   zBaseClient._redirect_streamc                 C  sL   d|j vr$t| jtr| jnt| j}tdi |j d| i|_ d S d S )Nr   rA   )r   r   r   rJ   r   r   r   )rg   r   r   rA   rA   rB   _set_timeoutH  s   

"zBaseClient._set_timeout)r   r   r   r   r   r   r   r   r   r+   r   r9   r   r   r   r   r   r   r   r9   r   r   r8   r_   )r8   r9   )r6   r,   r8   r,   )r   r   r   r9   r8   r   )r8   r   )r   r+   r8   r_   )r8   r   )r   r   r8   r_   )r8   r   )r   r    r8   r_   )r8   r,   )r6   r   r8   r_   )r8   r   )r   r#   r8   r_   )r8   r   )r   r"   r8   r_   )r8   r-   )r   r%   r8   r_   )r   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r   )r6   r   r8   r,   ra   )r   r   r8   r   )r   r   r8   r   )r   r   r8   r   )r   r   r8   r   )r   r   r   r   r8   r   )r   r   r\   r   r8   r   )r   r   r\   r   r8   r   )r   r   r\   r   r8   r,   )r   r   r6   r,   r   r   r8   r   )r   r   r   r   r8   r   )r   r   r8   r_   )!rK   rL   rM   r   r   rh   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                      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| fd-d.Zdddddedfd}d0d1Zdddddefd~d3d4Z	dd7d8Z
dddddddeeedd9ddJdKZedddddddeeedd9ddMdNZdeedOddSdTZddXdYZddZd[Zdd\d]Zdddeeedd^dd_d`Zdddeeedd^ddbdcZdddeeedd^ddddeZdddddddeeedd9ddfdgZdddddddeeedd9ddhdiZdddddddeeedd9ddjdkZdddeeedd^ddldmZddndoZddrdsZ			dddzd{Z  ZS )r2   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   r   r   r   r   r   ssl.SSLContext | str | boolr   CertTypes | Noner   r9   r   r   r   r   r  0None | typing.Mapping[str, BaseTransport | None]r   r+   r   r  r   r   r   r   r   r   r   r  BaseTransport | Noner   r   r8   r_   c                  s   t  j||||||||||d r&zdd l}W n ty%   tdd w o+|d u }|
|}j |d_ fdd| D _|d urb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                   :   i | ]\}}t ||d u rd nj| dqS N)r   r   r   r   r   r  r.   _init_proxy_transportrP   rQ   r   r   r   r   r  rg   r   r   rA   rB   r         z#Client.__init__.<locals>.<dictcomp>c                 S     i | ]	\}}t ||qS rA   r.   rP   rQ   r  rA   rA   rB   r         superrh   h2ImportErrorr   _init_transport
_transportr   _mountsr   r   sorted)rg   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   rh     sT   	zClient.__init__r   c                 C      |d ur|S t ||||||dS r  r   rg   r   r   r   r   r   r  r  rA   rA   rB   r       
zClient._init_transportr   c              	   C     t |||||||dS N)r   r   r   r   r   r  r   r!  rg   r   r   r   r   r   r   r  rA   rA   rB   r       
zClient._init_proxy_transportr6   r,   c                 C  >   | j  D ]\}}||r|du r| j  S |  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  rg   r6   patternr  rA   rA   rB   _transport_for_url  
   
zClient._transport_for_urlr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   bool | UseClientDefaultr   r   r   r   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.rT   
stacklevelr   r6   r   r   r   r   r   r   r   r   r   r   r   warningswarnDeprecationWarningr   sendrg   r   r6   r   r   r   r   r   r   r   r   r   r   r   messager   rA   rA   rB   r     s$   !zClient.requesttyping.Iterator[Response]c                c  sT    | j |||||||||	||d}| j||
|dd}z
|V  W |  dS |  w 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
        r5  T)r   r   r   r[   N)r   r;  rv   rg   r   r6   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[   c             
   C  s   | j tjkr
tdtj| _ t|tr| jn|}| | | 	||}| j
|||g d}z	|s3|  |W S  tyG } z|  |d}~ww )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   rS   rX   RuntimeErrorrW   r   rJ   r   r   r   _send_handling_authreadBaseExceptionrv   rg   r   r[   r   r   r\   r   rA   rA   rB   r;  o  s0   
zClient.sendr   rD  list[Response]c           	   
   C  s   | |}zLt|}	 | j|||d}z+z||}W n ty,   | Y W W |  S w t||_|  |}|	| W n t
yP } z|  |d }~ww q|  w NT)r   rD  )Zsync_auth_flownext_send_handling_redirectsr;  StopIterationrv   r   rD  rG  appendrH  	rg   r   r   r   rD  Z	auth_flowr\   next_requestr   rA   rA   rB   rF    s6   




zClient._send_handling_authc              
   C  s   	 t || jkrtd|d| jd D ]}|| q| |}z1| jd D ]}|| q%t||_|js7|W S | ||}||g }|rI|	  n||_
|W S W n tyb } z|  |d }~ww qNTz#Exceeded maximum allowed redirects.r   r   r\   )lenr   r   r   _send_single_requestr   rD  has_redirect_locationr   rG  rQ  rH  rv   rg   r   r   rD  hookr\   r   rA   rA   rB   rM    s6   





zClient._send_handling_redirectsc                 C  s   |  |j}t }t|jtstdt|d |	|}W d   n1 s)w   Y  t|jts6J ||_
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\   r]   HTTP Request: %s %s "%s %d %s")r.  r6   rq   rr   r   r[   r*   rE  r   handle_requestr   rZ   r   extract_cookiesr   r   loggerinfor   http_versionr   reason_phraserg   r   r  r]   r\   rA   rA   rB   rT    s2   	zClient._send_single_requestr   r   r   r   r   r   r   c          	      C     | j d||||||||d	S )U
        Send a `GET` request.

        **Parameters**: See `httpx.request`.
        r   rb  r   	rg   r6   r   r   r   r   r   r   r   rA   rA   rB   rF        z
Client.getAuthTypes | UseClientDefaultc          	      C  rc  )Z
        Send an `OPTIONS` request.

        **Parameters**: See `httpx.request`.
        OPTIONSrb  r   re  rA   rA   rB   options)  rf  zClient.optionsc          	      C  rc  )V
        Send a `HEAD` request.

        **Parameters**: See `httpx.request`.
        r   rb  r   re  rA   rA   rB   headF  rf  zClient.headc                C  $   | j d|||||||||	|
||dS )V
        Send a `POST` request.

        **Parameters**: See `httpx.request`.
        r   r0  r   rg   r6   r   r   r   r   r   r   r   r   r   r   r   rA   rA   rB   postc     zClient.postc                C  rm  )U
        Send a `PUT` request.

        **Parameters**: See `httpx.request`.
        PUTr0  r   ro  rA   rA   rB   put  rq  z
Client.putc                C  rm  )W
        Send a `PATCH` request.

        **Parameters**: See `httpx.request`.
        PATCHr0  r   ro  rA   rA   rB   patch  rq  zClient.patchc          	      C  rc  )X
        Send a `DELETE` request.

        **Parameters**: See `httpx.request`.
        DELETErb  r   re  rA   rA   rB   delete  rf  zClient.deletec                 C  sF   | j tjkrtj| _ | j  | j D ]}|dur|  qdS dS z.
        Close transport and proxies.
        N)r   rS   rX   r  rv   r  values)rg   r  rA   rA   rB   rv     s   
zClient.closerg   r3   c                 C  s`   | j tjkrtjdtjdi| j  }t|tj| _ | j  | j	 D ]
}|d ur-|  q#| S Nz-Cannot open a client instance more than once.z9Cannot reopen a client instance, once it has been closed.)
r   rS   rV   rW   rX   rE  r  	__enter__r  r|  )rg   msgr  rA   rA   rB   r~    s   
zClient.__enter__exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | Nonec                 C  sB   t j| _| j||| | j D ]}|d ur|||| qd S ra   )rS   rX   r   r  __exit__r  r|  )rg   r  r  r  r  rA   rA   rB   r    s   zClient.__exit__)(r   r   r   r   r   r   r   r   r   r  r   r  r   r9   r   r9   r   r9   r   r   r  r  r   r+   r   r9   r  r   r   r   r   r   r   r   r  r  r   r   r8   r_   )r   r  r   r  r   r9   r   r9   r   r9   r  r   r  r  r8   r   )r   r   r   r  r   r  r   r9   r   r9   r   r9   r  r   r8   r   )r6   r,   r8   r   r   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1  r   r   r   r   r8   r   )r   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1  r   r   r   r   r8   r>  
r   r   r[   r9   r   r   r   r1  r8   r   
r   r   r   r   r   r9   rD  rJ  r8   r   r   r   r   r9   rD  rJ  r8   r   r   r   r8   r   r6   r   r   r   r   r   r   r   r   r   r   r1  r   r   r   r   r8   r   r6   r   r   r   r   r   r   r   r   rg  r   r1  r   r   r   r   r8   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rg  r   r1  r   r   r   r   r8   r   rx   )rg   r3   r8   r3   NNNr  r  r  r  r  r  r8   r_   )rK   rL   rM   rN   r   r   r   rh   r  r  r.  r0   r   r   r[   r;  rF  rM  rT  rF   rj  rl  rp  rt  rw  rz  rv   r~  r  __classcell__rA   rA   r  rB   r2   R  s    /Q
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| fd-d.Zdddddedfd}d0d1Zdddddefd~d3d4Z	dd7d8Z
dddddddeeedd9ddJdKZedddddddeeedd9ddMdNZdeedOddSdTZddXdYZddZd[Zdd\d]Zdddeeedd^dd_d`Zdddeeedd^ddbdcZdddeeedd^ddddeZdddddddeeedd9ddfdgZdddddddeeedd9ddhdiZdddddddeeedd9ddjdkZdddeeedd^ddldmZddndoZddrdsZ			dddzd{Z  ZS )r1   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   r   r   r   r  r   r  r   r9   r   r   r   r  5None | typing.Mapping[str, AsyncBaseTransport | None]r   r+   r   r  r   r   r   r   r   r   r   r  AsyncBaseTransport | Noner   r   r   r8   r_   c                  s   t  j||||||||||d r&zdd l}W n ty%   tdd w o+|d u }|	|}j |d_ fdd| D _|
d urbj	dd |
 D  t
tj _d S )Nr   r   r  r	  c                   r
  r  r  r  r  rA   rB   r     r  z(AsyncClient.__init__.<locals>.<dictcomp>c                 S  r  rA   r  r  rA   rA   rB   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   r  r  r  rB   rh   I  sT   
zAsyncClient.__init__r   c                 C  r   r  r   r"  rA   rA   rB   r    r#  zAsyncClient._init_transportr   c              	   C  r$  r%  r  r&  rA   rA   rB   r    r'  z!AsyncClient._init_proxy_transportr6   r,   c                 C  r(  r)  r*  r,  rA   rA   rB   r.    r/  zAsyncClient._transport_for_urlr0  r   r   r   r   r   r   r   r   r   r   r   r1  r   r   r   r   c                  sT   |	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
        Nr2  rT   r3  r5  r6  r7  r<  rA   rA   rB   r     s&   "zAsyncClient.requesttyping.AsyncIterator[Response]c                C sf   | j |||||||||	||d}| j||
|ddI dH }z|V  W | I dH  dS | I dH  w r?  )r   r;  r}   r@  rA   rA   rB   r[     s.   "zAsyncClient.streamrA  r   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|s:| I dH  |W S  tyQ } z	| I dH  |d}~ww )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
        rB  rC  N)r   rS   rX   rE  rW   r   rJ   r   r   r   rF  areadrH  r}   rI  rA   rA   rB   r;  :  s2   
zAsyncClient.sendr   rD  rJ  c           	   
     s   | |}z^| I d H }	 | j|||dI d H }z4z
||I d H }W n ty9   | Y W W | I d H  S w t||_| I d H  |}|	| W n t
yc } z	| I d H  |d }~ww q| I d H  w rK  )Zasync_auth_flow	__anext__rM  asendStopAsyncIterationr}   r   rD  r  rO  rH  rP  rA   rA   rB   rF  m  s8   


zAsyncClient._send_handling_authc              
     s   	 t || jkrtd|d| jd D ]	}||I d H  q| |I d H }z7| jd D ]	}||I d H  q,t||_|jsA|W S | ||}||g }|rV|	 I d H  n||_
|W S W n tyr } z	| I d H  |d }~ww qrR  )rS  r   r   r   rT  r   rD  rU  r   r  rQ  rH  r}   rV  rA   rA   rB   rM    s8   

z$AsyncClient._send_handling_redirectsc                   s   |  |j}t }t|jtstdt|d |	|I dH }W d   n1 s-w   Y  t|jts:J ||_
t|j||d|_| j| | j|_td|j|j|j|j|j |S )rX  z?Attempted to send an sync request with an AsyncClient instance.r   NrY  rZ  )r.  r6   rq   rr   r   r[   r   rE  r   Zhandle_async_requestr   rz   r   r\  r   r   r]  r^  r   r_  r   r`  ra  rA   rA   rB   rT    s4   	z AsyncClient._send_single_requestrb  c          	        $   | j d||||||||d	I dH S )rd  r   rb  Nr   re  rA   rA   rB   rF        zAsyncClient.getrg  c          	        r  )rh  ri  rb  Nr   re  rA   rA   rB   rj    r  zAsyncClient.optionsc          	        r  )rk  r   rb  Nr   re  rA   rA   rB   rl    r  zAsyncClient.headc                  ,   | j d|||||||||	|
||dI dH S )rn  r   r0  Nr   ro  rA   rA   rB   rp  .      zAsyncClient.postc                  r  )rr  rs  r0  Nr   ro  rA   rA   rB   rt  S  r  zAsyncClient.putc                  r  )ru  rv  r0  Nr   ro  rA   rA   rB   rw  x  r  zAsyncClient.patchc          	        r  )rx  ry  rb  Nr   re  rA   rA   rB   rz    r  zAsyncClient.deletec                   sT   | j tjkr&tj| _ | j I dH  | j D ]}|dur%| I dH  qdS dS r{  )r   rS   rX   r  r}   r  r|  )rg   r   rA   rA   rB   r}     s   zAsyncClient.acloserg   r5   c                   sn   | j tjkrtjdtjdi| j  }t|tj| _ | j I d H  | j	 D ]}|d ur4| I d H  q'| S r}  )
r   rS   rV   rW   rX   rE  r  
__aenter__r  r|  )rg   r  r   rA   rA   rB   r    s    zAsyncClient.__aenter__r  r  r  r  r  r  c                   sP   t j| _| j|||I d H  | j D ]}|d ur%||||I d H  qd S ra   )rS   rX   r   r  	__aexit__r  r|  )rg   r  r  r  r   rA   rA   rB   r    s   zAsyncClient.__aexit__)(r   r   r   r   r   r   r   r   r   r  r   r  r   r9   r   r9   r   r   r  r  r   r+   r   r9   r  r   r   r   r   r   r   r   r  r  r   r9   r   r   r8   r_   )r   r  r   r  r   r9   r   r9   r   r9   r  r   r  r  r8   r   )r   r   r   r  r   r  r   r9   r   r9   r   r9   r  r   r8   r   )r6   r,   r8   r   r  )r   r   r6   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r1  r   r   r   r   r8   r  r  r  r  r  r  r  r  rx   )rg   r5   r8   r5   r  r  )rK   rL   rM   rN   r   r   r   rh   r  r  r.  r0   r   r   r[   r;  rF  rM  rT  rF   rj  rl  rp  rt  rw  rz  r}   r  r  r  rA   rA   r  rB   r1     s    0Q
97
3
"
&&!!!)))

)r6   r,   r7   r,   r8   r9   )r6   r,   r8   rD   )r6   r,   rH   r,   r8   r9   )\
__future__r   rs   enumloggingrq   typingr8  
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.baser   r   Z_transports.defaultr   r   _typesr   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   Z_urlsr,   r-   Z_utilsr.   r/   TYPE_CHECKINGssl__all__TypeVarr3   r5   rC   r@   rI   rJ   r0   	getLoggerr]  r   r   keysr   EnumrS   rZ   rz   CallableAnyZ	EventHookr~   r2   r1   rA   rA   rA   rB   <module>   sf     <




        N