pybragerone.api.client¶
HTTP API client for BragerOne.
Classes
|
HTTP API client with idempotent login and auto-refresh. |
Simple HTTP cache using ETag/Last-Modified headers with in-memory body storage. |
Exceptions
|
Raised for non-2xx HTTP responses. |
- exception pybragerone.api.client.ApiError(status: int, data: Any, headers: dict[str, str] | None = None)[source]¶
Bases:
RuntimeErrorRaised for non-2xx HTTP responses.
- class pybragerone.api.client.BragerOneApiClient(*, server: ServerConfig | None = None, api_base: str = 'https://io.brager.pl/v1', one_base: str = 'https://one.brager.pl', io_base: str | None = None, token_store: TokenStore | None = None, enable_http_trace: bool = False, redact_secrets: bool = True, creds_provider: Callable[[], tuple[str, str]] | None = None, validate_on_start: bool = True, refresh_leeway: int = 90, timeout: float = 8.0, concurrency: int = 4, verify: bool | str | SSLContext = True)[source]¶
Bases:
objectHTTP API client with idempotent login and auto-refresh.
Provides a comprehensive HTTP client for the BragerOne API with automatic token management, authentication, and retry logic.
- Parameters:
server (ServerConfig | None)
api_base (str)
one_base (str)
io_base (str | None)
token_store (TokenStore | None)
enable_http_trace (bool)
redact_secrets (bool)
creds_provider (CredProvider | None)
validate_on_start (bool)
refresh_leeway (int)
timeout (float)
concurrency (int)
verify (bool | str | ssl.SSLContext)
- property access_token: str¶
Get the current access token.
- Returns:
Current access token string.
- Raises:
RuntimeError – If the client is not authenticated yet.
- async close()[source]¶
Close the underlying HTTP session.
This should be called when the client is no longer needed to properly release resources.
- Return type:
- async ensure_auth(email: str | None = None, password: str | None = None)[source]¶
Ensure valid token: use cache + validation, and if missing/expired — login.
- async fetch_json_one(path: str)[source]¶
Fetch JSON content from ONE_BASE.
- Parameters:
path (
str) – The path to fetch from.- Returns:
tuple[int,dict[str,Any] |list[Any] |None] – Tuple of (status_code, json_data).- Return type:
Note
Assets are JS, so JSON extraction should be done catalog-side, not here. This method is here only for symmetry; you may not use it.
- async get_bytes(url: str)[source]¶
Get raw bytes from URL with caching (ETag/Last-Modified).
- Parameters:
url (
str) – The URL to fetch.- Returns:
bytes– The raw bytes of the response.- Raises:
ApiError – If the request fails.
- Return type:
Note
This method uses an in-memory cache to store ETag and Last-Modified headers to optimize subsequent requests to the same URL.
- async get_module_card(code: str)[source]¶
Get module card information by code.
- Parameters:
code (
str) – The module code identifier.- Returns:
ModuleCard– Module card information as a Pydantic model.- Raises:
ApiError – If the request fails.
- Return type:
- async get_modules(object_id: int)[source]¶
GET /v1/modules?page=1&limit=999&group_id=<object_id> → list of modules.
- async get_object(object_id: int)[source]¶
Get object by ID.
- Parameters:
object_id (
int) – The object identifier.- Returns:
ObjectDetails– Object details with operational status.- Raises:
ApiError – If the request fails.
- Return type:
- async get_object_permissions(object_id: int)[source]¶
Get object-specific permissions for the current user.
- Parameters:
object_id (
int) – The ID of the object to get permissions for.- Returns:
list[Permission] – List of object-specific permissions.- Raises:
ApiError – If the request fails.
- Return type:
- async get_objects()[source]¶
GET /v1/objects → list of objects (with tolerance for different shapes).
- Returns:
list[BragerObject] – List of BragerObject models.- Return type:
- async get_system_version()[source]¶
Get system version information.
- Returns:
SystemVersion– System version information as a Pydantic model.- Raises:
ApiError – If the request fails.
- Return type:
- async get_user_permissions()[source]¶
Get current user permissions.
- Returns:
list[Permission] – List of user permissions.- Raises:
ApiError – If the request fails.
- Return type:
- async module_command(*, devid: str, pool: str, parameter: str, value: Any, parameter_name: str | None = None, unit: int | None = None, extra_payload: Mapping[str, Any] | None = None, return_data: bool = False)[source]¶
Dispatch a parameter-like command payload to a module.
This method posts to
/v1/module/commandand mirrors the web application’s payload shape for non-raw writes.- Parameters:
devid (
str) – Module device identifier.pool (
str) – Parameter pool/group (for example"P4").parameter (
str) – Parameter channel/index token (for example"v8").value (
Any) – Value to be written.unit (
int|None) – Optional unit identifier expected by backend for selected writes.extra_payload (
Mapping[str,Any] |None) – Optional additional keys to include in the request payload.return_data (
bool) – Whether to return(status, data)instead of only success.
- Returns:
tuple[int,Any] |bool– Tuple of(status, data)whenreturn_data=True; otherwise a boolean indicating request success.- Return type:
- async module_command_auto(*, devid: str, value: Any | None = None, command: str | None = None, pool: str | None = None, parameter: str | None = None, parameter_name: str | None = None, unit: int | None = None, extra_payload: Mapping[str, Any] | None = None, return_data: bool = False)[source]¶
Automatically dispatch via
/module/commandor/module/command/raw.- Routing rules:
If
commandis provided, dispatches through/module/command/raw.If both
poolandparameterare provided, dispatches through/module/command.If both route styles are provided at once, raises
ValueError.If no complete route style is provided, raises
ValueError.
- Parameters:
devid (
str) – Module device identifier.value (
Any|None) – Command/write value. Optional for raw commands.command (
str|None) – Symbolic command for/module/command/raw.pool (
str|None) – Parameter pool/group for/module/command.parameter (
str|None) – Parameter channel/index token for/module/command.parameter_name (
str|None) – Optional display/name token for/module/command.unit (
int|None) – Optional unit identifier for/module/command.extra_payload (
Mapping[str,Any] |None) – Optional additional keys to include in request payload.return_data (
bool) – Whether to return(status, data)instead of only success.
- Returns:
tuple[int,Any] |bool– Tuple of(status, data)whenreturn_data=True; otherwise a boolean indicating request success.- Return type:
- async module_command_raw(*, devid: str, command: str, value: Any | None = None, extra_payload: Mapping[str, Any] | None = None, return_data: bool = False)[source]¶
Dispatch a symbolic command to a module.
This method matches the web application’s command transport and posts to
/v1/module/command/raw.- Parameters:
devid (
str) – Module device identifier.command (
str) – Symbolic command name (for example"MODULE_RESTART").value (
Any|None) – Optional command value (for example"ONLINE"or"OFF").extra_payload (
Mapping[str,Any] |None) – Optional additional keys to include in the request payload.return_data (
bool) – Whether to return(status, data)instead of only success.
- Returns:
tuple[int,Any] |bool– Tuple of(status, data)whenreturn_data=True; otherwise a boolean indicating request success.- Return type:
- async modules_activity_quantity_prime(modules: list[str], *, return_data: bool = False)[source]¶
Prime modules activity quantity.
- async modules_connect(wsid_ns: str, modules: list[str], group_id: int | None = None, engine_sid: str | None = None)[source]¶
Connect to modules via WebSocket.
- Parameters:
- Returns:
bool– True if connection successful, False otherwise.- Return type:
- async modules_parameters_prime(modules: list[str], *, return_data: bool = False)[source]¶
Prime modules parameters.
- async revoke()[source]¶
Server-side logout + local cleanup of token and persistence.
Attempts to revoke the token on the server and cleans up local state regardless of server response.
- Return type:
- set_token_store(store: TokenStore | None)[source]¶
Wire token persistence to a store that exposes load/save/clear.
- Parameters:
store (
TokenStore|None) – Token storage instance or None to disable persistence.- Return type:
- class pybragerone.api.client.HttpCache[source]¶
Bases:
objectSimple HTTP cache using ETag/Last-Modified headers with in-memory body storage.
This cache implementation stores response bodies along with their cache headers to enable efficient HTTP caching with proper conditional request handling.
- headers_for(url: str)[source]¶
Generate conditional request headers for cached URL.
Returns appropriate If-None-Match and If-Modified-Since headers based on cached ETag and Last-Modified values for the given URL.