-
-
Notifications
You must be signed in to change notification settings - Fork 999
Added RequestContent #636
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
Added RequestContent #636
Conversation
|
NB. The We'll want to think a little about exactly about if/how we want to expose any of this on a |
|
Much design credit here is due to @sethmlarson. |
florimondmanca
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really like the separation that this new interface enables too, yes!
Some minor comments/questions, but overall this is great. 😄
Co-Authored-By: Florimond Manca <[email protected]>
|
Other thoughts...
None of that is public API tho, so we could treat it incrementally. |
What about
I haven't looked at our multipart implementation, so I can't say yet, but definitely in favor of treating that as a follow-up. :-) |
florimondmanca
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me as-is, feel free to resolve the question on the content.py module name. :-) Exciting stuff in any case!
Gotcha. Not 100% sure yet, but I think that's one that is ok to tackle incrementally. 👍 |
The RequestContent interface
This pull requests adds
RequestContentas the internal interface for all request body construction. The RequestContent interface provides:.get_headers()- Return the headers for the encoding..is_rewindable()- True if the content can be replayed, False otherwise.async def __aiter__() -> bytes- Returns the byte stream for the encoded data.There are a few concrete implementations.
RequestContent- An empty request content.BytesRequestContent- Handles plain bytes and str content.StreamingRequestContent- Handles byte async iterables. Not rewindable.JSONRequestContent- Handles JSON data.URLEncodedRequestContent- Handles URL encoded form data.MultipartEncodedRequestContent- Handles Multipart encoded form data.There is also a top level function for handling returning a
RequestContentinstance, givendata, files, jsonarguments.encode(data, files, json) -> RequestContentMotivation
Short: Generally a much nicer seperation of logic & will allow us to support both sync+async cases on a single
Requestclass.Requestclass itself, and testable in isolation.__iter__on the the interface alongside the existing__aiter__- most encodings will support both. The StreamingRequestContent will end up as two distinct cases sync+async, which will only support one or the other case, and will error if used incorrectly. (Eg. passing an async iterable as adata=argument will raise an error when used with a sync client.)