o
    è’Oj¹  ã                   @   s’   d dl Z d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZmZ d dlmZmZmZ d dlmZ e  e¡ZG d	d
„ d
eƒZdd„ ZdS )é    N)ÚIOBase)ÚProtocolError)ÚReadTimeoutError)ÚScalarTypesÚparsers)ÚXMLParseErrorÚset_socket_timeout)ÚIncompleteReadErrorr   ÚResponseStreamingError)Úfirst_non_none_responsec                   @   s¤   e Zd ZdZdZ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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d#d$„ ZdS )&ÚStreamingBodyae  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   c                 C   s   || _ || _d| _d S )Nr   )Ú_raw_streamÚ_content_lengthÚ_amount_read)ÚselfÚ
raw_streamÚcontent_length© r   úW/var/www/html/pharmsmart-cdr-gen/venv/lib/python3.10/site-packages/botocore/response.pyÚ__init__6   s   
zStreamingBody.__init__c                 C   s   d S ©Nr   ©r   r   r   r   Ú__del__;   s   zStreamingBody.__del__c                 C   s.   z	t | j|ƒ W dS  ty   t d¡ ‚ w )z&Set the timeout seconds on the socket.zaCannot access the socket object of a streaming response. It's possible the interface has changed.N)r   r   ÚAttributeErrorÚloggerÚ	exception)r   Útimeoutr   r   r   r   A   s   ÿûz StreamingBody.set_socket_timeoutc                 C   s"   z| j  ¡ W S  ty   Y dS w )NF)r   Úreadabler   r   r   r   r   r   V   s
   ÿzStreamingBody.readableNc              
   C   sŠ   z| j  |¡}W n# ty } zt|j|d�‚d}~w ty+ } zt|d�‚d}~ww |  jt|ƒ7  _|du s?|sC|dkrC|  	¡  |S )zhRead at most amt bytes from the stream.

        If the amt argument is omitted, read all data.
        ©Úendpoint_urlÚerrorN©r    r   )
r   ÚreadÚURLLib3ReadTimeoutErrorr   ÚurlÚURLLib3ProtocolErrorr
   r   ÚlenÚ_verify_content_length)r   ÚamtÚchunkÚer   r   r   r"   \   s   €
€ÿzStreamingBody.readc              
   C   s†   z| j  |¡}W n# ty } zt|j|d�‚d}~w ty+ } zt|d�‚d}~ww |  j|7  _|dkrAt|ƒdkrA|  	¡  |S )zcRead bytes into a pre-allocated, writable bytes-like object b, and return the number of bytes read.r   Nr!   r   )
r   Úreadintor#   r   r$   r%   r
   r   r&   r'   )r   ÚbÚamount_readr*   r   r   r   r+   p   s   €
€ÿzStreamingBody.readintoc                 C   ó
   | j  ¡ S r   )r   Ú	readlinesr   r   r   r   r/   €   ó   
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   Ú__iter__ƒ   s   zStreamingBody.__iter__c                 C   s   |   | j¡}|r
|S tƒ ‚)z-Return the next 1k chunk from the raw stream.)r"   r2   ÚStopIteration)r   Úcurrent_chunkr   r   r   Ú__next__‡   s   zStreamingBody.__next__c                 C   s   | j S r   )r   r   r   r   r   Ú	__enter__Ž   s   zStreamingBody.__enter__c                 C   s   | j  ¡  d S r   ©r   Úclose)r   ÚtypeÚvalueÚ	tracebackr   r   r   Ú__exit__‘   s   zStreamingBody.__exit__Fc                 c   sj   � d}|   |¡D ]}||  d¡}|dd… D ]
}| |¡d V  q|d }q|r3| |¡d V  dS dS )zÓReturn 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.
        ó    TNéÿÿÿÿr   )r1   Ú
splitlines)r   Ú
chunk_sizeÚkeependsÚpendingr)   ÚlinesÚliner   r   r   Ú
iter_lines–   s   €
ÿzStreamingBody.iter_linesc                 c   s"   � 	 |   |¡}|dkrdS |V  q)z\Return an iterator to yield chunks of chunk_size bytes from the raw
        stream.
        Tr>   N)r"   )r   rA   r5   r   r   r   r1   ¥   s   €
üzStreamingBody.iter_chunksc                 C   s6   | j d ur| jt| j ƒkrt| jt| j ƒd�‚d S d S )N)Úactual_bytesÚexpected_bytes)r   r   Úintr	   r   r   r   r   r'   ¯   s   ÿþýz$StreamingBody._verify_content_lengthc                 C   r.   r   )r   Útellr   r   r   r   rJ   »   r0   zStreamingBody.tellc                 C   s   | j  ¡  dS )z*Close the underlying http response stream.Nr8   r   r   r   r   r9   ¾   s   zStreamingBody.closer   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r   r   r   r   r"   r+   r/   r3   r6   r7   r=   ÚnextrF   r1   r'   rJ   r9   r   r   r   r   r   '   s(    

r   c                 C   sv   | j j}|j|jdœ}|d dkr|j|d< n| jr(t|j|d  d¡ƒ|d< n|j|d< t	 
|¡}|| || j¡fS )N)ÚheadersÚstatus_coderQ   i,  ÚbodyrP   zcontent-length)Úservice_modelÚresolved_protocolrP   rQ   ÚcontentÚhas_streaming_outputr   ÚrawÚgetr   Úcreate_parserÚparseÚoutput_shape)Úoperation_modelÚhttp_responseÚprotocolÚresponse_dictÚparserr   r   r   Úget_responseÃ   s   þ
ÿ

ÿra   )ÚloggingÚior   Úurllib3.exceptionsr   r%   r   r#   Úbotocorer   r   Úbotocore.compatr   r   Úbotocore.exceptionsr	   r
   Úbotocore.hooksr   Ú	getLoggerrK   r   r   ra   r   r   r   r   Ú<module>   s   
 