o
    7'h›C  ã                   @  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mZ ddl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mZmZ d	d
lmZ d	dlmZmZ G dd„ dƒZG dd„ deƒZG dd„ dƒZdS )é    )ÚannotationsNé   )ÚAutoBackend)ÚSOCKET_OPTIONÚAsyncNetworkBackend)ÚConnectionNotAvailableÚUnsupportedProtocol)ÚOriginÚProxyÚRequestÚResponse)Ú
AsyncEventÚAsyncShieldCancellationÚAsyncThreadLocké   )ÚAsyncHTTPConnection)ÚAsyncConnectionInterfaceÚAsyncRequestInterfacec                   @  sB   e Zd Zddd„Zdd	d
„Zddd„Z	dddd„Zddd„ZdS )ÚAsyncPoolRequestÚrequestr   ÚreturnÚNonec                 C  s   || _ d | _tƒ | _d S ©N)r   Ú
connectionr   Ú_connection_acquired)Úselfr   © r   úUC:\Users\shubh\Videos\agent\nova\Lib\site-packages\httpcore/_async/connection_pool.pyÚ__init__   s   zAsyncPoolRequest.__init__r   úAsyncConnectionInterface | Nonec                 C  s   || _ | j ¡  d S r   )r   r   Úset)r   r   r   r   r   Úassign_to_connection   s   z%AsyncPoolRequest.assign_to_connectionc                 C  s   d | _ tƒ | _d S r   )r   r   r   ©r   r   r   r   Úclear_connection   s   z!AsyncPoolRequest.clear_connectionNÚtimeoutúfloat | Noner   c                 Ã  s4   �| j d u r| jj|d�I d H  | j d usJ ‚| j S )N©r$   )r   r   Úwait)r   r$   r   r   r   Úwait_for_connection   s
   €
z$AsyncPoolRequest.wait_for_connectionÚboolc                 C  s
   | j d u S r   )r   r"   r   r   r   Ú	is_queued'   s   
zAsyncPoolRequest.is_queued)r   r   r   r   )r   r   r   r   ©r   r   r   )r$   r%   r   r   )r   r)   )Ú__name__Ú
__module__Ú__qualname__r   r!   r#   r(   r*   r   r   r   r   r      s    


ÿr   c                   @  sš   e Zd ZdZ												dAdBdd„ZdCd#d$„ZedDd&d'„ƒZdEd+d,„ZdDd-d.„Z	dFd0d1„Z
dGd2d3„ZdHd4d5„Z			dIdJd<d=„ZdKd?d@„ZdS )LÚAsyncConnectionPoolz5
    A connection pool for making HTTP requests.
    Né
   TFr   Ússl_contextússl.SSLContext | NoneÚproxyúProxy | NoneÚmax_connectionsú
int | NoneÚmax_keepalive_connectionsÚkeepalive_expiryr%   Úhttp1r)   Úhttp2ÚretriesÚintÚlocal_addressú
str | NoneÚudsÚnetwork_backendúAsyncNetworkBackend | NoneÚsocket_optionsú%typing.Iterable[SOCKET_OPTION] | Noner   r   c                 C  sš   || _ || _|du rtjn|| _|du rtjn|| _t| j| jƒ| _|| _|| _|| _	|| _
|	| _|
| _|du r;tƒ n|| _|| _g | _g | _tƒ | _dS )a  
        A connection pool for making HTTP requests.

        Parameters:
            ssl_context: An SSL context to use for verifying connections.
                If not specified, the default `httpcore.default_ssl_context()`
                will be used.
            max_connections: The maximum number of concurrent HTTP connections that
                the pool should allow. Any attempt to send a request on a pool that
                would exceed this amount will block until a connection is available.
            max_keepalive_connections: The maximum number of idle HTTP connections
                that will be maintained in the pool.
            keepalive_expiry: The duration in seconds that an idle HTTP connection
                may be maintained for before being expired from the pool.
            http1: A boolean indicating if HTTP/1.1 requests should be supported
                by the connection pool. Defaults to True.
            http2: A boolean indicating if HTTP/2 requests should be supported by
                the connection pool. Defaults to False.
            retries: The maximum number of retries when trying to establish a
                connection.
            local_address: Local address to connect from. Can also be used to connect
                using a particular address family. Using `local_address="0.0.0.0"`
                will connect using an `AF_INET` address (IPv4), while using
                `local_address="::"` will connect using an `AF_INET6` address (IPv6).
            uds: Path to a Unix Domain Socket to use instead of TCP sockets.
            network_backend: A backend instance to use for handling network I/O.
            socket_options: Socket options that have to be included
             in the TCP socket when the connection was established.
        N)Ú_ssl_contextÚ_proxyÚsysÚmaxsizeÚ_max_connectionsÚ_max_keepalive_connectionsÚminÚ_keepalive_expiryÚ_http1Ú_http2Ú_retriesÚ_local_addressÚ_udsr   Ú_network_backendÚ_socket_optionsÚ_connectionsÚ	_requestsr   Ú_optional_thread_lock)r   r1   r3   r5   r7   r8   r9   r:   r;   r=   r?   r@   rB   r   r   r   r   0   s.   ,ÿÿýÿÿzAsyncConnectionPool.__init__Úoriginr	   r   c                 C  sø   | j d ure| j jjdv r(ddlm} || j jj| j j|| j| j| j	| j
