acdcli.bundled package

Submodules

acdcli.bundled.encoder module

requests_toolbelt.multipart.encoder

This holds all of the implementation details of the MultipartEncoder

class acdcli.bundled.encoder.CustomBytesIO(buffer=None, encoding='utf-8')[source]

Bases: _io.BytesIO

__init__(buffer=None, encoding='utf-8')[source]
append(bytes)[source]
len
smart_truncate()[source]
class acdcli.bundled.encoder.FileWrapper(file_object)[source]

Bases: object

__init__(file_object)[source]
len
read(length=-1)[source]
acdcli.bundled.encoder.IDENTITY(monitor)[source]
class acdcli.bundled.encoder.MultipartEncoder(fields, boundary=None, encoding='utf-8')[source]

Bases: object

The MultipartEncoder object is a generic interface to the engine that will create a multipart/form-data body for you.

The basic usage is:

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:

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:

encoder = MultipartEncoder([('field', 'value'),
                            ('other_field', 'other_value')])

Changed in version 0.4.0.

You can also provide tuples as part values as you would provide them to requests’ files parameter.

encoder = MultipartEncoder({
    'field': ('file_name', b'{"a": "b"}', 'application/json',
              {'X-My-Header': 'my-value'})
])

Warning

This object will end up directly in httplib. Currently, 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.

__init__(fields, boundary=None, encoding='utf-8')
boundary_value = None

Boundary value either passed in by the user or created

content_type
encoding = None

Encoding of the data being passed in

fields = None

Fields provided by the user

finished = None

Whether or not the encoder is finished

len

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.

parts = None

Pre-computed parts of the upload

read(size=-1)

Read data from the streaming encoder.

Parameters:size (int) – (optional), If provided, read will return exactly that many bytes. If it is not provided, it will return the remaining bytes.
Returns:bytes
to_string()
class acdcli.bundled.encoder.MultipartEncoderMonitor(encoder, callback=None)[source]

Bases: object

An object used to monitor the progress of a MultipartEncoder.

The MultipartEncoder should only be responsible for preparing and streaming the data. For anyone who wishes to monitor it, they shouldn’t be using that instance to manage that as well. Using this class, they can monitor an encoder and register a callback. The callback receives the instance of the monitor.

To use this monitor, you construct your MultipartEncoder as you normally would.

from requests_toolbelt import (MultipartEncoder,
                               MultipartEncoderMonitor)
import requests

def callback(encoder, bytes_read):
    # Do something with this information
    pass

m = MultipartEncoder(fields={'field0': 'value0'})
monitor = MultipartEncoderMonitor(m, callback)
headers = {'Content-Type': montior.content_type}
r = requests.post('https://httpbin.org/post', data=monitor,
                  headers=headers)

Alternatively, if your use case is very simple, you can use the following pattern.

from requests_toolbelt import MultipartEncoderMonitor
import requests

def callback(encoder, bytes_read):
    # Do something with this information
    pass

monitor = MultipartEncoderMonitor.from_fields(
    fields={'field0': 'value0'}, callback
    )
headers = {'Content-Type': montior.content_type}
r = requests.post('https://httpbin.org/post', data=monitor,
                  headers=headers)
__init__(encoder, callback=None)
bytes_read = None

Number of bytes already read from the MultipartEncoder instance

callback = None

Optionally function to call after a read

content_type
encoder = None

Instance of the MultipartEncoder being monitored

classmethod from_fields(fields, boundary=None, encoding='utf-8', callback=None)
len = None

Avoid the same problem in bug #80

read(size=-1)
to_string()
class acdcli.bundled.encoder.Part(headers, body)[source]

Bases: object

__init__(headers, body)[source]
bytes_left_to_write()[source]

Determine if there are bytes left to write.

Returns:bool – True if there are bytes left to write, otherwise False
classmethod from_field(field, encoding)[source]

Create a part from a Request Field generated by urllib3.

write_to(buffer, size)[source]

Write the requested amount of bytes to the buffer provided.

The number of bytes written may exceed size on the first read since we load the headers ambitiously.

Parameters:
  • buffer (CustomBytesIO) – buffer we want to write bytes to
  • size (int) – number of bytes requested to be written to the buffer
Returns:

int – number of bytes actually written

acdcli.bundled.encoder.coerce_data(data, encoding)[source]

Ensure that every object’s __len__ behaves uniformly.

acdcli.bundled.encoder.encode_with(string, encoding)[source]

Encoding string with encoding if necessary.

Parameters:
  • string (str) – If string is a bytes object, it will not encode it. Otherwise, this function will encode it with the provided encoding.
  • encoding (str) – The encoding with which to encode string.
Returns:

encoded bytes object

acdcli.bundled.encoder.readable_data(data, encoding)[source]

Coerce the data to an object with a read method.

acdcli.bundled.encoder.reset(buffer)[source]

Keep track of the buffer’s current position and write to the end.

This is a context manager meant to be used when adding data to the buffer. It eliminates the need for every function to be concerned with the position of the cursor in the buffer.

acdcli.bundled.encoder.to_list(fields)[source]

acdcli.bundled.fuse module

Module contents