Base Client¶
base_client
¶
Base HTTP client for all Griddy SDK modules.
This module provides the BaseClient class, which implements common HTTP functionality including rate limiting, automatic retries, and error handling.
Example
from griddy.core import BaseClient client = BaseClient( ... base_url="https://api.example.com", ... timeout=30, ... rate_limit_delay=1.0, ... ) response = client.get("/endpoint", params={"key": "value"}) client.close()
Classes¶
BaseClient
¶
BaseClient(
base_url: str,
timeout: int = 30,
max_retries: int = 3,
rate_limit_delay: float = 1.0,
headers: Dict[str, str] | None = None,
cookies_file: str | None = None,
)
Base HTTP client with common functionality for all data source clients.
This class provides a foundation for building API clients with built-in support for rate limiting, automatic retries on transient failures, and consistent error handling.
| ATTRIBUTE | DESCRIPTION |
|---|---|
base_url |
The base URL for all API requests.
|
timeout |
Request timeout in seconds.
|
rate_limit_delay |
Minimum delay between requests in seconds.
|
session |
The underlying requests Session object.
|
cookies_file |
Path to a cookies file for authentication.
|
Example
client = BaseClient( ... base_url="https://api.nfl.com", ... timeout=30, ... max_retries=3, ... rate_limit_delay=1.0, ... ) try: ... data = client.get("/games", params={"season": 2024}) ... finally: ... client.close()
Initialize the base client.
Creates an HTTP session with automatic retry capabilities for transient errors (429, 500, 502, 503, 504 status codes).
| PARAMETER | DESCRIPTION |
|---|---|
base_url
|
Base URL for the API (e.g., "https://api.nfl.com"). Trailing slashes are automatically removed.
TYPE:
|
timeout
|
Request timeout in seconds. Defaults to 30.
TYPE:
|
max_retries
|
Maximum number of retries for failed requests. Defaults to 3.
TYPE:
|
rate_limit_delay
|
Minimum delay between requests in seconds to avoid rate limiting. Defaults to 1.0.
TYPE:
|
headers
|
Additional headers to include in all requests. These are merged with default headers (Accept, Content-Type).
TYPE:
|
cookies_file
|
Path to a Netscape-format cookies file for authentication.
TYPE:
|
Example
client = BaseClient( ... base_url="https://api.example.com", ... timeout=60, ... max_retries=5, ... rate_limit_delay=2.0, ... headers={"Authorization": "Bearer token"}, ... )
Source code in griddy/core/base_client.py
Functions¶
get
¶
get(
endpoint: str,
params: Dict[str, Any] | None = None,
headers: Dict[str, str] | None = None,
) -> Dict[str, Any] | List[Any]
Make a GET request to the API.
Automatically applies rate limiting and handles errors.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint path (e.g., "/games" or "players/123"). Leading slashes are handled automatically.
TYPE:
|
params
|
Query parameters to include in the request.
TYPE:
|
headers
|
Additional headers to include in this request only.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any] | List[Any]
|
Parsed JSON response data. |
| RAISES | DESCRIPTION |
|---|---|
NotFoundError
|
When the resource is not found (404). |
AuthenticationError
|
When authentication fails (401). |
RateLimitError
|
When rate limit is exceeded (429). |
APIError
|
For all other error status codes. |
Example
data = client.get( ... "/games", ... params={"season": 2024, "week": 1}, ... )
Source code in griddy/core/base_client.py
post
¶
post(
endpoint: str,
data: Dict[str, Any] | None = None,
json_data: Dict[str, Any] | None = None,
headers: Dict[str, str] | None = None,
) -> Dict[str, Any]
Make a POST request to the API.
Automatically applies rate limiting and handles errors.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint path.
TYPE:
|
data
|
Form data to send in the request body.
TYPE:
|
json_data
|
JSON data to send in the request body.
TYPE:
|
headers
|
Additional headers to include in this request only.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, Any]
|
Parsed JSON response data. |
| RAISES | DESCRIPTION |
|---|---|
NotFoundError
|
When the resource is not found (404). |
AuthenticationError
|
When authentication fails (401). |
RateLimitError
|
When rate limit is exceeded (429). |
APIError
|
For all other error status codes. |
Example
result = client.post( ... "/auth/token", ... json_data={"username": "user", "password": "pass"}, ... )
Source code in griddy/core/base_client.py
close
¶
Close the HTTP session and release resources.
Should be called when the client is no longer needed to properly clean up connections.
Example
client = BaseClient(base_url="https://api.example.com") try: ... data = client.get("/endpoint") ... finally: ... client.close()