| jd�S |jdkrFddlm} || j jj| j j| j j|| j| jd�S ddlm} || j jj| j j| j j|| j| j| j	| j
| jd	�	S t|| j| j| j	| j
| j| j| j| j| jd
�
S )N)s   socks5s   socks5hr   )ÚAsyncSocks5Connection)Úproxy_originÚ
proxy_authÚremote_originr1   r8   r9   r:   r@   s   http)ÚAsyncForwardHTTPConnection)rX   Úproxy_headersÚproxy_ssl_contextrZ   r8   r@   )ÚAsyncTunnelHTTPConnection)	rX   r\   r]   rZ   r1   r8   r9   r:   r@   )
rV   r1   r8   r9   r:   r;   r=   r?   r@   rB   )rE   ÚurlÚschemeZsocks_proxyrW   rV   ÚauthrD   rK   rL   rM   rQ   Z
http_proxyr[   Úheadersr1   r^   r   rN   rO   rP   rR   )r   rV   rW   r[   r^   r   r   r   Úcreate_connection€   s^   
ø

ú÷öz%AsyncConnectionPool.create_connectionúlist[AsyncConnectionInterface]c                 C  s
   t | jƒS )aÓ  
        Return a list of the connections currently in the pool.

        For example:

        ```python
        >>> pool.connections
        [
            <AsyncHTTPConnection ['https://example.com:443', HTTP/1.1, ACTIVE, Request Count: 6]>,
            <AsyncHTTPConnection ['https://example.com:443', HTTP/1.1, IDLE, Request Count: 9]> ,
            <AsyncHTTPConnection ['http://example.com:80', HTTP/1.1, IDLE, Request Count: 1]>,
        ]
        ```
        )ÚlistrS   r"   r   r   r   Úconnectionsµ   s   
zAsyncConnectionPool.connectionsr   r   r   c           
      Ã  s´  �|j j ¡ }|dkrtdƒ‚|dvrtd|› d�ƒ‚|j di ¡}| dd¡}| j� t|ƒ}| j 	|¡ W d  ƒ n1 s@w   Y  zF	 | j� |  
¡ }W d  ƒ n1 sYw   Y  |  |¡I dH  |j|d
�I dH }z| |j¡I dH }W n ty‡   | ¡  Y nw nqGW n4 ty¿ }	 z(| j� | j |¡ |  
¡ }W d  ƒ n1 s«w   Y  |  |¡I dH  |	d‚d}	~	ww t|jtjƒsÉJ ‚t|j|jt|j|| d�|jd�S )z¡
        Send an HTTP request, and return an HTTP response.

        This is the core implementation that is called into by `.request()` or `.stream()`.
        Ú z;Request URL is missing an 'http://' or 'https://' protocol.)ÚhttpÚhttpsÚwsÚwssz)Request URL has an unsupported protocol 'z://'.r$   ÚpoolNTr&   )ÚstreamÚpool_requestrl   )Ústatusrb   ÚcontentÚ
extensions)r_   r`   Údecoder   rq   ÚgetrU   r   rT   ÚappendÚ_assign_requests_to_connectionsÚ_close_connectionsr(   Úhandle_async_requestr   r   r#   ÚBaseExceptionÚremoveÚ
isinstancerm   ÚtypingÚAsyncIterabler   ro   rb   ÚPoolByteStream)
r   r   r`   Ztimeoutsr$   rn   Úclosingr   ÚresponseÚexcr   r   r   rw   Ç   sf   €ÿ
ÿý
ýÿûê
ü€øÿúz(AsyncConnectionPool.handle_async_requestc                   sZ  g }t | jƒD ]9}| ¡ r| j |¡ q| ¡ r$| j |¡ | |¡ q| ¡ r@tdd„ | jD ƒƒ| jkr@| j |¡ | |¡ qdd„ | j	D ƒ}|D ]_}|j
jj‰ ‡ fdd„| jD ƒ}dd„ | jD ƒ}|rp|d }| |¡ qKt| jƒ| jk r‰|  ˆ ¡}| j |¡ | |¡ qK|rª|d }| j |¡ | |¡ |  ˆ ¡}| j |¡ | |¡ qK|S )aE  
        Manage the state of the connection pool, assigning incoming
        requests to connections as available.

        Called whenever a new request is added or removed from the pool.

        Any closing connections are returned, allowing the I/O for closing
        those connections to be handled seperately.
        c                 S  ó   g | ]}|  ¡ ‘qS r   ©Úis_idle©Ú.0r   r   r   r   Ú
