httpx
httpx copied to clipboard
support chunked upload in async file-like interfaces
Summary
This PR resolves #1620 and implements chunked upload for anyio.open_file, trio.open_file and aiofiles.open_file when used as content= parameter for post and put request. And implements support for multipart file upload for the same libraries. Most of the code for multipart file upload copied from my old stale PR https://github.com/encode/httpx/pull/3339.
changes:
-
_compat.pyfile added to defineTypeIsandaclosingfor the range of supported python versions -
_types.AsyncReadableFileprotocol was added along withis_async_readable_filetype predicate function to detect and perform type narrowing for trio/asyncio/aiofiles async files -
_types.FileContentwas extended to include the._types.AsyncReadableFileprotocol -
_content.AsyncIteratorByteStreamupdated to useasync readmethods for async files instead of looping over lines -
_content.encode_contentupdated to attach content length header for_types.AsyncReadableFiletype -
_multipart.FileFieldupdated to include async versions ofrenderandrender_data -
_multipart.MultipartStreamupdated with async version ofiter_dataand__aiter__method updated to use it -
test_chunked_async_file_multipartandtest_chunked_async_file_contenttests are added to test the new functionality -
aiofilesandtypes-aiofilesadded in requirements to test aiofiles (can be removed with test if aiofiles support is not desirable)
Checklist
- [x] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
- [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
- [ ] I've updated the documentation accordingly. (can update if PR is ok otherwise)