Firebase Realtime Database library for kiarina namespace
Project description
kiarina-lib-firebase-rtdb
English | 日本語
[!NOTE] What is this? An asynchronous package for retrieving data from Firebase Realtime Database and watching real-time changes.
Dependencies
| Package | Version | License |
|---|---|---|
| HTTPX | >=0.28.1 |
BSD-3-Clause |
| kiarina-lib-firebase | >=2.1.0 |
MIT |
| Pydantic | >=2.10.6 |
MIT |
| Pydantic Settings | >=2.10.1 |
MIT |
| pydantic-settings-manager | >=3.2.0 |
MIT |
Installation
pip install kiarina-lib-firebase-rtdb
Features
- Retrieving Data Retrieves data at a path through the Firebase Realtime Database REST API.
- Watching Data Changes
Receives
putandpatchevents through Server-Sent Events. - Recovering the Stream Refreshes the ID token after authentication revocation and reconnects with exponential backoff after network errors.
- Stopping the Stream
Stops a watch with an
asyncio.Event. - Configuring Retries Configures retry intervals through environment variables or pydantic-settings-manager.
Retrieving Data
Get an ID token from TokenManager and specify a database path.
from kiarina.lib.firebase import TokenManager
from kiarina.lib.firebase_rtdb import get_data
token_manager = TokenManager(
api_key="firebase-web-api-key",
refresh_token="firebase-refresh-token",
)
data = await get_data(
"https://your-project-default-rtdb.firebaseio.com",
"/agents/state",
await token_manager.get_id_token(),
)
Watching Data Changes
watch_data yields put events for complete replacements and patch events for partial updates.
from kiarina.lib.firebase_rtdb import watch_data
async for event in watch_data(
"https://your-project-default-rtdb.firebaseio.com",
"/agents/state",
token_manager,
):
print(event.event_type, event.path, event.data)
When authentication is revoked, it calls TokenManager.refresh() and reconnects immediately. Network errors use the configured exponential backoff.
Stopping the Stream
Setting stop_event ends the watch when the stream next receives data. Cancel the watch task when an immediate stop is required.
import asyncio
from kiarina.lib.firebase_rtdb import watch_data
stop_event = asyncio.Event()
async for event in watch_data(
"https://your-project-default-rtdb.firebaseio.com",
"/agents/state",
token_manager,
stop_event=stop_event,
):
print(event.data)
if event.data == "stop":
stop_event.set()
Configuring Retries
Retry settings use a single-mode settings_manager.
kiarina.lib.firebase_rtdb:
max_retry_delay: 60.0
initial_retry_delay: 1.0
retry_delay_multiplier: 2.0
Load the settings when the application starts.
import yaml
from pydantic_settings_manager import load_user_configs
from kiarina.lib.firebase_rtdb import settings_manager
with open("config.yaml", encoding="utf-8") as file:
load_user_configs(yaml.safe_load(file) or {})
settings = settings_manager.get_settings()
To configure only this package, assign the values directly to settings_manager.user_config.
from kiarina.lib.firebase_rtdb import settings_manager
settings_manager.user_config = {
"max_retry_delay": 60.0,
"initial_retry_delay": 1.0,
"retry_delay_multiplier": 2.0,
}
The same values are available as environment variables.
export KIARINA_LIB_FIREBASE_RTDB_MAX_RETRY_DELAY=60.0
export KIARINA_LIB_FIREBASE_RTDB_INITIAL_RETRY_DELAY=1.0
export KIARINA_LIB_FIREBASE_RTDB_RETRY_DELAY_MULTIPLIER=2.0
API Reference
kiarina.lib.firebase_rtdb
from kiarina.lib.firebase_rtdb import (
DataChangeEvent,
RTDBSettings,
RTDBStreamCancelledError,
get_data,
settings_manager,
watch_data,
)
get_data
async def get_data(
database_url: str,
path: str,
id_token: str,
) -> Any: ...
Retrieves JSON data at the specified path.
Parameters
database_url(str): Firebase Realtime Database URLpath(str): Path of the data to retrieveid_token(str): Firebase ID token
Returns
Any: JSON value from the response
Raises
httpx.HTTPStatusError: The HTTP response indicates an errorhttpx.HTTPError: The request fails
watch_data
async def watch_data(
database_url: str,
path: str,
token_manager: TokenManager,
*,
stop_event: asyncio.Event | None = None,
) -> AsyncIterator[DataChangeEvent]: ...
Watches the specified path and yields data changes from the Firebase SSE stream.
Parameters
database_url(str): Firebase Realtime Database URLpath(str): Path of the data to watchtoken_manager(TokenManager): Instance that manages the ID tokenstop_event(asyncio.Event | None): Event that requests the watch to stop
Yields
DataChangeEvent: Aputorpatchdata change
Raises
RTDBStreamCancelledError: Firebase cancels the stream
Network errors are retried internally. Other unexpected exceptions are propagated to the caller.
DataChangeEvent
@dataclass
class DataChangeEvent:
event_type: Literal["put", "patch"]
path: str
data: Any
A data change received from Firebase Realtime Database.
Fields
event_type(Literal["put", "patch"]): Event typepath(str): Relative path that changeddata(Any): Updated data
RTDBSettings
class RTDBSettings(BaseSettings):
max_retry_delay: float = 60.0
initial_retry_delay: float = 1.0
retry_delay_multiplier: float = 2.0
Settings used when reconnecting a stream.
Fields
max_retry_delay(float): Maximum retry interval in secondsinitial_retry_delay(float): Initial retry interval in secondsretry_delay_multiplier(float): Value multiplied by the retry interval after a network error
settings_manager
settings_manager: SettingsManager[RTDBSettings]
Manages a single RTDBSettings configuration.
RTDBStreamCancelledError
class RTDBStreamCancelledError(Exception): ...
Indicates that Firebase cancelled the SSE stream.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kiarina_lib_firebase_rtdb-2.3.1.tar.gz.
File metadata
- Download URL: kiarina_lib_firebase_rtdb-2.3.1.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f14525f233b6043e2a6d22ed1151abaedcfdd17b6cfee2ebfa09db83de856a11
|
|
| MD5 |
c0488254b9027e79f50993c83d6ac6d7
|
|
| BLAKE2b-256 |
932e598f536c195b83c8222c379ae9929fa780ff23475ed38f8481f5de89c666
|
Provenance
The following attestation bundles were made for kiarina_lib_firebase_rtdb-2.3.1.tar.gz:
Publisher:
release-pypi.yml on kiarina/kiarina-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kiarina_lib_firebase_rtdb-2.3.1.tar.gz -
Subject digest:
f14525f233b6043e2a6d22ed1151abaedcfdd17b6cfee2ebfa09db83de856a11 - Sigstore transparency entry: 2044312909
- Sigstore integration time:
-
Permalink:
kiarina/kiarina-python@443dbb48340706ef8cf35551de167d64bc11ee82 -
Branch / Tag:
refs/tags/v2.3.1 - Owner: https://github.com/kiarina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-pypi.yml@443dbb48340706ef8cf35551de167d64bc11ee82 -
Trigger Event:
push
-
Statement type:
File details
Details for the file kiarina_lib_firebase_rtdb-2.3.1-py3-none-any.whl.
File metadata
- Download URL: kiarina_lib_firebase_rtdb-2.3.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
344d1babdc9f6432c6e648912be3194c7cbc15a193792bb334465017427e1bc1
|
|
| MD5 |
a30a6d85385a35a2a104957ffed519c7
|
|
| BLAKE2b-256 |
4555137073f456d6e46a0a013ad9484c0ceb2436cb390060d7d9d188d0fa79b4
|
Provenance
The following attestation bundles were made for kiarina_lib_firebase_rtdb-2.3.1-py3-none-any.whl:
Publisher:
release-pypi.yml on kiarina/kiarina-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kiarina_lib_firebase_rtdb-2.3.1-py3-none-any.whl -
Subject digest:
344d1babdc9f6432c6e648912be3194c7cbc15a193792bb334465017427e1bc1 - Sigstore transparency entry: 2044313207
- Sigstore integration time:
-
Permalink:
kiarina/kiarina-python@443dbb48340706ef8cf35551de167d64bc11ee82 -
Branch / Tag:
refs/tags/v2.3.1 - Owner: https://github.com/kiarina
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-pypi.yml@443dbb48340706ef8cf35551de167d64bc11ee82 -
Trigger Event:
push
-
Statement type: