refactor: consolidate HTTP client factories with Content-Type headers#203
refactor: consolidate HTTP client factories with Content-Type headers#203
Conversation
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.
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.
|
|
||
| resp.raise_for_status() |
There was a problem hiding this comment.
In upload_build, the requests response is created inside the session context but raise_for_status() is called after the session is closed, and the response is never explicitly closed. To avoid leaking connections / leaving the response unconsumed, keep status checking (and ideally resp.close()/context management) within the session scope so the response lifecycle is clearly bounded.
| resp.raise_for_status() | |
| try: | |
| resp.raise_for_status() | |
| finally: | |
| resp.close() |
| headers = { | ||
| "User-Agent": get_user_agent(), | ||
| "Content-Type": "application/json", | ||
| } |
There was a problem hiding this comment.
get_authenticated_httpx_client now always sets a Content-Type: application/json header, but the docstring’s “Automatically includes” list doesn’t mention it. Please update the docstring so it accurately reflects the headers the factory applies by default.
| session = requests.Session() | ||
| session.headers["User-Agent"] = get_user_agent() | ||
| session.headers["Content-Type"] = "application/json" | ||
|
|
There was a problem hiding this comment.
get_authenticated_requests_session now sets Content-Type: application/json by default, but the docstring doesn’t list this in the “Automatically includes” section. Update the docstring to keep the documented default headers in sync with the implementation.
| if self.session is None or self.session.closed: | ||
| from runpod_flash.core.utils.user_agent import get_user_agent | ||
|
|
||
| timeout = aiohttp.ClientTimeout(total=300) # 5 minute timeout | ||
| self.session = aiohttp.ClientSession( | ||
| timeout=timeout, | ||
| headers={ | ||
| "User-Agent": get_user_agent(), | ||
| "Authorization": f"Bearer {self.api_key}", | ||
| "Content-Type": "application/json", | ||
| }, | ||
| from runpod_flash.core.utils.http import get_authenticated_aiohttp_session | ||
|
|
||
| self.session = get_authenticated_aiohttp_session( | ||
| timeout=300.0, # 5 minute timeout for REST operations | ||
| api_key_override=self.api_key, | ||
| ) |
There was a problem hiding this comment.
PR description says the RunPod API client refactor has “No behavior changes”, but RunpodRestClient._get_session() now uses get_authenticated_aiohttp_session(), which configures a TCPConnector with ThreadedResolver (the previous REST session did not). Either update the PR description to reflect this behavior change, or make the connector/resolver configurable so REST can preserve prior defaults.
* feat: cleanup flash deploy/undeploy/build command output format * fix: cleanup
* refactor: remove noisy @Remote decorator debug logs Remove debug logs from client.py that fire on every @Remote function/class: - RUNPOD_ENDPOINT_ID/FLASH_RESOURCE_NAME environment check logs - Local dev mode stub creation logs - is_local_function result logs - Original function return logs - Remote execution wrapper creation logs Also remove unused flash_resource_name variable that was only used in the removed debug log. These logs provide no actionable information during normal development and create substantial noise (5-10 lines per decorated item). * refactor: remove class serialization debug logs from execute_class.py Remove 5 debug log lines that fire for every @Remote class: - Cached class data log (line 60) - Retrieved cached class data log (lines 84-86) - Successfully extracted class code log (line 125) - Generated cache key log (line 185) - Created remote class wrapper log (line 232) These logs fire 5-10 times per run and only matter when debugging class serialization issues, not during normal development. * refactor: remove per-resource discovery debug logs Remove 2 debug log lines that fire for every decorated resource: - Entry point resource discovery log (lines 55-57) - Project directory resource discovery log (lines 408-410) These logs fire 10-20 times per run. The INFO-level summary logs already show total resource counts, making per-resource debug logs redundant. * refactor: remove duplicate parse failure debug logs from scanner.py Remove 6 duplicate debug log lines across three scanning passes: - First pass (resource configs): lines 77, 81 - Second pass (@Remote functions): lines 90, 94 - Third pass (function calls): lines 118, 122 These logs fire 3× per Python file during scanning (150+ logs for 50 files). Parse failures in dependencies are expected and not actionable. Keep SyntaxError warnings as they indicate actual issues. * refactor: remove per-request load balancer debug logs Remove 3 debug log lines that fire on every request: - ROUND_ROBIN selection log (lines 88-91) - LEAST_CONNECTIONS selection log (lines 112-115) - RANDOM selection log (line 128) These logs fire on EVERY request (100+ times per second in production) and would flood production systems with no actionable value. * refactor: comment out verbose API debug logs in runpod.py Comment out (not remove) 8 debug log lines in API methods: GraphQL (_execute_graphql): - GraphQL Query log - GraphQL Variables log - GraphQL Response Status log - GraphQL Response log REST (_execute_rest): - REST Request log - REST Data log - REST Response Status log - REST Response log These logs dump multi-KB JSON responses on every API call (10-50× per deploy operation). Commenting out preserves them for future debugging while silencing them during normal development. Add noqa comment to json import since it's only used in commented code. * refactor: remove verbose debug/info logs from resource_manager.py Removed 6 noisy logs that fire per-resource operation: - get_or_deploy_resource called with config dump - DRIFT DEBUG with existing/new config fields - Resource found in cache (per-lookup) - exists, reusing (per-reuse) - Resource NOT found in cache (per-deployment) - Config drift detected (redundant with warning log) * refactor: simplify DEBUG log format to remove logger name and file location Removed %(name)s | %(filename)s:%(lineno)d from DEBUG format. DEBUG and INFO now use the same clean format: timestamp | level | message Updated test to match new behavior. * refactor: remove structural change debug logs from serverless.py Removed 3 noisy logs: - Version-triggering changes detected (INFO) - Structural change in field (DEBUG, 2 occurrences) These logs fire during endpoint updates and provide no actionable value. * refactor: silence httpcore/httpx trace logs Set httpcore and httpx loggers to WARNING level to suppress verbose connection/request trace logs that appear in DEBUG mode: - connect_tcp.started/complete - start_tls.started/complete - send_request_headers/body - receive_response_headers These low-level HTTP transport logs provide no actionable value during normal development. * fix: prevent false redaction of Job IDs and Template IDs Replaced overly broad TOKEN_PATTERN with PREFIXED_KEY_PATTERN that only redacts tokens with known sensitive prefixes (sk-, key_, api_). This fixes false positives where Job IDs, Worker IDs, and Template IDs were being redacted even though they're not sensitive. Updated test to use prefixed token instead of generic long token. * refactor: remove verbose debug logs from build and API operations Removed repetitive and overly-verbose debug logs: - ignore.py: Remove per-file "Ignoring:" logs (pattern summary sufficient) - app.py: Remove "already hydrated" debug log - runpod.py: Remove logs that print full input_data/variables (finalizing upload, fetching environment, deploying environment) - runpod.py: Change template update logs from info to debug - serverless.py: Change template update log from info to debug These logs added noise without value. Pattern summaries and operation names provide sufficient context. * refactor: silence file lock and asyncio debug logs Removed verbose file locking and resource persistence logs: - file_lock.py: Remove "File lock acquired" and "File lock released" - resource_manager.py: Remove "Saved resources in .runpod/resources.pkl" - logger.py: Silence asyncio logger (prevents "Using selector: KqueueSelector") These operational details added noise without debugging value. * refactor: remove app hydration debug logs Removed: - runpod.py: "Fetching flash app by name for input:" - app.py: "Hydrating app" These operation-level logs add noise without debugging value.
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.
…runpod/flash into deanq/ae-2106-consolidated-http
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