U
    g}                     @  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
rdd 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dZdd
ddZG dd deZG dd de	ZdS )    )annotationsN   )RequestResponse)AsyncByteStream   )AsyncBaseTransportASGITransportboolreturnc                  C  s8   zdd l } |  dkrW dS W n tk
r2   Y nX dS )Nr   trioTF)sniffioZcurrent_async_libraryImportError)r    r   :/tmp/pip-unpacked-wheel-ql4n0x43/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ddZdddd	Zd
S )ASGIResponseStreamzlist[bytes]None)bodyr   c                 C  s
   || _ d S N)_body)selfr   r   r   r   __init__8   s    zASGIResponseStream.__init__ztyping.AsyncIterator[bytes]r   c                 C s   d | jV  d S )N    )joinr   )r   r   r   r   	__aiter__;   s    zASGIResponseStream.__aiter__N)__name__
__module____qualname__r   r   r   r   r   r   r   7   s   r   c                   @  s8   e Zd ZdZdddddd	d
ddZdd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{   _ASGIAppr
   strztuple[str, int]r   )appraise_app_exceptions	root_pathclientr   c                 C  s   || _ || _|| _|| _d S r   )r(   r)   r*   r+   )r   r(   r)   r*   r+   r   r   r   r   W   s    zASGITransport.__init__r   r   )requestr   c                   s`  t jtst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dfdd}ddd fdd}z| |||I d H  W nB tk
r   | jr   d krdd kri Y nX  s.td k	s<td k	sJt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   )typeZasgihttp_versionmethodheadersschemepathraw_pathZquery_stringserverr+   r*   Fzdict[str, typing.Any]r   c                    s^   r  I d H  ddiS z  I d H } W n$ tk
rP   ddddd Y S X d| ddS )Nr5   zhttp.disconnectTzhttp.requestr   F)r5   r   	more_body)wait	__anext__StopAsyncIteration)r   )request_body_chunksrequest_completeresponse_completer   r   receive   s    z3ASGITransport.handle_async_request.<locals>.receivez&typing.MutableMapping[str, typing.Any]r   )messager   c                   s   | d dkr.rt | d | dg dnT| d dkr rFt | dd}| d	d
}|rvjdkrv | |s  d S )Nr5   zhttp.response.startstatusr8   Tzhttp.response.bodyr   r   r=   FHEAD)AssertionErrorgetis_setr7   appendset)rE   r   r=   )
body_partsr,   rC   response_headersresponse_startedstatus_coder   r   send   s    
z0ASGITransport.handle_async_request.<locals>.sendi  )r8   stream)
isinstancerR   r   rH   r7   r8   rawurlr9   r:   r;   splitqueryhostportr+   r*   r   r   r(   	Exceptionr)   rL   rJ   r   r   )r   r,   ZscoperD   rQ   rR   r   )rM   r,   rA   rB   rC   rN   rO   rP   r   handle_async_requestc   sL    



z"ASGITransport.handle_async_requestN)Tr#   r$   )r    r!   r"   __doc__r   r[   r   r   r   r   r	   ?   s      )
__future__r   typingZ_modelsr   r   _typesr   baser   TYPE_CHECKINGr   r   Unionr   MutableMappingr'   AnyZ_MessageCallable	AwaitableZ_ReceiveZ_Sendr&   __all__r   r   r   r	   r   r   r   r   <module>   s*   