client.MaskinportenClient
Requests Maskinporten access tokens and reuses each one until shortly before it
Usage
client.MaskinportenClient(
config=None,
api_env="test",
*,
refresh_margin=10,
token_url=None,
audience=None
)expires.
The client is thread-safe: when the cached token has expired, one thread fetches a new token and the others wait for it instead of sending their own requests.
Parameters
config: MaskinportenSecrets | None = None-
The credentials to use. If None, they are read once with load_config() when the client is created.
api_env: ApiEnv = "test"-
The Maskinporten environment, “test” or “prod”.
refresh_margin: float = 10-
Seconds before the token expires at which to fetch a new one.
token_url: str | None = None-
The token endpoint to call. Defaults to the
api_envendpoint. audience: str | None = None-
The JWT audience. Defaults to the
api_envissuer.
Raises
ValueError-
If
api_envis not “test” or “prod”, orrefresh_marginis negative.
Examples
client = MaskinportenClient(api_env=“prod”) headers = {“Authorization”: f”Bearer {client.get_token()}“}
Methods
| Name | Description |
|---|---|
| clear_cache() | Drops the cached token, so the next get_token() fetches a new one. |
| get_token() | Returns a valid access token, fetching a new one if the cached one is missing |
clear_cache()
Drops the cached token, so the next get_token() fetches a new one.
Usage
clear_cache()get_token()
Returns a valid access token, fetching a new one if the cached one is missing
Usage
get_token()or about to expire.
Raises
MaskinportenTokenError- If a new token is needed and the request fails.