o
    í6Wj}  ã                   @  s  d dl mZ d dlZddlmZmZ ddlmZ ddlm	Z	 ej
r2d 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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Úboolc                  C  s6   zdd l } |  ¡ dkrW dS W dS  ty   Y dS w )Nr   ÚtrioTF)ÚsniffioÚcurrent_async_libraryÚImportError)r   © r   úe/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/httpx/_transports/asgi.pyÚis_running_trio   s   ÿýýr   ÚEventc                  C  s&   t ƒ rdd l} |  ¡ S dd l}| ¡ S )Nr   )r   r   r   Úasyncio)r   r   r   r   r   Ú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__8   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   7   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_exceptionsr   Ú	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   W   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 r…i ‰Y nw ˆ ¡ sŽJ ‚ˆd us”J ‚ˆd usšJ ‚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>o   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 )Nr8   zhttp.disconnectTzhttp.requestr   F)r8   r   Ú	more_body)ÚwaitÚ	__anext__ÚStopAsyncIteration)r   )Úrequest_body_chunksÚrequest_completeÚresponse_completer   r   Úreceive†   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 )Nr8   zhttp.response.startÚstatusr<   Tzhttp.response.bodyr   r   rC   FÚHEAD)ÚgetÚis_setr;   ÚappendÚset)rK   r   rC   )Ú
body_partsr/   rI   Úresponse_headersÚresponse_startedÚstatus_coder   r   Úsend”   s    €
÷z0ASGITransport.handle_async_request.<locals>.sendiô  )r<   Ústream)r
   rB   )rK   rL   r
   r   )Ú
isinstancerX   r   r;   r<   ÚrawÚurlr=   r>   r?   ÚsplitÚqueryÚhostÚportr-   r+   r!   r   r(   Ú	Exceptionr*   rR   rP   r   r   )r   r/   ÚscoperJ   rW   rX   r   )rS   r/   rG   rH   rI   rT   rU   rV   r   Úhandle_async_requestc   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   rb   r   r   r   r   r	   ?   s    û)r
   r   )r
   r   )Ú
__future__r   ÚtypingÚ_modelsr   r   Ú_typesr   Úbaser   ÚTYPE_CHECKINGr   r   ÚUnionr   ÚMutableMappingr,   ÚAnyÚ_MessageÚCallableÚ	AwaitableÚ_ReceiveÚ_Sendr)   Ú__all__r   r   r   r	   r   r   r   r   Ú<module>   s,    ÿÿ

