Refactor OAuth2 settings and improve client validation - #157
Merged
Conversation
Both clients drifted apart on the auth endpoint, the errors they raise and the values they accept, so this brings them back together and covers the cases that failed silently. - The async client rejected a version out of its list even when a custom auth_endpoint was given, which is what that parameter is for. Both clients now resolve the endpoint with the same list, so a version only has to be added once and the SDK list is no longer a second source of truth. - A token response that is not JSON escaped from the async client as the error of the JSON parser, while the sync one already reported it as an AuthenticationError. - Errors reported by the transport, such as an invalid certificate, arrive without a body and were reported as "Request failed with status 0", dropping the only useful part of the failure. - An ASIN was matched without a boundary, so a longer identifier in a URL was trimmed to ten characters and a different item was requested. - get_items only checked the items returned by Amazon, not the ones that matched the request, so a response holding other items returned an empty list instead of raising. - throttling was the only option without validation, accepting a negative value and failing with a TypeError for anything that is not a number. - The token manager of the sync client had no lock, so every thread asked for its own token as soon as the cached one expired. - Retry-After is also read when Amazon sends it as a date. - AmazonCreatorsApi can be closed, releasing the pool of connections that it keeps open, which only the async client could do. - mypy was configured for a version it no longer supports, so the whole check was running against its default instead. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KN1v6C671EcHw5Vur9rC3y
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR refactors OAuth2 configuration into a shared module, improves input validation for both sync and async clients, adds context manager support, and enhances error handling for edge cases.
Key Changes
OAuth2 Configuration Refactoring
amazon_creatorsapi/core/oauth.pymodule to centralize OAuth2 constants and logic shared between sync and async clientsVERSION_ENDPOINTS,COGNITO_SCOPE,LWA_SCOPE,GRANT_TYPE,TOKEN_EXPIRATION_BUFFER, andDEFAULT_EXPIRATIONto the shared moduleis_lwa(),get_scope(), andget_auth_endpoint()to eliminate duplicationAmazonCreatorsApiandAsyncAmazonCreatorsApinow useget_auth_endpoint()for consistent version validationInput Validation Improvements
validate_throttling()function to validate wait time between API calls (must be a number ≥ 0)validate_timeout()andvalidate_retries()to properly handle non-numeric inputs with clear error messagesauth_endpointparameter now makes any API version valid, allowing forward compatibilityClient Lifecycle Management
__enter__()and__exit__()methods toAmazonCreatorsApifor context manager supportclose()method to release connection poolsToken Manager Improvements
TimeoutOAuth2TokenManagerusing a lock to prevent multiple threads from requesting tokens simultaneouslyError Handling Enhancements
handle_api_error()now accepts and reports thereasonfield from API exceptions when response body is emptyget_seconds_until()function to parse HTTP date headers inRetry-AfterresponsesTest Coverage
Documentation
https://claude.ai/code/session_01KN1v6C671EcHw5Vur9rC3y