o
    VÌhÌ  ã                   @   sD   d Z ddlmZ ddlmZmZ G dd„ deƒZG dd„ deƒZd	S )
a  

requests_toolbelt.streaming_iterator
====================================

This holds the implementation details for the :class:`StreamingIterator`. It
is designed for the case where you, the user, know the size of the upload but
need to provide the data as an iterator. This class will allow you to specify
the size and stream the data without using a chunked transfer-encoding.

é    )Ú	super_lené   )ÚCustomBytesIOÚencode_withc                   @   s$   e Zd ZdZd	dd„Zd
dd„ZdS )ÚStreamingIteratora  
    This class provides a way of allowing iterators with a known size to be
    streamed instead of chunked.

    In requests, if you pass in an iterator it assumes you want to use
    chunked transfer-encoding to upload the data, which not all servers
    support well. Additionally, you may want to set the content-length
    yourself to avoid this but that will not work. The only way to preempt
    requests using a chunked transfer-encoding and forcing it to stream the
    uploads is to mimic a very specific interace. Instead of having to know
    these details you can instead just use this class. You simply provide the
    size and iterator and pass the instance of StreamingIterator to requests
    via the data parameter like so:

    .. code-block:: python

        from requests_toolbelt import StreamingIterator

        import requests

        # Let iterator be some generator that you already have and size be
        # the size of the data produced by the iterator

        r = requests.post(url, data=StreamingIterator(size, iterator))

    You can also pass file-like objects to :py:class:`StreamingIterator` in
    case requests can't determize the filesize itself. This is the case with
    streaming file objects like ``stdin`` or any sockets. Wrapping e.g. files
    that are on disk with ``StreamingIterator`` is unnecessary, because
    requests can determine the filesize itself.

    Naturally, you should also set the `Content-Type` of your upload
    appropriately because the toolbelt will not attempt to guess that for you.
    úutf-8c                 C   sT   t |ƒ| _| jdk rtdƒ‚| j| _|| _|| _t|dƒr"|| _d S t||ƒ| _d S )Nr   z1The size of the upload must be a positive integerÚread)	ÚintÚsizeÚ
ValueErrorÚlenÚencodingÚiteratorÚhasattrÚ_fileÚ_IteratorAsBinaryFile)Úselfr
   r   r   © r   úx/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/requests_toolbelt/streaming_iterator.pyÚ__init__7   s   

ÿ

zStreamingIterator.__init__éÿÿÿÿc                 C   s   t | j |¡| jƒS ©N)r   r   r   r   ©r   r
   r   r   r   r   O   s   zStreamingIterator.readN©r   ©r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s    
#r   c                   @   s0   e Zd Zddd„Zdd„ Zdd„ Zdd	d
„ZdS )r   r   c                 C   s   || _ || _tƒ | _d S r   )r   r   r   Ú_buffer)r   r   r   r   r   r   r   T   s   z_IteratorAsBinaryFile.__init__c                 C   s*   z
t t| jƒ| jƒW S  ty   Y dS w )Nó    )r   Únextr   r   ÚStopIteration)r   r   r   r   Ú
_get_bytes_   s
   ÿz _IteratorAsBinaryFile._get_bytesc                 C   s\   | j  ¡  |t| j ƒ }d}|dkr(|r,|  ¡ }|| j  |¡8 }|dkr*|sd S d S d S d S )NTr   )r   Úsmart_truncater   r#   Úappend)r   r
   Úamount_to_loadÚbytes_to_appendr   r   r   Ú_load_bytese   s   
þz!_IteratorAsBinaryFile._load_bytesr   c                 C   s2   t |ƒ}|dkrd | j¡S |  |¡ | j |¡S )Nr   r    )r	   Újoinr   r(   r   r   r   r   r   r   r   n   s
   
z_IteratorAsBinaryFile.readNr   r   )r   r   r   r   r#   r(   r   r   r   r   r   r   S   s
    
	r   N)	r   Úrequests.utilsr   Úmultipart.encoderr   r   Úobjectr   r   r   r   r   r   Ú<module>   s
   A