## client.MaskinportenClient


Requests Maskinporten access tokens and reuses each one until shortly before it


Usage

``` python
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()](config.load_config.md#pymaskinporten.config.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_env` endpoint.

`audience: str | None = None`  
The JWT audience. Defaults to the `api_env` issuer.


## Raises


`ValueError`  
If `api_env` is not "test" or "prod", or `refresh_margin` is negative.


## Examples

client = MaskinportenClient(api_env="prod") headers = {"Authorization": f"Bearer {client.get_token()}"}


## Methods

| Name | Description |
|----|----|
| [clear_cache()](#clear_cache) | Drops the cached token, so the next [get_token()](client.MaskinportenClient.md#pymaskinporten.client.MaskinportenClient.get_token) fetches a new one. |
| [get_token()](#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()](client.MaskinportenClient.md#pymaskinporten.client.MaskinportenClient.get_token) fetches a new one.


Usage

``` python
clear_cache()
```


------------------------------------------------------------------------


#### get_token()


Returns a valid access token, fetching a new one if the cached one is missing


Usage

``` python
get_token()
```


or about to expire.


##### Raises


`MaskinportenTokenError`  
If a new token is needed and the request fails.
