Skip to content

Enhance get_items to handle large requests and expose partial errors - #155

Merged
sergioteula merged 4 commits into
masterfrom
claude/modulo-sdk-review-kal2oc
Sep 4, 2026
Merged

Enhance get_items to handle large requests and expose partial errors#155
sergioteula merged 4 commits into
masterfrom
claude/modulo-sdk-review-kal2oc

Conversation

@sergioteula

Copy link
Copy Markdown
Owner

Summary

This PR enhances the get_items method to automatically handle requests larger than the API limit by splitting them into multiple calls, while maintaining the requested order and exposing partial errors from Amazon's responses.

Key Changes

  • Automatic request splitting: get_items now splits requests with more than 10 items into multiple API calls, allowing any amount of items to be requested at once
  • Order preservation: Items are returned in the order they were requested, regardless of the order returned by Amazon
  • Duplicate handling: Duplicate item identifiers are automatically deduplicated before making requests
  • Partial error exposure: Added ResultList container that extends list to expose partial errors from Amazon responses via an errors attribute
  • Include unavailable items: New include_unavailable parameter adds placeholder items (with only ASIN) for requested items missing from the response
  • Token manager timeout: Replaced the SDK's token manager with TimeoutOAuth2TokenManager that respects the configured timeout for authentication requests, preventing indefinite hangs
  • Request validation: Added build_request utility that validates request parameters and raises InvalidArgumentError for invalid values

Implementation Details

  • New utility modules:

    • amazon_creatorsapi/core/items.py: Functions for deduplication, chunking, and sorting items
    • amazon_creatorsapi/core/results.py: ResultList container for results with partial errors
    • amazon_creatorsapi/core/auth.py: TimeoutOAuth2TokenManager for timeout-aware token requests
    • amazon_creatorsapi/core/validation.py: Enhanced with build_request function
  • Updated get_items signature to return ResultList[Item] instead of list[Item]

  • Both sync (AmazonCreatorsApi) and async (AsyncAmazonCreatorsApi) implementations updated consistently

  • Comprehensive test coverage added for new functionality

  • Documentation updated with examples of the new capabilities

Breaking Changes

  • get_items now returns ResultList[Item] instead of list[Item] (backward compatible as ResultList extends list)

https://claude.ai/code/session_01QioufR61SgXdYiiGmcNytD

claude and others added 4 commits September 3, 2026 23:18
Requests for several items now keep the order they were asked for, are
split into as many API calls as the item limit requires, and report the
partial errors that Amazon returns for the items it could not resolve.

- Split get_items into chunks and ask for duplicated identifiers once
- Return the items in the order of the request, with include_unavailable
  to get a placeholder for the ones missing from the response
- Expose the partial errors of a response in the returned list and in the
  message of ItemsNotFoundError
- Raise InvalidArgumentError instead of leaking pydantic validation errors
- Apply the timeout to the OAuth2 token refresh of the sync client, which
  waited indefinitely, and report its failures as AuthenticationError
- Fix the test fixtures that used the casing of the old PA-API responses

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QioufR61SgXdYiiGmcNytD
Failures were reported with the error codes of the old Product Advertising
API, so anything that was not a 404 or a 429 arrived as a generic request
error, and a throttled or failed request was never tried again even though
the API documents that clients should back off and retry.

- Map the errors from the response of the API, keeping the reason and the
  fields that failed in the message of the exception
- Raise AuthenticationError for missing or expired credentials and the new
  AccessDeniedError for the ones without access to the operation
- Add ResourceNotFoundError for feeds and reports, told apart from items
- Retry throttled and server errors with the new retries parameter, waiting
  longer before every attempt and honouring the Retry-After header
- Refresh an expired token once and send the request again
- Wrap connection failures and unparseable responses in RequestError

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QioufR61SgXdYiiGmcNytD
The async client built its requests by hand, so it accepted values that the
API rejects and could drift away from the synchronous one. It now builds
them with the models of the SDK, which also brings the parameters that were
missing to both clients.

- Add the availability parameter to search_items, and reject a search
  without any criteria instead of sending it to the API
- Add host and auth_endpoint to both clients, and give every client its own
  configuration instead of the one shared by the whole process
- Report the identifier Amazon gives to a request in the error message
- Measure throttling with a monotonic clock, guarded by a lock so the
  synchronous client can be shared by several threads
- Ship the py.typed marker and export get_asin and errors from the package
- Fix the examples of the documentation that used names that do not exist,
  and the documented limits that did not match the ones of the API
- Drop the unused six dependency

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QioufR61SgXdYiiGmcNytD
@sergioteula
sergioteula merged commit 9221dce into master Sep 4, 2026
7 checks passed
@sergioteula
sergioteula deleted the claude/modulo-sdk-review-kal2oc branch September 4, 2026 07:45
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.

2 participants