class Bearer(ParentBearer): (source)
Constructor: Bearer(source, max_retries, lifetime_safe_fraction, initial_retry_timeout, ...)
Bearer that keeps a shared cached token refreshed in the background.
The Bearer wraps another ParentBearer and starts a
background task on the first fetch. The cached token is refreshed
proactively based on the token's expiration and the configured
lifetime_safe_fraction.
- The bearer supports two renewal modes for fetch requests:
- asynchronous: trigger a background renewal and wait for the
- cached token to become fresh;
- synchronous: block until a new token has been fetched (or until
- a specified request timeout elapses).
Example
Construct a bearer and use it to initialize the SDK:
from nebius.sdk import SDK
from nebius.aio.token.renewable import Bearer
from nebius.aio.token.static import Bearer as StaticBearer
some_bearer = StaticBearer("my-static-token") # for illustrative purposes
# Wrap some bearer with renewal
renewable_bearer = Bearer(some_bearer)
sdk = SDK(credentials=renewable_bearer)
| Parameters | |
| source | The inner bearer used to actually fetch tokens. |
| max | Maximum number of retry attempts performed by
receivers created by receiver. |
| lifetime | Fraction of remaining token lifetime after which a refresh will be scheduled (e.g. 0.9 refreshes when 90% of lifetime has passed). |
| initial | Initial retry delay used in a backoff when a refresh fails. |
| max | Maximum retry delay cap. |
| retry | Exponential backoff base used to grow retry delays between attempts. |
| refresh | Timeout used for individual refresh requests when contacting the inner bearer. |
| Method | __init__ |
Initialize the renewable bearer. |
| Method | bg |
Run a coroutine without awaiting or tracking, and log exceptions. |
| Async Method | close |
Close the bearer, cancelling background tasks and closing source. |
| Async Method | fetch |
Fetch a token, renewing it if necessary. |
| Method | is |
Return True when a renewal should be performed. |
| Method | receiver |
Return a per-request Receiver bound to this bearer. |
| Method | request |
Request a token renewal. |
| Method | stop |
Stop the renewal background task and unblock any waiters. |
| Property | wrapped |
Return the wrapped bearer. |
| Async Method | _fetch |
Fetch a single token from the inner bearer. |
| Async Method | _run |
Background loop that refreshes the token periodically. |
| Instance Variable | _break |
Undocumented |
| Instance Variable | _cache |
Undocumented |
| Instance Variable | _initial |
Undocumented |
| Instance Variable | _is |
Undocumented |
| Instance Variable | _is |
Undocumented |
| Instance Variable | _lifetime |
Undocumented |
| Instance Variable | _max |
Undocumented |
| Instance Variable | _max |
Undocumented |
| Instance Variable | _refresh |
Undocumented |
| Instance Variable | _refresh |
Undocumented |
| Instance Variable | _renew |
Undocumented |
| Instance Variable | _renew |
Undocumented |
| Instance Variable | _renew |
Undocumented |
| Instance Variable | _renewal |
Undocumented |
| Instance Variable | _renewal |
Undocumented |
| Instance Variable | _retry |
Undocumented |
| Instance Variable | _source |
Undocumented |
| Instance Variable | _synchronous |
Undocumented |
| Instance Variable | _tasks |
Undocumented |
Inherited from Bearer:
| Property | name |
Optional human-readable name for the bearer. |
ParentBearer, max_retries: int = 2, lifetime_safe_fraction: float = 0.9, initial_retry_timeout: timedelta = timedelta(timedelta = timedelta(float = 1.5, refresh_request_timeout: timedelta = timedelta(Initialize the renewable bearer.
Run a coroutine without awaiting or tracking, and log exceptions.
The coroutine is scheduled as a background task and any unhandled exception is logged. The returned task is tracked so it can be cancelled during shutdown.
| Parameters | |
coro:Awaitable[ | Awaitable to run in a fire-and-forget task. |
| Returns | |
Task[ | The created asyncio.Task instance. |
float | None = None, options: dict[ str, str] | None = None) -> Token:
(source)
¶
Fetch a token, renewing it if necessary.
The fetch operation may trigger a background renewal or perform a synchronous renewal based on the provided options. When a synchronous renewal is requested the caller waits for a new token to be fetched (or for the request timeout to expire).
| Parameters | |
timeout:float | None | Optional timeout in seconds for waiting for a token. |
options:dict[ | Optional mapping of request flags. Recognized keys include:
|
| Returns | |
Token | A fresh Token instance. |
| Raises | |
RenewalError | when the cache remains empty after renewal. |
Return True when a renewal should be performed.
Renewal is required when there is no cached token or when an explicit renewal request has been signalled.
Request a token renewal.
If the bearer is not stopped this clears the freshness flag and signals the background loop to perform a renewal as soon as possible.
Stop the renewal background task and unblock any waiters.
This method marks the bearer as stopped, clears the freshness event and signals any waiting synchronous renewal attempts to unblock. It is safe to call multiple times.
Fetch a single token from the inner bearer.
This helper performs a single attempt to obtain a token from self._source while cooperating with the synchronous-renewal signalling primitives. It returns the received token and updates the cached token on success.