Skip to content

Conversation

@KimBioInfoStudio
Copy link

Summary

Add a new Response.raise_for_excepted_status(expected) method that provides strict status code validation. Unlike raise_for_status() which only raises on non-2xx status codes, this method requires all acceptable status codes to be explicitly listed—including success codes.

Use case: In some API integrations, certain non-2xx status codes are expected (e.g., 404 when checking resource existence). Conversely, you may want to ensure only specific status codes are accepted.

response.raise_for_excepted_status([200, 404])  # Only 200 and 404 allowed

Changes

  • Add _ensure_request() helper to validate request is set
  • Add _raise_status_error() helper to build and raise HTTPStatusError
  • Refactor raise_for_status() to use shared helpers
  • Add raise_for_excepted_status() with strict status code validation
  • Add documentation in api.md and quickstart.md
  • Add comprehensive unit tests (10 test cases)

Checklist

  • I understand that this PR may be closed in case there was no previous discussion.
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Add a new method that raises HTTPStatusError unless the status code
is explicitly listed in the expected parameter. Unlike `raise_for_status()`,
this method requires all acceptable status codes (including 2xx) to be
explicitly specified.

Also refactors shared logic into `_ensure_request()` and `_raise_status_error()`
helper methods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant