refactor: consolidate HTTP client factories with Content-Type headers#203
refactor: consolidate HTTP client factories with Content-Type headers#203
Conversation
There was a problem hiding this comment.
Pull request overview
This PR centralizes HTTP client/session creation and standardizes default headers (notably Content-Type: application/json) across httpx, requests, and aiohttp, then refactors RunPod API clients and tarball operations to use those factories.
Changes:
- Add default
Content-Type: application/jsonheaders to existinghttpxandrequestsfactories. - Introduce a centralized
aiohttpsession factory with consistent headers and timeout/connector configuration. - Refactor RunPod API clients and app tarball download/upload flows to use the factories, plus add unit tests covering the factories.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/runpod_flash/core/utils/http.py |
Adds default JSON Content-Type to factories and introduces get_authenticated_aiohttp_session() for centralized async session configuration. |
src/runpod_flash/core/api/runpod.py |
Refactors GraphQL/REST clients to use the centralized aiohttp session factory. |
src/runpod_flash/core/resources/app.py |
Refactors tarball download/upload to use the centralized requests session factory and context management. |
tests/unit/core/utils/test_http.py |
Adds tests validating headers, override precedence, and timeout behavior for the factories. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
496b030 to
c4e269f
Compare
dbcf3ab to
9227a33
Compare
- Move raise_for_status() inside session scope with try/finally in app.py - Add Content-Type header documentation to httpx and requests docstrings - Add use_threaded_resolver param to aiohttp factory (REST client passes False) - Add tests for use_threaded_resolver parameter behavior
QA ReportStatus: FAIL Targeted Test Results
New Failures Introduced by This PR1.
2.
Coverage Impact
Full Suite Results
Pre-existing failures (not caused by this PR):
PR Diff Analysis
Potential Risk: Auth Headers on Pre-Signed URLsThe tarball RecommendationBLOCK — Two tests fail on any machine with stored RunPod credentials ( Generated by flash-qa agent |
Add "Content-Type: application/json" header to centralized HTTP client factories for consistency with aiohttp usage and to prevent issues with JSON endpoints. - get_authenticated_httpx_client: Add Content-Type header - get_authenticated_requests_session: Add Content-Type header
Add get_authenticated_aiohttp_session() to provide consistent HTTP client creation across all client types (httpx, requests, aiohttp). Features: - User-Agent header with flash version - Authorization header with API key support - Content-Type: application/json - Configurable timeout (default 300s for GraphQL) - TCPConnector with ThreadedResolver for DNS - API key override for worker-to-mothership propagation This enables consolidation of aiohttp session creation scattered across RunpodGraphQLClient and RunpodRestClient.
Refactor RunpodGraphQLClient and RunpodRestClient to use the new get_authenticated_aiohttp_session() factory instead of creating aiohttp.ClientSession directly. Benefits: - Removes ~18 lines of duplicated configuration - Consistent headers across all HTTP clients - Centralized timeout and connector configuration - Easier to test and maintain No behavior changes - same headers, timeout, and connector settings.
Refactor download_tarball() and upload_build() to use get_authenticated_requests_session() instead of direct requests.get/put. Benefits: - Automatic User-Agent header inclusion - Consistent with other HTTP operations - Proper session management with context manager - Centralized configuration Changes: - download_tarball: Use session factory for presigned URL download - upload_build: Use session factory with Content-Type override for tarball upload
Add tests for Content-Type headers and API key override functionality in existing factories. Add complete test suite for new aiohttp factory. New tests: - Content-Type header presence (httpx, requests, aiohttp) - API key override parameter (httpx, requests, aiohttp) - aiohttp session creation with User-Agent - aiohttp API key inclusion/exclusion - aiohttp custom/default timeouts (300s) All 26 tests passing.
- Move raise_for_status() inside session scope with try/finally in app.py - Add Content-Type header documentation to httpx and requests docstrings - Add use_threaded_resolver param to aiohttp factory (REST client passes False) - Add tests for use_threaded_resolver parameter behavior
…olution
Replace raw os.getenv("RUNPOD_API_KEY") with get_api_key() from
core.credentials in CLI-context code. This supports credentials.toml
fallback from 'flash login' in addition to environment variables.
Remove dead code from the abandoned per-request API key propagation concept. The LB handler no longer extracts inbound Bearer tokens, and stubs rely on the standard credentials module for API key resolution. - Delete runtime/api_key_context.py (contextvars-based propagation) - Remove extract_api_key_middleware from lb_handler.py - Remove context var usage from load_balancer_sls.py
7dc5ff2 to
fc2b70a
Compare
Summary
Consolidates HTTP client creation into centralized factories and ensures consistent Content-Type headers across all HTTP operations.
Changes
Add Content-Type headers to existing factories (3891b3a)
Content-Type: application/jsonAdd centralized aiohttp session factory (2c85b2b)
get_authenticated_aiohttp_session()functionRefactor RunPod API clients (aae56bd)
Refactor app.py tarball operations (ad5b9cb)
download_tarball()andupload_build()use centralized factoryAdd comprehensive test coverage (65401be)
Benefits
Test Plan