U
    9ˆj\  ã                   @   s8   d dl mZ ddlmZ ddlmZ G dd„ deƒZdS )é    )ÚAsyncConsumeré   )Úaclose_old_connections)ÚStopConsumerc                   @   s^   e Zd ZdZdd„ Zdddœdd„Zd	d
œdd„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚAsyncHttpConsumerzf
    Async HTTP consumer. Provides basic primitives for building asynchronous
    HTTP endpoints.
    c                 O   s
   g | _ d S )N)Úbody)ÚselfÚargsÚkwargs© r   ú9/tmp/pip-unpacked-wheel-o8d015gg/channels/generic/http.pyÚ__init__   s    zAsyncHttpConsumer.__init__éÈ   N)ÚstatusÚheadersc                Ã   s@   |dkrg }nt |tƒr$t| ¡ ƒ}|  d||dœ¡I dH  dS )a?  
        Sets the HTTP response status and headers. Headers may be provided as
        a list of tuples or as a dictionary.

        Note that the ASGI spec requires that the protocol server only starts
        sending the response to the client after ``self.send_body`` has been
        called the first time.
        Nzhttp.response.start)Útyper   r   )Ú
isinstanceÚdictÚlistÚitemsÚsend)r   r   r   r   r   r   Úsend_headers   s    	

ÿzAsyncHttpConsumer.send_headersF)Ú	more_bodyc                Ã   s.   t |tƒstdƒ‚|  d||dœ¡I dH  dS )a  
        Sends a response body to the client. The method expects a bytestring.

        Set ``more_body=True`` if you want to send more body content later.
        The default behavior closes the response, and further messages on
        the channel will be ignored.
        zBody is not byteszhttp.response.body)r   r   r   N)r   ÚbytesÚAssertionErrorr   )r   r   r   r   r   r   Ú	send_body"   s    
ÿzAsyncHttpConsumer.send_bodyc                 Ë   s.   | j f d|i|—ŽI dH  |  |¡I dH  dS )zà
        Sends a response to the client. This is a thin wrapper over
        ``self.send_headers`` and ``self.send_body``, and everything said
        above applies here as well. This method may only be called once.
        r   N)r   r   )r   r   r   r
   r   r   r   Úsend_response/   s    zAsyncHttpConsumer.send_responsec                 Ã   s   t dƒ‚dS )z·
        Receives the request body as a bytestring. Response may be composed
        using the ``self.send*`` methods; the return value of this method is
        thrown away.
        z?Subclasses of AsyncHttpConsumer must provide a handle() method.N)ÚNotImplementedError)r   r   r   r   r   Úhandle8   s    ÿzAsyncHttpConsumer.handlec                 Ã   s   dS )zh
        Overrideable place to run disconnect handling. Do not send anything
        from here.
        Nr   )r   r   r   r   Ú
disconnectB   s    zAsyncHttpConsumer.disconnectc                 Ã   sZ   d|kr| j  |d ¡ | d¡sVz|  d | j ¡¡I dH  W 5 |  ¡ I dH  X tƒ ‚dS )z�
        Async entrypoint - concatenates body fragments and hands off control
        to ``self.handle`` when the body has been completely received.
        r   r   Nó    )r   ÚappendÚgetr   r   Újoinr   ©r   Úmessager   r   r   Úhttp_requestI   s    
zAsyncHttpConsumer.http_requestc                 Ã   s$   |   ¡ I dH  tƒ I dH  tƒ ‚dS )zG
        Let the user do their cleanup and close the consumer.
        N)r   r   r   r$   r   r   r   Úhttp_disconnectW   s    z!AsyncHttpConsumer.http_disconnect)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r&   r'   r   r   r   r   r      s   	
r   N)Zchannels.consumerr   Údbr   Ú
exceptionsr   r   r   r   r   r   Ú<module>   s   