From encode/httpx#1096
The httpcore.SyncByteStream and httpcore.AsyncByteStream classes provide a simple base implementation that allows users to pass iterator/close or aiterator/aclose parameters. That's actually a bit fiddly if you've just got a non-iterating bytestring that you want to pass. We ought to also support passing plain bytes to the base implementation. So...
"""
The base interface for request and response bodies.
Concrete implementations should subclass this class, and implement
the `\\__aiter__` method, and optionally the `close` method.
"""
def __init__(
self, content: Union[bytes, AsyncIterator[bytes]] = b"", aclose_func: Callable = None,
) -> None:
self.content = content
self.aclose_func = aclose_func
async def __aiter__(self) -> AsyncIterator[bytes]:
"""
Yield bytes representing the request or response body.
"""
if isinstance(self.content, bytes):
yield self.content
else:
async for chunk in self.content:
yield chunk
async def aclose(self) -> None:
"""
Must be called by the client to indicate that the stream has been closed.
"""
if self.aclose_func is not None:
await self.aclose_func()
From encode/httpx#1096
The
httpcore.SyncByteStreamandhttpcore.AsyncByteStreamclasses provide a simple base implementation that allows users to passiterator/closeoraiterator/acloseparameters. That's actually a bit fiddly if you've just got a non-iterating bytestring that you want to pass. We ought to also support passing plain bytes to the base implementation. So...