-
-
Notifications
You must be signed in to change notification settings - Fork 857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exposing the content stream API #1253
Comments
Delightful write up Tom, as always! Sounds like a nice way to expose the API while not exposing the entire hierarchy of actual implementations (which I agree ought to remain private at this time). Some actionable items already, though I'm not sure I wrapped my head around all possible details, but we can discuss those against a PR. 👍 |
Thanks @florimondmanca - main thing here is just to try to explain what needs to change and why, before we expose an I'll aim address this in step by step PRs, and make the motivation clear at each point.
|
Actually we may not need to merge We can do this perfectly well with the existing
We still need the refactoring to drop |
While we've pretty much got all of our public/private API properly demarcated now, there's one aspect where that's still not quite true, which is the content stream API. We currently expose
ContentStream
parameters or properties in the following places...Request(..., stream=<ContentStream>)
- Exposed in order to copy + modify a request eg. on redirect or digest auth.Response(..., stream=<ContentStream>)
- Exposed in order to pass from the transport layer when constructing the response.request.stream
- Exposed in order to pass to the transport layer when sending the request, or to copy + modify a request eg. on redirect or digest auth.We're also a bit wooly about if we actually mean an
httpcore.AsyncByteStream
/httpcore.SyncByteStream
or anhttpx.ContentStream
in places where that's exposed. Furthermore, we actually would like to expose content streams for some advanced functionality, like being able to support streaming the content of a file, while providing replay support.Eg...
But if we exposed the content stream API then we could support something like...
Because we're a bit fuzzy around the
ContentStream
vs.httpcore.AsyncByteStream
/httpcore.SyncByteStream
I think we also need an API that's more clear there, and doesn't have this odd artifact of.can_replay()
and.get_headers()
methods, that are used in some contexts, but not in others.Here's what I think we need to do here.
httpcore
API, so that we only havehttpcore.ByteStream
, which is an abstract base class with for a unified sync+async byte stream. Ie. it exposes__iter__
,__aiter__
,close
,aclose
methods.can_replay()
on the httpx implementations. Instead raise an exception in__iter__
/__aiter__
if we attempt to replay a generator byte stream. Eg.StreamConsumed("Attempted to iterate over a byte stream, but the stream has already been consumed.")
If necessary we could catch that in auth/redirect contexts and provide more information about exactly why it's occured.get_headers()
on the httpx implementations, and instead haveencode
return a two-tuple of(headers, stream)
.ByteStream
instead ofContentStream
.I guess
httpx.ByteStream
would then just be a synonym forhttpcore.ByteStream
.Also, the only byte stream class that we would expose would be the abstract base
httpx.ByteStream
- If you want to use it you have to subclass it and provide a concrete implementation. For anything else you're already covered by the standarddata
/files
/json
types.The text was updated successfully, but these errors were encountered: