
    $h                     P    S r SSKJr  SSKJrJr   " S S\5      r " S S\5      rg	)
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                   ,    \ rS rSrSrSS jrSS jrSrg)	StreamingIterator   a  
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.
c                     [        U5      U l        U R                  S:  a  [        S5      eU R                  U l        X0l        X l        [        US5      (       a  X l        g [        X#5      U l        g )Nr   z1The size of the upload must be a positive integerread)	intsize
ValueErrorlenencodingiteratorhasattr_file_IteratorAsBinaryFile)selfr   r   r   s       \/var/www/html/shao/venv/lib/python3.13/site-packages/requests_toolbelt/streaming_iterator.py__init__StreamingIterator.__init__7   sa    I	99q=C  99 ! !8V$$!J.xBDJ    c                 `    [        U R                  R                  U5      U R                  5      $ N)r   r   r   r   r   r   s     r   r   StreamingIterator.readO   s     4::??40$--@@r   )r   r   r   r   r   Nzutf-8)__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__ r   r   r   r      s    !FC0Ar   r   c                   4    \ rS rSrSS jrS rS rS	S jrSrg)
r   S   c                 :    Xl         X l        [        5       U l        g r   )r   r   r   _buffer)r   r   r   s      r   r   _IteratorAsBinaryFile.__init__T   s      ! %r   c                 v     [        [        U R                  5      U R                  5      $ ! [         a     gf = f)Nr   )r   nextr   r   StopIteration)r   s    r   
_get_bytes _IteratorAsBinaryFile._get_bytes_   s3    	tDMM2DMMBB 		s   (+ 
88c                    U R                   R                  5         U[        U R                   5      -
  nSnUS:  aF  U(       a>  U R                  5       nX R                   R	                  U5      -  nUS:  a
  U(       a  M<  g g g g )NTr   )r+   smart_truncater   r0   append)r   r   amount_to_loadbytes_to_appends       r   _load_bytes!_IteratorAsBinaryFile._load_bytese   sm    ##%	$,, 77q _"oo/Oll11/BBN q __ _ r   c                     [        U5      nUS:X  a  SR                  U R                  5      $ U R                  U5        U R                  R                  U5      $ )Nr    r   )r   joinr   r7   r+   r   r   s     r   r   _IteratorAsBinaryFile.readn   sH    4y2:88DMM**||  &&r   )r+   r   r   Nr   r   )	r!   r"   r#   r$   r   r0   r7   r   r&   r'   r   r   r   r   S   s    	'C'r   r   N)	r%   requests.utilsr   multipart.encoderr   r   objectr   r   r'   r   r   <module>r?      s-   
 % 9>A >AB!'F !'r   