class documentation

Bearer that composes a wrapped bearer with a file-backed cache.

Example

Wrap a custom bearer with a name and file cache:

from nebius.sdk import SDK
from nebius.aio.token.token import Bearer, Receiver, Token
from nebius.aio.token.file_cache.renewable_bearer import (
    RenewableFileCacheBearer
)

class SomeCustomHeavyLoadBearer(Bearer):
    def receiver(self) -> Receiver:
        return SomeReceiver()

class SomeReceiver(Receiver):
    async def _fetch(self, timeout=None, options=None) -> Token:
        # Simulate heavy load token fetch
        return Token("heavy-token")

    def can_retry(self, err, options=None) -> bool:
        return False

custom_bearer = SomeCustomHeavyLoadBearer()
cached_bearer = RenewableFileCacheBearer(custom_bearer)

sdk = SDK(credentials=cached_bearer)
Parameters
bearerWrapped bearer used for refreshing tokens. Must be named.
safety_marginSafety margin before token expiration.
cache_filePath to the file used for persistent cache.
throttleIn-memory throttle interval for cache reads.
metricsOptional auth metrics callbacks. The recorder is also bound to the wrapped bearer when possible so cache and acquisition events share one callback sink.
providerOptional provider label for emitted auth metrics. When omitted, the label is inferred from the wrapped bearer.
Method __init__ Create a renewable file-backed bearer.
Method receiver Return a RenewableFileCacheReceiver bound to the cache.
Method set_metrics Attach auth metrics callbacks and propagate them to the source.
Instance Variable metrics Undocumented
Instance Variable safety_margin A datetime.timedelta or seconds value used to determine when a token is considered too close to expiry. If None, the cache will be used until tokens are expired.
Property metrics_provider Return the metric provider label.
Property wrapped Return the wrapped bearer instance.
Method _is_token_fresh Undocumented
Instance Variable _bearer Undocumented
Instance Variable _cache Undocumented

Inherited from Bearer:

Class Method default_metrics_provider Return this bearer's default fully qualified metric provider label.
Async Method close Close the bearer and any wrapped resources.
Property name Optional human-readable name for the bearer.
def __init__(self, bearer: ParentBearer, safety_margin: timedelta | float = timedelta(hours=2), cache_file: str | Path = Path(DEFAULT_CONFIG_DIR) / DEFAULT_CREDENTIALS_FILE, throttle: timedelta | float = timedelta(minutes=5), metrics: AuthMetricsLike = None, provider: str | None = None): (source)

Create a renewable file-backed bearer.

The constructor creates the underlying ThrottledTokenCache.

Raises
ValueErrorWhen the wrapped bearer has no name.
def receiver(self) -> ParentReceiver: (source)

Return a RenewableFileCacheReceiver bound to the cache.

Returns
ParentReceiverA receiver that uses the file cache and refreshes from the wrapped bearer when necessary.
def set_metrics(self, metrics: AuthMetricsLike): (source)

Attach auth metrics callbacks and propagate them to the source.

safety_margin: timedelta | None = (source)

A datetime.timedelta or seconds value used to determine when a token is considered too close to expiry. If None, the cache will be used until tokens are expired.

@property
metrics_provider: str = (source)

Return the metric provider label.

Return the wrapped bearer instance.

Returns
The wrapped bearer passed at construction.
def _is_token_fresh(self, token: Token) -> bool: (source)

Undocumented

Undocumented

Undocumented