U
    n/e                     @   sr   d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm	Z	 G dd dee j
ZG dd dejZdd	 ZdS )
    N)IncompleteReadErrorReadTimeoutErrorResponseStreamingError)parsersc                   @   s   e Zd ZdS )AioReadTimeoutErrorN)__name__
__module____qualname__ r
   r
   8/tmp/pip-unpacked-wheel-8mnez3y6/aiobotocore/response.pyr      s   r   c                       s   e Zd ZdZdZejed fddZdd Z	dd	 Z
d
d ZdddZdd Zdd Zdd ZeZedfddZefddZdd Zdd Z  ZS )StreamingBodyad  Wrapper class for an http response body.

    This provides a few additional conveniences that do not exist
    in the urllib3 model:

        * Set the timeout on the socket (i.e read() timeouts)
        * Auto validation of content length, if the amount of bytes
          we read does not match the content length, an exception
          is raised.
    i   )
raw_streamcontent_lengthc                    s   t  | || _d| _d S )Nr   )super__init___self_content_length_self_amount_read)selfr   r   	__class__r
   r   r   !   s    zStreamingBody.__init__c                    s   | j  I d H S N)__wrapped__
__aenter__r   r
   r
   r   r   '   s    zStreamingBody.__aenter__c                    s   | j |||I d H S r   )r   	__aexit__)r   exc_typeexc_valexc_tbr
   r
   r   r   *   s    zStreamingBody.__aexit__c                 C   s
   |    S r   )Zat_eofr   r
   r
   r   readable/   s    zStreamingBody.readableNc              
      s   z$| j j|dk	r|ndI dH }W nb tjk
rX } zt| j j|dW 5 d}~X Y n0 tjj	k
r } zt
|dW 5 d}~X Y nX |  jt|7  _|dks|s|dkr|   |S )zhRead at most amt bytes from the stream.

        If the amt argument is omitted, read all data.
        N)Zendpoint_urlerror)r    r   )r   contentreadasyncioTimeoutErrorr   urlaiohttpZclient_exceptionsZClientConnectionErrorr   r   len_verify_content_length)r   amtchunker
   r
   r   r"   2   s     zStreamingBody.readc                    s   dd |   2 I d H }|S )Nc                    s   g | z3 d H W }|q6 S r   r
   ).0liner
   r
   r   
<listcomp>M   s     z+StreamingBody.readlines.<locals>.<listcomp>)
iter_lines)r   linesr
   r
   r   	readlinesK   s    zStreamingBody.readlinesc                 C   s   |  | jS )z:Return an iterator to yield 1k chunks from the raw stream.)iter_chunks_DEFAULT_CHUNK_SIZEr   r
   r
   r   	__aiter__P   s    zStreamingBody.__aiter__c                    s"   |  | jI dH }|r|S tdS )z-Return the next 1k chunk from the raw stream.N)r"   r3   StopAsyncIteration)r   current_chunkr
   r
   r   	__anext__T   s    zStreamingBody.__anext__Fc                 C  sn   d}|  |2 zD3 dH W }|| d}|dd D ]}||d V  q4|d }q6 |rj||d V  dS )zReturn an iterator to yield lines from the raw stream.

        This is achieved by reading chunk of bytes (of size chunk_size) at a
        time from the raw stream, and then yielding lines from there.
            NTr   r   )r2   
splitlines)r   
chunk_sizekeependspendingr*   r0   r-   r
   r
   r   r/   ]   s    zStreamingBody.iter_linesc                 C  s&   |  |I dH }|dkrq"|V  q dS )z\Return an iterator to yield chunks of chunk_size bytes from the raw
        stream.
        Nr8   )r"   )r   r:   r6   r
   r
   r   r2   l   s    zStreamingBody.iter_chunksc                 C   s2   | j d k	r.| jt| j kr.t| jt| j dd S )N)Zactual_bytesZexpected_bytes)r   r   intr   r   r
   r
   r   r(   v   s    z$StreamingBody._verify_content_lengthc                 C   s   | j S r   )r   r   r
   r
   r   tell   s    zStreamingBody.tell)N)r   r   r	   __doc__r3   r&   StreamReaderstrr   r   r   r   r"   r1   r4   r7   Zanextr/   r2   r(   r>   __classcell__r
   r
   r   r   r      s   

r   c                    s   | j d }|j|jd}|d dkr6|jI d H |d< n2| jrXt|j|d d|d< n|jI d H |d< t	|}t
|jr||| jI d H }n||| j}||fS )Nprotocol)headersstatus_coderE   i,  bodyrD   zcontent-length)metadatarD   rE   r!   Zhas_streaming_outputr   rawgetr   create_parserr#   iscoroutinefunctionparseZoutput_shape)Zoperation_modelhttp_responserC   Zresponse_dictparserparsedr
   r
   r   get_response   s(    
 

 rP   )r#   r&   Zaiohttp.client_exceptionsZwraptZbotocore.responser   r   r   Zaiobotocorer   r$   r   ZObjectProxyr   rP   r
   r
   r
   r   <module>   s   t