�
ӵ Yc @ s d Z d d l Z d d l Z d d l Z d d l m Z d d l m Z d e f d � � YZ d � Z
d e f d
� � YZ d � Z d � Z
d
� Z e j d � � Z d � Z d � Z d e f d � � YZ d e j f d � � YZ d e f d � � YZ d S( s�
requests_toolbelt.multipart.encoder
===================================
This holds all of the implementation details of the MultipartEncoder
i����N( t uuid4i ( t fieldst MultipartEncoderc B s� e Z d Z d d d � Z e d � � Z d � Z d � Z d � Z d � Z
d � Z d � Z d
� Z
d � Z d � Z d
� Z d � Z e d � � Z d � Z d d � Z RS( s
The ``MultipartEncoder`` oject is a generic interface to the engine that
will create a ``multipart/form-data`` body for you.
The basic usage is:
.. code-block:: python
import requests
from requests_toolbelt import MultipartEncoder
encoder = MultipartEncoder({'field': 'value',
'other_field', 'other_value'})
r = requests.post('https://httpbin.org/post', data=encoder,
headers={'Content-Type': encoder.content_type})
If you do not need to take advantage of streaming the post body, you can
also do:
.. code-block:: python
r = requests.post('https://httpbin.org/post',
data=encoder.to_string(),
headers={'Content-Type': encoder.content_type})
If you want the encoder to use a specific order, you can use an
OrderedDict or more simply, a list of tuples:
.. code-block:: python
encoder = MultipartEncoder([('field', 'value'),
('other_field', 'other_value')])
.. versionchanged:: 0.4.0
You can also provide tuples as part values as you would provide them to
requests' ``files`` parameter.
.. code-block:: python
encoder = MultipartEncoder({
'field': ('file_name', b'{"a": "b"}', 'application/json',
{'X-My-Header': 'my-value'})
])
.. warning::
This object will end up directly in :mod:`httplib`. Currently,
:mod:`httplib` has a hard-coded read size of **8192 bytes**. This
means that it will loop until the file has been read and your upload
could take a while. This is **not** a bug in requests. A feature is
being considered for this object to allow you, the user, to specify
what size should be returned on a read. If you have opinions on this,
please weigh in on `this issue`_.
.. _this issue:
https://github.com/sigmavirus24/requests-toolbelt/issues/75
s utf-8c C s� | p t � j | _ d j | j � | _ | | _ d j t | j | j � t d | j � g � | _ | | _ t
| _ g | _ t
g � | _ d | _ d | _ t d | � | _ | j � | j � d S( Ns --{0}t s
t encoding( R t hext boundary_valuet formatt boundaryR t joint encode_witht _encoded_boundaryR t Falset finishedt partst itert _iter_partst Nonet
_current_partt _lent
CustomBytesIOt _buffert _prepare_partst _write_boundary( t selfR R R ( ( sG /usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt __init__Q s
c C s | j p | j � S( sv Length of the multipart/form-data body.
requests will first attempt to get the length of the body by calling
``len(body)`` and then by checking for the ``len`` attribute.
On 32-bit systems, the ``__len__`` method cannot return anything
larger than an integer (in C) can hold. If the total size of the body
is even slightly larger than 4GB users will see an OverflowError. This
manifested itself in `bug #80`_.
As such, we now calculate the length lazily as a property.
.. _bug #80:
https://github.com/sigmavirus24/requests-toolbelt/issues/80
( R t _calculate_length( R ( ( sG /usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt len| s c C s d j | j � S( Ns <MultipartEncoder: {0!r}>( R R ( R ( ( sG /usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyt __repr__� s c s@ t | j � � t � f d � | j D� � � d | _ | j S( s�
This uses the parts to calculate the length of the body.
This returns the calculated length so __len__ can be lazy.
c 3 s# | ] } � t | � d Vq d S( i N( t total_len( t .0t p( t boundary_len( sG /usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pys <genexpr>� s i ( R R t sumR R ( R ( ( R sG /usr/lib/python2.7/site-packages/requests_toolbelt/multipart/encoder.pyR � s *c C s'