Skip to content

feat: add shared retrying requests session client (1/5) - #138

Open
sujen1412 wants to merge 1 commit into
developfrom
feature/requests-session-client
Open

sujen1412 wants to merge 1 commit into
developfrom
feature/requests-session-client

Conversation

@sujen1412

Copy link
Copy Markdown
Contributor

Github Issue: #137 (item 1 of 5)

Description

maap-py makes ~35 direct requests.* calls with no retries, no timeouts and duplicated auth-header construction. This is the first step of consolidating them: add the shared client only, with no callers migrated yet (items 2-5 in #137 follow as separate PRs).

Overview of work done

In maap/utils/requests_utils.py:

  • get_session() returns a shared, lazily created requests.Session (connection pooling) with a retrying HTTPAdapter: retries connection errors and 429/500/502/503/504 with exponential backoff, honours Retry-After, and returns the last response rather than raising RetryError.
  • Only idempotent methods (GET/HEAD/OPTIONS/PUT/DELETE) are retried. POST is not retried unless retry_post=True, which uses a separate session, so job submission can't be duplicated.
  • New request() helper: default (10, 60) timeout, auth headers (token / Authorization, MAAP_PGT) read from config on every call rather than snapshotted, auth=False to avoid sending MAAP credentials to third-party hosts, extra headers merged without mutating the caller's dict.
  • Debug logging redacts token, Authorization and proxy-ticket (previously the full headers were logged).
  • make_request / make_dps_request now delegate to request(). As a consequence, existing dps_job.py callers gain retries and a timeout, and PUT/DELETE no longer raise NotImplementedError.

Overview of verification done

New test/test_requests_utils.py (17 tests) runs against a local HTTP server, since responses mocks at the adapter level and would bypass urllib3 retries. It covers retry on 503/502/429, PUT/DELETE retry, no POST retry by default and opt-in, exhausted retries returning the last response, no retry on 404, default and overridden timeout, per-call token refresh, Bearer vs token header, auth=False, header non-mutation, token redaction in logs, and make_request for all methods.

test_requests_utils.py passes and flake8 is clean. The remaining suite shows the same 20 failures and 16 errors with and without this change (verified on the base branch); they need outbound network access, which was blocked in my environment.

Overview of integration done

None yet. Nothing outside dps_job.py uses this module so far, so a deployment check is best done once callers are migrated in the follow-up PRs.

PR checklist:

  • Linted
  • Updated unit tests
  • Updated changelog
  • Integration testing
  • Updated documentation

🤖 Generated with Claude Code

https://claude.ai/code/session_01Rp9gSo9Fw6M7DUFhWnNPaU


Generated by Claude Code

Add a requests.Session-based client to maap.utils.requests_utils with
retries (429/5xx + connection errors, idempotent methods only unless
opted in), default timeouts, request-time auth injection with an
auth=False opt-out, and token redaction in debug logs. make_request and
make_dps_request now delegate to it and support PUT/DELETE.

No other callers are migrated yet.

Refs #137

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Rp9gSo9Fw6M7DUFhWnNPaU
adapter = HTTPAdapter(max_retries=_build_retry(methods))
session = requests.Session()
session.mount('https://', adapter)
session.mount('http://', adapter)
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.

3 participants