Skip to content

Commit

Permalink
v30/request - streaming application/octet-stream requests
Browse files Browse the repository at this point in the history
  • Loading branch information
commonism committed Aug 24, 2023
1 parent 178461a commit 0a0789c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
10 changes: 9 additions & 1 deletion aiopenapi3/v30/glue.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
from typing import List, Union, cast
import json
import urllib.parse
Expand Down Expand Up @@ -327,7 +328,14 @@ def _prepare_body(self, data, rbq):
self.req.content = msg
elif (ct := "application/octet-stream") in self.operation.requestBody.content:
self.req.headers["Content-Type"] = ct
self.req.content = data[1].read()
value = data
if isinstance(data, tuple) and len(data) >= 2:
# (name, file-like-object, …)
value = data[1]
if isinstance(value, (io.IOBase, str, bytes)):
self.req.content = value
else:
raise TypeError(data)
else:
raise NotImplementedError(self.operation.requestBody.content)

Expand Down
23 changes: 20 additions & 3 deletions docs/source/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,19 +188,24 @@ File uploads via "multipart/form-data" as mentioned in the httpx documentation
`encoding <https://www.python-httpx.org/advanced/#multipart-file-encoding>`_)
do not require the content of the request to be in memory but work with file-like-objects instead.

httpx request streaming using file-like objects is limited to "multipart/form-data".
It can not be used with "application/json" or "application/octet-stream".
httpx request streaming using file-like objects is limited to "multipart/form-data" and "application/octet-stream".
Additionally it does not support choice of encoding (such as base16, base64url or quoted-printable) as possible with OpenAPI v3.1 contentEncoding, which should not be a limitation.
It can not be used with "application/json".


Use via `Manual Requests`_ using the :meth:`~aiopenapi3.request.RequestBase.request` API.

multipart/form-data
^^^^^^^^^^^^^^^^^^^

Pass the form fields as a list of tuples.

.. code:: python
data = [
("name",('form-data:name', file-like-object, content_type, headers))
]
A Request like
.. code::
Expand Down Expand Up @@ -238,6 +243,18 @@ Mixing file-like-objects and other form data fields is possible.
See :aioai3:ref:`tests.stream_test.test_request`.
application/octet-stream
^^^^^^^^^^^^^^^^^^^^^^^^
Pass the data as file-like-object or tuple where the second entry is a file-like-object as with multipart/form-data.
.. code:: python
data = Path("/data/file").open("rb")
data = ("name", Path("/data/file").open("rb"))
Response Streaming
------------------
Expand Down

0 comments on commit 0a0789c

Please sign in to comment.