U
    g6                     @  s  U d Z ddlmZ ddlZddlZddlmZ ejrDddlZddl	Z	ddl
mZmZmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddl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* ej+dddZ,ej+dddZ-ej.ej/e0e0e0f ej/e0e0ej.e1e2f f ej/e0e0de0f f Z3ddgZ4i a5de6d< ddddZ7ej8ddddZ9G dd de%Z:G dd de*Z;G dd de"Z<G dd de)Z=dS ) a  
Custom transports, with nicely configured defaults.

The following additional keyword arguments are currently supported by httpcore...

* uds: str
* local_address: str
* retries: int

Example usages...

# Disable HTTP/2 on a single specific domain.
mounts = {
    "all://": httpx.HTTPTransport(http2=True),
    "all://*example.org": httpx.HTTPTransport()
}

# Using advanced httpcore configuration, with connection retries.
transport = httpx.HTTPTransport(retries=1)
client = httpx.Client(transport=transport)

# Using advanced httpcore configuration, with unix domain sockets.
transport = httpx.HTTPTransport(uds="socket.uds")
client = httpx.Client(transport=transport)
    )annotationsN)TracebackType   )DEFAULT_LIMITSLimitsProxycreate_ssl_context)ConnectErrorConnectTimeoutLocalProtocolErrorNetworkErrorPoolTimeoutProtocolError
ProxyError	ReadErrorReadTimeoutRemoteProtocolErrorTimeoutExceptionUnsupportedProtocol
WriteErrorWriteTimeout)RequestResponse)AsyncByteStream	CertTypes
ProxyTypesSyncByteStream)URL   )AsyncBaseTransportBaseTransportTHTTPTransport)boundAAsyncHTTPTransportz,dict[type[Exception], type[httpx.HTTPError]]HTTPCORE_EXC_MAPreturnc                  C  s`   dd l } | jt| jt| jt| jt| jt| jt| jt| jt| j	t	| j
t
| jt| jt| jt| jtiS Nr   )httpcorer   r
   r   r   r   r   r	   r   r   r   r   r   r   r   )r*    r+   =/tmp/pip-unpacked-wheel-ql4n0x43/httpx/_transports/default.py_load_httpcore_exceptionsJ   s<                  r-   ztyping.Iterator[None]c               
   c  s   t tdkrt az
d V  W nv tk
r }  zXd }t D ]*\}}t| |sNq:|d ks`t||r:|}q:|d krp t| }||| W 5 d } ~ X Y nX d S r)   )lenr&   r-   	Exceptionitems
isinstance
issubclassstr)excZ
mapped_excZfrom_excZto_excmessager+   r+   r,   map_httpcore_exceptions_   s    

r6   c                   @  s8   e Zd ZdddddZdddd	Zddd
dZdS )ResponseStreamztyping.Iterable[bytes]Nonehttpcore_streamr(   c                 C  s
   || _ d S N_httpcore_streamselfr:   r+   r+   r,   __init__z   s    zResponseStream.__init__ztyping.Iterator[bytes]r'   c              	   c  s(   t   | jD ]
}|V  qW 5 Q R X d S r;   r6   r=   r?   partr+   r+   r,   __iter__}   s    
zResponseStream.__iter__c                 C  s   t | jdr| j  d S )Nclose)hasattrr=   rE   r?   r+   r+   r,   rE      s    zResponseStream.closeN)__name__
__module____qualname__r@   rD   rE   r+   r+   r+   r,   r7   y   s   r7   c                   @  s   e Zd Zdddddedddddfdddd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dZdddddZdd d!d"ZdS )$r"   TNFr   ssl.SSLContext | str | boolCertTypes | Noneboolr   ProxyTypes | None
str | Noneint%typing.Iterable[SOCKET_OPTION] | Noner8   verifycert	trust_envhttp1http2limitsproxyudslocal_addressretriessocket_optionsr(   c                 C  sT  dd l }t|ttfr t|dn|}t|||d}|d krb|j||j|j|j	||||	|
|d
| _
n|jjdkr|j|j|jj|jj|jj|jjd|j|jj||j|j|j|j	|||d| _
n|jjdkr<zdd l}W n tk
r   td	d Y nX |j|j|jj|jj|jj|jjd|j||j|j|j	||d
| _
ntd|jjdd S )Nr   urlrS   rT   rU   
ssl_contextmax_connectionsmax_keepalive_connectionskeepalive_expiryrV   rW   rZ   r[   r\   r]   httphttpsschemehostporttarget)	proxy_url
proxy_authproxy_headersrb   proxy_ssl_contextrc   rd   re   rV   rW   r]   Zsocks5Zsocks5h{Using SOCKS proxy, but the 'socksio' package is not installed. Make sure to install httpx using `pip install httpx[socks]`.rn   ro   rb   rc   rd   re   rV   rW   zOProxy protocol must be either 'http', 'https', 'socks5', or 'socks5h', but got .)r*   r1   r3   r   r   r   ConnectionPoolrc   rd   re   _poolr_   rj   Z	HTTPProxy
raw_schemeraw_hostrl   raw_pathraw_authheadersrawrb   socksioImportErrorZ
SOCKSProxy
ValueErrorr?   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r*   rb   r~   r+   r+   r,   r@      s|    


