# `ExFattureincloud.OAuth.TokenManager`
[🔗](https://github.com/elchemista/ex_fattureincloud/blob/v0.1.0/lib/ex_fattureincloud/oauth/token_manager.ex#L25)

Optional supervised OAuth refresh coordinator for one OAuth connection.

Required options are `:client_id`, `:load` (a zero-arity callback returning
`{:ok, %OAuth.Token{}}`) and `:save` (a callback accepting the new token and
returning `:ok`). Set `:client_secret` for authorization-code applications;
omit it for device-code applications. `:client` configures OAuth HTTP requests.

`:refresh_margin` is in seconds (default 60, capped at half the token lifetime).
`:retry_interval` is in milliseconds (default 30,000). `:automatic` defaults to
true; disabling it keeps renewal on demand. `:name` is a GenServer name and `:id`
optionally distinguishes supervised children.

Refresh and persistence run in a task; concurrent callers share its result.
New credentials are published only after `:save` succeeds. If persistence fails,
the rotated credentials remain in memory and only saving is retried. Timeouts,
server errors and invalid responses pause renewal because their outcome may be
uncertain. `invalid_grant` requires authorization again. `reload/1` recovers by
reading the application's current stored credentials.

Use one owner per OAuth connection, even when it covers multiple companies.
Coordination is local to this process; deployments on multiple nodes must elect
one owner or provide distributed coordination. A process crash between remote
renewal and successful persistence cannot restore an unsaved rotated token.

# `server`

```elixir
@type server() :: GenServer.server()
```

The PID or registered name of a running token manager.

# `status`

```elixir
@type status() :: %{
  status:
    :ready
    | :persistence_failed
    | :rate_limited
    | :refresh_failed
    | :reauthorization_required,
  expires_at: integer(),
  pending_save?: boolean()
}
```

The current renewal state, cached token expiry, and pending-save flag.

# `token_result`

```elixir
@type token_result() :: {:ok, String.t()} | {:error, ExFattureincloud.Error.t()}
```

A current access token or a structured authentication error.

# `access_token`

```elixir
@spec access_token(server(), timeout()) :: token_result()
```

Gets a current access token, sharing any refresh already in progress.

# `child_spec`

```elixir
@spec child_spec(keyword()) :: Supervisor.child_spec()
```

Returns a child specification. Set a distinct `:id` or `:name` for each connection.

# `client`

```elixir
@spec client(server()) :: ExFattureincloud.Client.t()
```

Builds an API client using the manager's HTTP settings and current credentials.

# `reload`

```elixir
@spec reload(server()) :: :ok | {:error, ExFattureincloud.Error.t()}
```

Reloads persisted credentials when no refresh or save is pending.

# `start_link`

```elixir
@spec start_link(keyword()) :: GenServer.on_start()
```

Starts one refresh coordinator and loads its stored OAuth credentials.

# `status`

```elixir
@spec status(server()) :: status()
```

Returns the renewal state, expiry, and pending-save flag without credentials.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
