Better developer experience for accessing Request/Response bodies in event_hooks and httpx.Auth
#2853
Unanswered
ghost
asked this question in
Potential Issue
Replies: 1 comment 6 replies
-
|
Thanks so much for a well worded proposal. 💚 What are the implications when using the |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
One inconvenience I came across when trying out httpx is that it requires that you call
response.read()(orresponse.aread()for AsyncClients) in event hooks if you need to access the response body.While at first glance this seems like a simple enough requirement, consider the following two code snippets:
The two examples attempt to accomplish the same thing, but lead to different outcomes because of this behavior.
In the second snippet, it doesn't make sense for the event hook to call
response.read()because it does not directly access the response body. It also is not always apparent to the user of the client that they should callresponse.read()when catching exceptions: From my experience, it's common for event hooks to be encapsulated from the user, and the user may not be aware of the event hooks used on the client (because that's none of their concern). With httpx's current design, it's not clear who's responsibility it is to call response.read().The response body is also not accessible to subclasses of
httpx.Authby default. However,httpx.Authhandles that in a completely different way through the use of therequires_response_bodyproperty (unless you're using thecallableauth type in which case you do need to directly callresponse.read()). This inconsistent handling of the response body makes using httpx more complex than it should be.I don't see any benefit from requiring users to explicitly specify when they need to access the response body and throwing a
ResponseNotReadexception at them when they don't.I propose that
response.contentshould automatically read and return the response body if it hasn't already been read. Same thing goes forrequest.content. This would remove the need for theResponseNotReadandRequestNotReadexceptions as well as therequires_response_bodyandrequires_request_bodyproperties fromhttpx.Auth.Beta Was this translation helpful? Give feedback.
All reactions