base_client
base_client
Base HTTP client for ETM API communication.
Classes
BaseClient
HTTP client with async capabilities for ETM API communication.
Each instance is independent and can be configured with different tokens and base URLs, allowing multiple clients in the same script.
Initialize the BaseClient with authentication and connection details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
Optional[str]
|
API authentication token. If None, uses the token from application settings. |
None
|
base_url
|
Optional[str]
|
Base URL for API requests. If None, uses the base URL from application settings. |
None
|
Source code in src/pyetm/clients/base_client.py
Functions
close
Clean up resources and close the session.
This method should be called when the client is no longer needed to properly release network connections and other resources.
AsyncBatchRunner
Utility class for executing multiple HTTP requests concurrently.
This class provides both asynchronous and synchronous methods for batch processing of HTTP requests with proper error handling and result wrapping.
Functions
batch_requests
async
staticmethod
Execute multiple requests concurrently using asyncio.
This method processes all requests in parallel and returns results in the same order as the input requests. Each result is wrapped in a ServiceResult for consistent error handling. Results can be either dict (for JSON responses) or ETMResponse objects (for non-JSON responses like CSV).
Source code in src/pyetm/clients/base_client.py
batch_requests_sync
staticmethod
Synchronous wrapper for batch_requests method.
This method provides a synchronous interface to the async batch_requests functionality by running it in the session's event loop using asyncio.run_coroutine_threadsafe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
ETMSession
|
Active ETM session instance for making requests. |
required |
requests
|
List[dict]
|
List of request specifications with the same format as batch_requests method. |
required |
Returns:
| Type | Description |
|---|---|
List[ServiceResult[Optional[Dict[str, Any]]]]
|
List[ServiceResult]: List of ServiceResult objects containing either success data or error information for each request. |
Note
This method blocks until all requests are completed and should only be used when async/await syntax is not available in the calling context.
Source code in src/pyetm/clients/base_client.py
Functions
get_client
cached
Get the default BaseClient instance.
This function returns a cached client instance configured with settings from environment variables or .env file. For simple use cases, this provides a convenient default client without needing to explicitly create one.
For advanced use cases requiring multiple clients with different configurations, create BaseClient instances directly.
Returns:
| Name | Type | Description |
|---|---|---|
BaseClient |
BaseClient
|
Cached default client instance |
Example
from pyetm.clients import get_client client = get_client() # Uses env vars response = client.session.get("/scenarios/123")
Source code in src/pyetm/clients/base_client.py
make_batch_requests
Convenience function for making batch requests using a BaseClient instance.
This helper function extracts the session from a BaseClient and delegates to AsyncBatchRunner.batch_requests_sync for execution.