a Æ*NaÌã@sDdZddlmZddlmZmZGdd„deƒZGdd„deƒZd 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$eZdZdZd dd„Zd dd„ZdS) Ú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-8cCsRt|ƒ|_|jdkrtdƒ‚|j|_||_||_t|dƒrB||_n t||ƒ|_dS)Nrz1The size of the upload must be a positive integerÚread) ÚintÚsizeÚ ValueErrorÚlenÚencodingÚiteratorÚhasattrÚ_fileÚ_IteratorAsBinaryFile)Úselfr rr ©rú/private/var/folders/js/6pj4vh5d4zd0k6bxv74qrbhr0000gr/T/pip-target-22xwyzbs/lib/python/requests_toolbelt/streaming_iterator.pyÚ__init__7s  ÿ zStreamingIterator.__init__éÿÿÿÿcCst|j |¡|jƒS©N)rrrr ©rr rrrrOszStreamingIterator.readN)r)r)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrrrrs# rc@s0eZdZd dd„Zdd„Zdd„Zd d d „Zd S)rrcCs||_||_tƒ|_dSr)rr rÚ_buffer)rrr rrrrTsz_IteratorAsBinaryFile.__init__cCs.ztt|jƒ|jƒWSty(YdS0dS)Nó)rÚnextrr Ú StopIteration)rrrrÚ _get_bytes_s z _IteratorAsBinaryFile._get_bytescCsF|j ¡|t|jƒ}d}|dkrB|rB| ¡}||j |¡8}qdS)NTr)rZsmart_truncaterr!Úappend)rr Zamount_to_loadZbytes_to_appendrrrÚ _load_byteses   z!_IteratorAsBinaryFile._load_bytesrcCs2t|ƒ}|dkrd |j¡S| |¡|j |¡S)Nrr)r Újoinrr#rrrrrrrns   z_IteratorAsBinaryFile.readN)r)r)rrrrr!r#rrrrrrSs  rN) rZrequests.utilsrZmultipart.encoderrrÚobjectrrrrrrÚs A