-
-
Notifications
You must be signed in to change notification settings - Fork 227
feat: Add raise_on_unexpected_status
flag to Client
#593
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
feat: Add raise_on_unexpected_status
flag to Client
#593
Conversation
b16fc5c
to
a31befa
Compare
Codecov Report
@@ Coverage Diff @@
## main #593 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 49 49
Lines 1966 1969 +3
=========================================
+ Hits 1966 1969 +3
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Hey @JamesHinshelwood, thanks for the contribution! I am wondering if maybe we should just expose the underlying This could come in the form of subclassing (if it doesn't get too messy to mix in with generics) or as an inner attribute to which we delegate some methods for easier access. What do you think? |
For my use-case, what I really want is an API where I can just call an endpoint and handle the 'expected' responses, then I would get an exception raised for anything unexpected. Even exposing the response directly wouldn't be ideal for me, because then I still need to convert the 'unexpected' response to exceptions myself. I've realised though that 'expected' doesn't necessarily mean def _parse_response(*, response: httpx.Response) -> Optional[GoodResponse]:
if response.status_code == 200:
response_200 = GoodResponse.from_dict(response.json())
return response_200
# return None
raise Exception(f"Unexpected status code: {response.status_code}) Also tagging @theFong @ramnes @johnthagen @JorgeGarciaIrazabal who expressed an interest in the discussion, but might have missed this PR. |
a31befa
to
e26e93f
Compare
raise_on_error_code
flag to Client
raise_on_unexpected_status
flag to Client
I've updated this PR with what I (hopefully) described above. Let me know what you think. |
Raising on 2xx responses doesn't feel right to me, why do you feel it's needed? A lot of APIs use 201 and 204, for example. |
If the |
Ah, right, sorry! I thought you were always raising on non-200 status codes. LGTM. :) |
This is definitely an interesting concept—it basically makes the client stricter by enforcing that the server only returns the types that the spec says it will. I cannot count the amount of times I have seen undocumented responses from servers 😓. I think this is a good behavior to have, I would just like a couple of tweaks to make it a bit more user friendly:
|
This seems really important, otherwise you need the full From @dbanty response, it looks like we are only 3 small steps away from getting this in? |
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.
I finished this one off, thanks for the work everyone!
Progress toward #491
Not sure this is the right approach. Just wanted to raise a PR to gather feedback :)