Renewable token bearer and receiver.
This module implements a wrapper around an existing asynchronous Bearer that provides automatic background token refresh and a light-weight per-request Receiver implementation which delegates the actual token fetch to the wrapped bearer while supporting retry accounting.
The primary public types provided here are:
Receiver-- per-request receiver that calls the parent- bearer to obtain a token and tracks retry attempts.
Bearer-- a bearer that wraps another bearer and keeps a- cached token refreshed in the background. It provides synchronous and asynchronous renewal request modes and convenient shutdown semantics.
Notes
The renewal bearer starts a background task on the first token fetch and keeps the cached token refreshed up to a configurable safe fraction of the token lifetime. When a fetch request requires a renewed token it may either wait for the renewal to complete (synchronous mode) or trigger a background renewal and wait for the cached token to become fresh (asynchronous mode).
Examples
Basic usage (asynchronous renewal):
from nebius.aio.token.static import Bearer as StaticBearer
from nebius.aio.token.renewable import Bearer as RenewableBearer
import asyncio
async def demo():
src = StaticBearer("my-static-token")
bearer = RenewableBearer(src)
receiver = bearer.receiver()
tok = await receiver.fetch(timeout=5)
print(tok)
asyncio.run(demo())
Synchronous renewal request (waits for a fresh token, raising on timeout):
from nebius.aio.token.static import Bearer as StaticBearer
from nebius.aio.token.renewable import Bearer as RenewableBearer
from nebius.aio.token.renewable import (
OPTION_RENEW_SYNCHRONOUS,
OPTION_RENEW_REQUEST_TIMEOUT,
)
import asyncio
async def sync_demo():
src = StaticBearer("my-static-token")
bearer = RenewableBearer(src)
receiver = bearer.receiver()
options = {
OPTION_RENEW_SYNCHRONOUS: "1",
OPTION_RENEW_REQUEST_TIMEOUT: "2.0",
}
tok = await receiver.fetch(timeout=5, options=options)
print(tok)
asyncio.run(sync_demo())
| Class | |
Bearer that keeps a shared cached token refreshed in the background. |
| Class | |
Per-request receiver that delegates fetching to the parent renewable bearer while accounting for retry attempts. |
| Exception | |
Raised when a renewal operation is requested but the bearer is already stopped. |
| Exception | |
Base exception raised for renewal-related failures. |
| Constant | VERY |
If the timeout is not specified, this value will be used as the default. |
| Type Variable | T |
Undocumented |
| Variable | log |
Undocumented |