zHTTPTransport.__init__r!   r?   r(   c                 C  s   | j   | S r;   )rw   	__enter__rG   r+   r+   r,   r      s    
zHTTPTransport.__enter__type[BaseException] | NoneBaseException | NoneTracebackType | Noneexc_type	exc_value	tracebackr(   c              	   C  s&   t   | j||| W 5 Q R X d S r;   )r6   rw   __exit__r?   r   r   r   r+   r+   r,   r      s    zHTTPTransport.__exit__r   r   requestr(   c              	   C  s   t |jtstdd l}|j|j|j|jj	|jj
|jj|jjd|jj|j|jd}t  | j|}W 5 Q R X t |jtjstt|j|jt|j|jdS Nr   ri   )methodr_   r|   content
extensions)status_coder|   streamr   )r1   r   r   AssertionErrorr*   r   r   r   r_   rx   ry   rl   rz   r|   r}   r   r6   rw   handle_requesttypingIterabler   statusr7   r?   r   r*   reqrespr+   r+   r,   r      s.    zHTTPTransport.handle_requestr'   c                 C  s   | j   d S r;   )rw   rE   rG   r+   r+   r,   rE     s    zHTTPTransport.close)NNN)	rH   rI   rJ   r   r@   r   r   r   rE   r+   r+   r+   r,   r"      s&   &Q   	c                   @  s8   e Zd ZdddddZdddd	Zddd
dZdS )AsyncResponseStreamztyping.AsyncIterable[bytes]r8   r9   c                 C  s
   || _ d S r;   r<   r>   r+   r+   r,   r@   
  s    zAsyncResponseStream.__init__ztyping.AsyncIterator[bytes]r'   c              	   C s2   t  " | j2 z3 d H W }|V  q6 W 5 Q R X d S r;   rA   rB   r+   r+   r,   	__aiter__  s    zAsyncResponseStream.__aiter__c                   s    t | jdr| j I d H  d S )Naclose)rF   r=   r   rG   r+   r+   r,   r     s    zAsyncResponseStream.acloseN)rH   rI   rJ   r@   r   r   r+   r+   r+   r,   r   	  s   r   c                   @  s   e Zd Zdddddedddddfdddd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dZdddddZdd d!d"ZdS )$r%   TNFr   rK   rL   rM   r   rN   rO   rP   rQ   r8   rR   c                 C  sH  dd l }t|ttfr t|dn|}t|||d}|d krb|j||j|j|j	||||	|
|d
| _
n|jjdkr|j|j|jj|jj|jj|jjd|j|jj|j||j|j|j	|||d| _
n|jjdkr<zdd l}W n tk
r   td	d Y nX |j|j|jj|jj|jj|jjd|j||j|j|j	||d
| _
ntdd S )Nr   r^   r`   ra   rf   ri   )rn   ro   rp   rq   rb   rc   rd   re   rV   rW   r]   rr   rs   rt   zdProxy protocol must be either 'http', 'https', 'socks5', or 'socks5h', but got {proxy.url.scheme!r}.)r*   r1   r3   r   r   r   ZAsyncConnectionPoolrc   rd   re   rw   r_   rj   ZAsyncHTTPProxyrx   ry   rl   rz   r{   r|   r}   rb   r~   r   ZAsyncSOCKSProxyr   r   r+   r+   r,   r@     s|    


zAsyncHTTPTransport.__init__r$   r   c                   s   | j  I d H  | S r;   )rw   
__aenter__rG   r+   r+   r,   r   i  s    zAsyncHTTPTransport.__aenter__r   r   r   r   c              	     s,   t   | j|||I d H  W 5 Q R X d S r;   )r6   rw   	__aexit__r   r+   r+   r,   r   m  s    zAsyncHTTPTransport.__aexit__r   r   r   c              	     s   t |jtstdd l}|j|j|j|jj	|jj
|jj|jjd|jj|j|jd}t  | j|I d H }W 5 Q R X t |jtjstt|j|jt|j|jdS r   )r1   r   r   r   r*   r   r   r   r_   rx   ry   rl   rz   r|   r}   r   r6   rw   handle_async_requestr   AsyncIterabler   r   r   r   r+   r+   r,   r   v  s.    z'AsyncHTTPTransport.handle_async_requestr'   c                   s   | j  I d H  d S r;   )rw   r   rG   r+   r+   r,   r     s    zAsyncHTTPTransport.aclose)NNN)	rH   rI   rJ   r   r@   r   r   r   r   r+   r+   r+   r,   r%     s&   &Q   	)>__doc__
__future__r   
contextlibr   typesr   TYPE_CHECKINGsslZhttpx_configr   r   r   r   _exceptionsr	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Z_modelsr   r   _typesr   r   r   r   Z_urlsr   baser   r    TypeVarr!   r$   UnionTuplerP   bytes	bytearrayZSOCKET_OPTION__all__r&   __annotations__r-   contextmanagerr6   r7   r"   r   r%   r+   r+   r+   r,   <module>   s>   @ 