feat: expose response headers via thread-safe contextvars #764
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a thread-safe and async-safe way to access HTTP response headers (especially rate-limit information) from Auth0's Management API responses.
Changes
Added
ContextVarfor thread-safe header storage (auth0/rest.py)Added
last_response_headersproperty toRestClientRestClientclassdict[str, str]containing rate-limit and other response headersUpdated
EmptyResponseto preserve headers from 204 responsesTest suite (
auth0/test/test_rest_headers_contextvar.py)References
Testing
This PR includes comprehensive testing:
Unit Tests Added: 8 Tests (All Passing ✅)
test_headers_accessible_after_request- Verifies headers are stored and accessible after successful API callstest_headers_on_204_response- Confirms headers are preserved on 204 No Content responsestest_headers_updated_on_successive_requests- Validates headers are updated with each new requesttest_headers_empty_initially- Confirms empty dict before any API callstest_thread_isolation- KEY TEST: Verifies each thread sees its own headers (not contaminated by other threads)test_headers_in_same_context_reflect_latest_request- Confirms context-specific behaviortest_post_request_headers- Headers work on POST requeststest_patch_request_headers- Headers work on PATCH requestsTest Coverage
Existing Tests
Checklist