<listcomp>&  ó    zGAsyncConnectionPool._assign_requests_to_connections.<locals>.<listcomp>c                 S  ó   g | ]}|  ¡ r|‘qS r   ©r*   ©r…   r   r   r   r   r†   .  s    c                   s"   g | ]}|  ˆ ¡r| ¡ r|‘qS r   )Zcan_handle_requestZis_availabler„   ©rV   r   r   r†   1  s    ýýÿc                 S  rˆ   r   r‚   r„   r   r   r   r†   6  s
    ÿÿr   )re   rS   Ú	is_closedry   Zhas_expiredrt   rƒ   ÚlenrI   rT   r   r_   rV   r!   rH   rc   )r   Úclosing_connectionsr   Zqueued_requestsrn   Zavailable_connectionsZidle_connectionsr   r‹   r   ru     sP   
ÿÿ
€

þÿ




€z3AsyncConnectionPool._assign_requests_to_connectionsr~   c                 Ã  sF   �t ƒ � |D ]	}| ¡ I d H  qW d   ƒ d S 1 sw   Y  d S r   )r   Úaclose)r   r~   r   r   r   r   rv   U  s   €ÿ"ÿz&AsyncConnectionPool._close_connectionsc                 Ã  sL   �| j � t| jƒ}g | _W d   ƒ n1 sw   Y  |  |¡I d H  d S r   )rU   re   rS   rv   )r   rŽ   r   r   r   r�   [  s   €
þzAsyncConnectionPool.aclosec                 Ã  s   �| S r   r   r"   r   r   r   Ú
__aenter__c  s   €zAsyncConnectionPool.__aenter__Úexc_typeútype[BaseException] | NoneÚ	exc_valueúBaseException | NoneÚ	tracebackútypes.TracebackType | Nonec                 Ã  s   �|   ¡ I d H  d S r   )r�   )r   r‘   r“   r•   r   r   r   Ú	__aexit__f  s   €zAsyncConnectionPool.__aexit__Ústrc           
      C  s²   | j j}| j�, dd„ | jD ƒ}dd„ | jD ƒ}| d¡}| d¡}| d¡}| d¡}W d   ƒ n1 s6w   Y  d|› d|› d�}d	|› d|› d
�}	d|› d|› d|	› d�S )Nc                 S  r�   r   r‰   rŠ   r   r   r   r†   q  r‡   z0AsyncConnectionPool.__repr__.<locals>.<listcomp>c                 S  r�   r   r‚   r„   r   r   r   r†   r  s    ÿFTz
Requests: z	 active, z queuedzConnections: z idleú<z [z | z]>)Ú	__class__r,   rU   rT   rS   Úcount)
r   Ú
class_nameZrequest_is_queuedZconnection_is_idleZnum_active_requestsZnum_queued_requestsZnum_active_connectionsZnum_idle_connectionsZrequests_infoZconnection_infor   r   r   Ú__repr__n  s    ÿ


÷ÿÿzAsyncConnectionPool.__repr__)NNr0   NNTFr   NNNN)r1   r2   r3   r4   r5   r6   r7   r6   r8   r%   r9   r)   r:   r)   r;   r<   r=   r>   r?   r>   r@   rA   rB   rC   r   r   )rV   r	   r   r   )r   rd   )r   r   r   r   )r~   rd   r   r   r+   )r   r/   )NNN)r‘   r’   r“   r”   r•   r–   r   r   )r   r˜   )r,   r-   r.   Ú__doc__r   rc   Úpropertyrf   rw   ru   rv   r�   r�   r—   r�   r   r   r   r   r/   +   s8    ó
P5

G
G

ür/   c                   @  s*   e Zd Zdd	d
„Zddd„Zddd„ZdS )r}   rm   útyping.AsyncIterable[bytes]rn   r   rl   r/   r   r   c                 C  s   || _ || _|| _d| _d S )NF)Ú_streamÚ_pool_requestÚ_poolÚ_closed)r   rm   rn   rl   r   r   r   r   †  s   
zPoolByteStream.__init__útyping.AsyncIterator[bytes]c              
   C sR   �z| j 2 z	3 d H W }|V  q6 W d S  ty( } z
|  ¡ I d H  |d ‚d }~ww r   )r¡   rx   r�   )r   Úpartr€   r   r   r   Ú	__aiter__‘  s   €ÿ€þzPoolByteStream.__aiter__c                 Ã  s¬   �| j sTd| _ tƒ � t| jdƒr| j ¡ I d H  W d   ƒ n1 s#w   Y  | jj� | jj | j	¡ | j 
¡ }W d   ƒ n1 sDw   Y  | j |¡I d H  d S d S )NTr�   )r¤   r   Úhasattrr¡   r�   r£   rU   rT   ry   r¢   ru   rv   )r   r~   r   r   r   r�   ™  s   €€þ
þözPoolByteStream.acloseN)rm   r    rn   r   rl   r/   r   r   )r   r¥   r+   )r,   r-   r.   r   r§   r�   r   r   r   r   r}   …  s    

r}   )Ú
__future__r   ÚsslrF   Útypesr{   Z_backends.autor   Z_backends.baser   r   Ú_exceptionsr   r   Z_modelsr	   r
   r   r   Z_synchronizationr   r   r   r   r   Z
interfacesr   r   r   r/   r}   r   r   r   r   Ú<module>   s"      \