Underlying HTTP Client
Relevant source files
The following files were used as context for generating this wiki page:
The Underlying HTTP Client provides the core communication layer for the FACEIT Python library. It handles authentication, request execution, and error management using the httpx library. This system supports both synchronous and asynchronous operations and implements robust retry logic and adaptive rate limiting to ensure reliable interactions with the FACEIT Data API.
Sources: src/faceit/http/client.py:64-70, README.md:15-18
Client Architecture
The client architecture follows an inheritance-based structure starting from an abstract base class. This design separates shared logic, such as endpoint building and header preparation, from the specific execution models of synchronous and asynchronous clients.
Class Hierarchy
The following diagram illustrates the relationship between the client classes:
Core Components and Configuration
The BaseAPIClient defines several default constants used across all client instances.
| Parameter | Default Value | Description |
|---|---|---|
DEFAULT_BASE_URL | https://open.faceit.com/data/v4 | The root URL for FACEIT Data API requests. |
DEFAULT_TIMEOUT | 10.0 | Default timeout in seconds for HTTP requests. |
DEFAULT_API_KEY_ENV | FACEIT_SECRET | The environment variable name used to fetch the API key. |
Sources: src/faceit/http/client.py:80-82
Authentication and Security
The client uses API key authentication passed via the Authorization header as a Bearer token. It includes a validator to ensure the API key follows a valid UUID format before making requests.
- Header Construction: Every request includes
Accept: application/jsonandAuthorization: Bearer <API_KEY>. - Masking: The
api_keyproperty masks the sensitive token, only showing the first and last four characters. - Environment Loading: If an instance is initialized with
envtypes, the client attempts to load the secret usingpython-decouple.
Sources: src/faceit/http/client.py:108-111, src/faceit/http/client.py:137-142, src/faceit/http/client.py:165-173
Retry Logic and Exception Handling
The client integrates the tenacity library to handle transient failures. It automatically retries requests when encountering specific exceptions or HTTP status codes.
Retry Trigger Conditions
The client retries under the following conditions:
- Network Issues:
httpx.TimeoutException,httpx.ConnectError, orhttpx.RemoteProtocolError. - Retryable Status Codes: 429 (Too Many Requests), 502 (Bad Gateway), 503 (Service Unavailable), and 504 (Gateway Timeout).
Sources: src/faceit/http/client.py:85-94, src/faceit/http/helpers.py:30-41
Response Processing Flow
Asynchronous Adaptive Limits
The AsyncClient implements an adaptive rate-limiting mechanism specifically to handle SSL connection errors, which were observed during concurrent testing.
Concurrency Controls
- Semaphore: A class-wide
asyncio.Semaphorerestricts the total number of concurrent requests across all async client instances. - Adaptive Reduction: If SSL errors exceed a threshold (default 5), the client halves the
max_concurrent_requestsuntil it reaches a minimum floor. - Recovery: The client periodically checks for stability. If no SSL errors occur for a recovery interval (default 300s), it gradually increases the concurrency limit.
Sources: src/faceit/http/client.py:255-274, src/faceit/http/client.py:349-373, src/faceit/http/client.py:382-411
Adaptive Configuration Parameters
| Parameter | Default | Description |
|---|---|---|
MAX_CONCURRENT_REQUESTS_ABSOLUTE | 100 | Hard ceiling for concurrent requests. |
DEFAULT_MAX_CONCURRENT_REQUESTS | 30 | Initial limit for concurrent requests. |
DEFAULT_SSL_ERROR_THRESHOLD | 5 | Number of SSL errors before triggering reduction. |
DEFAULT_MIN_CONNECTIONS | 5 | Minimum floor for adaptive reduction. |
DEFAULT_RECOVERY_INTERVAL | 300 | Seconds of stability required before increasing limit. |
Sources: src/faceit/http/client.py:277-285
Endpoint Construction
The client provides utilities to build absolute URLs from path parts using an internal Endpoint helper. This helper manages base URL joining and trailing slash consistency.
# Example of internal endpoint building
def create_endpoint(self, *path_parts: str) -> Endpoint:
return Endpoint(*path_parts, base=self.base_url)
Sources: src/faceit/http/client.py:144-145, src/faceit/http/helpers.py:44-65
Summary
The Underlying HTTP Client acts as the engine of the library, abstracting the complexities of httpx and API-specific requirements. It ensures that developers can interact with FACEIT endpoints through a type-safe interface (SyncClient or AsyncClient) while the client internally manages authentication security, transient failure recovery via tenacity, and network stability via adaptive rate limiting.