Skip to content

Add content=b"" to base stream implementations. #125

Description

@lovelydinosaur

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=contentself.aclose_func=aclose_funcasyncdef__aiter__(self) ->AsyncIterator[bytes]:
""" Yield bytes representing the request or response body. """ifisinstance(self.content, bytes):
yieldself.contentelse:
asyncforchunkinself.content:
yieldchunkasyncdefaclose(self) ->None:
""" Must be called by the client to indicate that the stream has been closed. """ifself.aclose_funcisnotNone:
awaitself.aclose_func()

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions