-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multipart/form-data streaming encoder
I initially implemented this for Requests in the requests-toolbelt but this was already pretty generic because Requests just passes file-like objects (which is how the streaming encoder behaves) directly to urllib3. All that needed to change was what we were relying on from the requests namespace and imports and such. This also adds the decoder in ther same breath because it's easier to ensure that's all working together properly in one and it all fits together nicely. One thing we _could_ do is consolidate a bunch of the logic too and make `encode_multipart_formdata` rely on the streaming encoder and call `getall` instead so that we don't have 2 implementations of the same logic.
- Loading branch information
1 parent
c96cf40
commit 112bcb9
Showing
15 changed files
with
1,388 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Added support for reduced memory ``multipart/form-data`` uploads. | ||
|
||
Previously, one would need to specify ``fields=...`` with | ||
``encode_multipart=True`` and ``multipart_boundary=...`` to send a | ||
``multipart/form-data`` request to a server via urllib3's ``PoolManager``. | ||
This would encode all of the data into a buffer in memory to send it to the | ||
server. | ||
|
||
Now, one can use the ``urllib3.multipart.MultipartEncoder``. It accepts the | ||
same format of values as the ``fields=`` argument and accepts an optional | ||
``boundary=`` argument like ``PoolManager.urlopen`` and | ||
``PoolManager.request``. This will not load the entirety of your data into | ||
memory but will instead conservatively stream it by implementing a subset of | ||
the file API for ``http.client`` to use. | ||
|
||
Example: | ||
|
||
.. code-block:: python | ||
import urllib3 | ||
from urllib3 import multipart | ||
encoder = multipart.MultipartEncoder({ | ||
"field_1": "value_1", | ||
"field_2": "value_2", | ||
"field_3": ( | ||
"my_super_large_file.tar.gz", | ||
open("my_super_large_file.tar.gz", "rb"), | ||
"application/tar+gzip", | ||
), | ||
"field_4": ( | ||
"big_data_dump.psql.gz", | ||
open("big_data_dump.psql.gz", "rb"), | ||
"application/gzip", | ||
), | ||
}) | ||
response = urllib3.request( | ||
"POST", | ||
"https://httpbin.org/post", | ||
headers=encoder.headers, | ||
body=encoder, | ||
) | ||
This also adds support for efficiently decoding ``multipart/form-data`` | ||
responses too via ``urllib3.multipart.MultipartDecoder``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,6 @@ API Reference | |
urllib3.exceptions | ||
urllib3.response | ||
urllib3.fields | ||
urllib3.multipart | ||
urllib3.util | ||
contrib/index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Multipart Encoders and Decoders | ||
=============================== | ||
|
||
Streaming Request Encoding | ||
-------------------------- | ||
|
||
.. autoclass:: urllib3.multipart.MultipartEncoder | ||
:members: content_type, content_length, headers | ||
|
||
.. automethod:: read | ||
|
||
|
||
Response Decoding | ||
----------------- | ||
|
||
.. autoclass:: urllib3.multipart.MultipartDecoder | ||
:members: content_type, encoding, parts | ||
|
||
.. automethod:: from_response | ||
|
||
.. autoclass:: urllib3.multipart.BodyPart | ||
:members: content, encoding, headers, text | ||
|
||
.. autoexception:: urllib3.multipart.ImproperBodyPartContentError | ||
|
||
.. autoexception:: urllib3.multipart.NonMultipartContentTypeError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ pytest>=6.2 | |
trustme==0.9.0 | ||
types-backports | ||
types-requests | ||
types-mock | ||
nox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Multipart support for urllib3.""" | ||
|
||
from .decoder import ImproperBodyPartContentError as ImproperBodyPartContentError | ||
from .decoder import MultipartDecoder as MultipartDecoder | ||
from .decoder import NonMultipartContentTypeError as NonMultipartContentTypeError | ||
from .encoder import MultipartEncoder as MultipartEncoder | ||
|
||
__authors__ = "Ian Stapleton Cordasco, Cory Benfield" | ||
__copyright__ = "Copyright 2014 Ian Stapleton Cordasco, Cory Benfield" | ||
|
||
__all__ = [ | ||
"MultipartEncoder", | ||
"MultipartDecoder", | ||
"ImproperBodyPartContentError", | ||
"NonMultipartContentTypeError", | ||
] |
Oops, something went wrong.