o
    ?Hhr                     @  s  d dl m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
 ejr6d dlZd dlZejejejf Zejeejf Zejg eje f Zejejeejf gejd f Zejejeejf eegejd f ZdgZdddZG dd deZG dd de
ZdS )    )annotationsN   )RequestResponse)AsyncByteStream   )AsyncBaseTransportASGITransportreturnEventc                  C  s,   t  dkrdd l} |  S dd l}| S )Ntrior   )sniffiocurrent_async_libraryr   r   asyncio)r   r    r   V/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/httpx/_transports/asgi.pycreate_event   s
   r   c                   @  s    e Zd ZdddZddd	Zd
S )ASGIResponseStreambodylist[bytes]r
   Nonec                 C  s
   || _ d S N)_body)selfr   r   r   r   __init__+   s   
zASGIResponseStream.__init__typing.AsyncIterator[bytes]c                 C s   d | jV  d S )N    )joinr   )r   r   r   r   	__aiter__.   s   zASGIResponseStream.__aiter__N)r   r   r
   r   )r
   r   )__name__
__module____qualname__r   r   r   r   r   r   r   *   s    
r   c                   @  s,   e Zd ZdZ			ddddZdddZdS )r	   a  
    A custom AsyncTransport that handles sending requests directly to an ASGI app.

    ```python
    transport = httpx.ASGITransport(
        app=app,
        root_path="/submount",
        client=("1.2.3.4", 123)
    )
    client = httpx.AsyncClient(transport=transport)
    ```

    Arguments:

    * `app` - The ASGI application.
    * `raise_app_exceptions` - Boolean indicating if exceptions in the application
       should be raised. Default to `True`. Can be set to `False` for use cases
       such as testing the content of a client 500 response.
    * `root_path` - The root path on which the ASGI application should be mounted.
    * `client` - A two-tuple indicating the client IP and port of incoming requests.
    ```
    T z	127.0.0.1{   app_ASGIAppraise_app_exceptionsbool	root_pathstrclienttuple[str, int]r
   r   c                 C  s   || _ || _|| _|| _d S r   )r%   r'   r)   r+   )r   r%   r'   r)   r+   r   r   r   r   J   s   
zASGITransport.__init__requestr   r   c                   sJ  t jts	J dddidjdd jjD jjjjjj	
dd jjjjjjf| j| jd	}j d
d d g  d
t dfdd}d fdd}z| |||I d H  W n ty   | jru   d u rdd u ri Y nw  sJ d usJ d usJ t }t|dS )Nhttpversionz3.0z1.1c                 S  s   g | ]
\}}|  |fqS r   )lower).0kvr   r   r   
<listcomp>b   s    z6ASGITransport.handle_async_request.<locals>.<listcomp>   ?r   )typeasgihttp_versionmethodheadersschemepathraw_pathquery_stringserverr+   r)   Fr
   dict[str, typing.Any]c                    s^   r  I d H  ddiS z	  I d H } W n ty(   ddddd Y S w d| ddS )Nr6   zhttp.disconnectTzhttp.requestr   F)r6   r   	more_body)wait	__anext__StopAsyncIteration)r   )request_body_chunksrequest_completeresponse_completer   r   receivey   s   z3ASGITransport.handle_async_request.<locals>.receivemessage&typing.MutableMapping[str, typing.Any]r   c                   s   | d dkrrJ | d |  dg dd S | d dkrE r%J |  dd}|  d	d
}|r=jdkr= | |sG  d S d S d S )Nr6   zhttp.response.startstatusr:   Tzhttp.response.bodyr   r   rA   FHEAD)getis_setr9   appendset)rI   r   rA   )
body_partsr-   rG   response_headersresponse_startedstatus_coder   r   send   s    
z0ASGITransport.handle_async_request.<locals>.sendi  )r:   stream)r
   r@   )rI   rJ   r
   r   )
isinstancerV   r   r9   r:   rawurlr;   r<   r=   splitqueryhostportr+   r)   r   r   r%   	Exceptionr'   rP   rN   r   r   )r   r-   scoperH   rU   rV   r   )rQ   r-   rE   rF   rG   rR   rS   rT   r   handle_async_requestV   sR   

z"ASGITransport.handle_async_requestN)Tr"   r#   )
r%   r&   r'   r(   r)   r*   r+   r,   r
   r   )r-   r   r
   r   )r   r    r!   __doc__r   r`   r   r   r   r   r	   2   s    )r
   r   )
__future__r   typingr   _modelsr   r   _typesr   baser   TYPE_CHECKINGr   r   Unionr   MutableMappingr*   Any_MessageCallable	Awaitable_Receive_Sendr&   __all__r   r   r	   r   r   r   r   <module>   s,    
