Skip to content

feat(proxy): add proxy support to AmazonCreatorsApi#150

Open
mekarpeles wants to merge 2 commits into
sergioteula:masterfrom
mekarpeles:proxy-support
Open

feat(proxy): add proxy support to AmazonCreatorsApi#150
mekarpeles wants to merge 2 commits into
sergioteula:masterfrom
mekarpeles:proxy-support

Conversation

@mekarpeles

@mekarpeles mekarpeles commented Jun 11, 2026

Copy link
Copy Markdown

Summary

Fixes two independent gaps that prevented AmazonCreatorsApi from routing traffic through an HTTP proxy. Closes #149.

Problem 1 — no proxy parameter: AmazonCreatorsApi.__init__ created ApiClient without a Configuration, so configuration.proxy was always None. RESTClientObject already handled urllib3.ProxyManager when configuration.proxy was set — it just wasn't reachable from the public API.

Problem 2 — token refresh bypassed proxy: OAuth2TokenManager.refresh_token() called requests.post() directly. Even with proxy config wired, token refresh on cold cache (worker restart, token expiry) still went direct.

Changes

  • amazon_creatorsapi/api.py — adds proxy: str | None = None to AmazonCreatorsApi.__init__; builds a Configuration with it and passes to ApiClient
  • creatorsapi_python_sdk/api_client.py — extracts configuration.proxy when lazily initialising _token_manager and passes it as a proxies dict
  • creatorsapi_python_sdk/auth/oauth2_token_manager.py — accepts proxies in __init__; refresh_token() uses requests.Session with .proxies set. Applies to both LWA (v3.x) and Cognito (v2.x) flows

Usage

from amazon_creatorsapi import AmazonCreatorsApi

api = AmazonCreatorsApi(
    credential_id="...",
    credential_secret="...",
    version="2.2",
    tag="your-tag",
    country="US",
    proxy="http://user:pass@proxy:3128",
)

Tests

  • tests/amazon_creatorsapi/api_test.py — 2 new tests verifying configuration.proxy is set (or None) on ApiClient based on the proxy param
  • tests/amazon_creatorsapi/oauth2_token_manager_test.py — new file, 3 tests: proxy applied on Cognito refresh, proxy applied on LWA refresh, no proxy → session not configured

39 tests passing, no regressions.

Known limitations

proxy_headers (for cases where credentials cannot be embedded in the URL) is not yet exposed. The current interface covers the common case of credential-bearing proxy URLs.

Real-world context

This unblocks Internet Archive / Open Library, which runs behind an authenticated Squid proxy and currently carries two workarounds:


This PR was developed with Claude (claude-sonnet-4-6) as co-author.

Fixes two independent gaps that prevented routing traffic through an
HTTP proxy:

1. AmazonCreatorsApi.__init__ now accepts a `proxy` URL parameter and
   passes it to ApiClient via a Configuration object. RESTClientObject
   already used urllib3.ProxyManager when configuration.proxy was set;
   this just wires the public API through to it.

2. OAuth2TokenManager.refresh_token() previously called requests.post()
   directly, bypassing any proxy configuration. It now creates a
   requests.Session and sets .proxies when a proxy URL is provided,
   ensuring token refresh on cold cache also routes through the proxy.

Closes sergioteula#149

Co-authored-by: Claude (claude-sonnet-4-6) <claude[bot]@users.noreply.github.com>
@mekarpeles mekarpeles marked this pull request as ready for review June 22, 2026 20:06
urllib3.ProxyManager ignores userinfo embedded in the proxy URL when
establishing HTTPS CONNECT tunnels — it only reads the netloc for
the tunnel destination, not the proxy auth headers.  The result is a
407 Proxy Authentication Required on every HTTPS API call through an
authenticated Squid proxy (e.g. IA's http-proxy.us.archive.org).

Fix: before handing the URL to ProxyManager, check whether
configuration.proxy_headers is already set (caller-supplied).  If
not, parse the URL, extract username:password, build the
Proxy-Authorization header via urllib3.make_headers(proxy_basic_auth=)
and strip the credentials from the URL.  The full credentialed URL is
preserved in configuration.proxy so the requests-based OAuth2 token
leg (api_client.py) continues to work unchanged.

Confirmed via proxy_test.py on ol-home0:
  Approach A (URL-only): 407 Proxy Authentication Required
  Approach B (proxy_headers): 400 from creatorsapi.amazon (connected)
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.

AmazonCreatorsApi has no proxy support — regular calls and OAuth2 token refresh both bypass HTTP proxy

1 participant