Core API#
The core API provides the main client interface, configuration management, and exception handling for the sgu-client library.
Main Client#
The SGUClient is the primary entry point for all API interactions. It provides a hierarchical interface to access different data domains.
Usage Example:
from sgu_client import SGUClient, SGUConfig
# Basic usage
client = SGUClient()
# With custom configuration
config = SGUConfig(timeout=60, debug=True, max_retries=5)
client = SGUClient(config=config)
# Using context manager
with SGUClient() as client:
stations = client.levels.observed.get_stations()
API Documentation:
Main SGU Client class.
- class sgu_client.sgu_client.SGUClient(config=None)[source]#
Bases:
objectMain client for interacting with SGU API.
This is the primary interface users will interact with.
Example
>>> client = SGUClient() >>> stations = client.levels.observed.get_stations() >>> measurements = client.levels.observed.get_measurements()
>>> # With custom config >>> config = SGUConfig(timeout=60, debug=True) >>> client = SGUClient(config=config)
- Parameters:
config (
SGUConfig|None)
- __init__(config=None)[source]#
Initialize the SGU client.
- Parameters:
config (
SGUConfig|None) – Configuration object. If None, uses default config.
Configuration#
The SGUConfig class manages all configuration options for the SGU client, including API endpoints, timeouts, retry behavior, and logging.
Configuration Options:
timeout: Request timeout in seconds (default: 30)
max_retries: Maximum number of retry attempts (default: 3)
debug: Enable debug logging (default: False)
base_url: Override default API base URL (advanced usage)
Configuration management for SGU Client.
- class sgu_client.config.SGUConfig(**data)[source]#
Bases:
BaseModelConfiguration settings for SGU API client.
- Parameters:
data (
Any)
-
base_url:
str#
-
timeout:
float#
-
max_retries:
int#
-
user_agent:
str#
-
log_level:
str|int|None#
- model_config: ClassVar[ConfigDict] = {'extra': 'forbid', 'frozen': True}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- sgu_client.config.setup_logging(log_level)[source]#
Configure logging for the SGU client.
This function sets up logging with a standard format that includes timestamp, logger name, level, and message. It uses a singleton pattern to ensure logging is only configured once per process.
- Parameters:
log_level (
str|int|None) – Logging level to use. Can be: - String: “DEBUG”, “INFO”, “WARNING”, “ERROR”, “CRITICAL” - Integer: logging.DEBUG, logging.INFO, etc. - None: Skip logging configuration (user manages their own logging)- Return type:
None
Example
>>> setup_logging("DEBUG") >>> setup_logging(logging.INFO) >>> setup_logging(None) # Don't configure logging
Exceptions#
The library provides specific exception classes for different error scenarios, all inheriting from SGUClientError.
Exception Hierarchy:
SGUClientError- Base exceptionAPIError- API-related errorsValidationError- Data validation errorsNetworkError- Network communication errors
Custom exceptions for SGU Client.
- exception sgu_client.exceptions.SGUClientError[source]#
Bases:
ExceptionBase exception for all SGU client errors.
- exception sgu_client.exceptions.SGUAPIError(message, status_code=None, response_data=None)[source]#
Bases:
SGUClientErrorRaised when the SGU API returns an error response.
- Parameters:
message (
str)status_code (
int|None)response_data (
dict|None)
- exception sgu_client.exceptions.SGUConnectionError[source]#
Bases:
SGUClientErrorRaised when connection to SGU API fails.
- exception sgu_client.exceptions.SGUTimeoutError[source]#
Bases:
SGUClientErrorRaised when a request to SGU API times out.
- exception sgu_client.exceptions.SGUValidationError[source]#
Bases:
SGUClientErrorRaised when data validation fails.
Base Client#
The BaseClient provides low-level HTTP communication with retry logic and error handling. Most users won’t interact with this directly.
Base HTTP client for SGU API.
- class sgu_client.client.base.BaseClient(config=None)[source]#
Bases:
objectBase HTTP client with common functionality for SGU API.
- Parameters:
config (
SGUConfig|None)
- __init__(config=None)[source]#
Initialize the base client.
- Parameters:
config (
SGUConfig|None) – Configuration object. If None, uses default config.
- get(endpoint, params=None, base_url=None, **kwargs)[source]#
Make a GET request.
- Parameters:
endpoint (
str) – API endpoint pathparams (
dict[str,Any] |None) – Query parametersbase_url (
str|None) – Optional override for base URL**kwargs – Additional arguments passed to requests
- Return type:
dict[str,Any]- Returns:
JSON response data
- Raises:
SGUConnectionError – If connection fails
SGUTimeoutError – If request times out
SGUAPIError – If API returns an error
- post(endpoint, data=None, **kwargs)[source]#
Make a POST request.
- Parameters:
endpoint (
str) – API endpoint pathdata (
dict[str,Any] |None) – Request body data**kwargs – Additional arguments passed to requests
- Return type:
dict[str,Any]- Returns:
JSON response data
- Raises:
SGUConnectionError – If connection fails
SGUTimeoutError – If request times out
SGUAPIError – If API returns an error
See Also#
Groundwater Levels API - Groundwater levels API using this core infrastructure
Groundwater Chemistry API - Groundwater chemistry API using this core infrastructure