httpx icon indicating copy to clipboard operation
httpx copied to clipboard

support chunked upload in async file-like interfaces

Open tsimoshka opened this issue 2 months ago • 0 comments

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.py file added to define TypeIs and aclosing for the range of supported python versions
  • _types.AsyncReadableFile protocol was added along with is_async_readable_file type predicate function to detect and perform type narrowing for trio/asyncio/aiofiles async files
  • _types.FileContent was extended to include the. _types.AsyncReadableFile protocol
  • _content.AsyncIteratorByteStream updated to use async read methods for async files instead of looping over lines
  • _content.encode_content updated to attach content length header for _types.AsyncReadableFile type
  • _multipart.FileField updated to include async versions of render and render_data
  • _multipart.MultipartStream updated with async version of iter_data and __aiter__ method updated to use it
  • test_chunked_async_file_multipart and test_chunked_async_file_content tests are added to test the new functionality
  • aiofiles and types-aiofiles added 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)

tsimoshka avatar Oct 28 '25 11:10 tsimoshka