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-
len¶
-
-
class
acdcli.bundled.encoder.MultipartEncoder(fields, boundary=None, encoding='utf-8')[source]¶ Bases:
objectThe
MultipartEncoderobject is a generic interface to the engine that will create amultipart/form-databody 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’
filesparameter.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,httplibhas 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 thelenattribute.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, readwill 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:
objectAn object used to monitor the progress of a
MultipartEncoder.The
MultipartEncodershould 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
MultipartEncoderas 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
MultipartEncoderinstance
-
callback= None¶ Optionally function to call after a read
-
content_type¶
-
encoder= None¶ Instance of the
MultipartEncoderbeing 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-
bytes_left_to_write()[source]¶ Determine if there are bytes left to write.
Returns: bool – Trueif there are bytes left to write, otherwiseFalse
-
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
stringwithencodingif necessary.Parameters: Returns: encoded bytes object
-
acdcli.bundled.encoder.readable_data(data, encoding)[source]¶ Coerce the data to an object with a
readmethod.