Monzo.Client (monzo_client v1.0.0)

Copy Markdown View Source

Immutable client configuration passed to every Monzo resource function.

Construct one with new/1. Mutable state (the current access/refresh token pair) lives in a supervised Monzo.TokenStore process referenced by token_store, not in this struct - so a %Monzo.Client{} is safe to share across processes and store in application config.

Options

  • :access_token - the user's current access token.
  • :refresh_token - enables automatic refresh-on-401, together with :client_id and :client_secret.
  • :client_id / :client_secret - required for token exchange, manual refresh, and automatic 401 recovery.
  • :on_refresh - a (access_token, refresh_token -> any) callback invoked whenever the token store successfully refreshes, so you can persist the rotated tokens (Monzo refresh tokens are single-use).
  • :base_url - defaults to "https://api.monzo.com". Override for testing against a mock server.
  • :adapter - a module implementing Monzo.HTTP.Adapter. Defaults to Monzo.HTTP.HttpcAdapter.
  • :timeout_ms - per-request timeout, defaults to 15000.
  • :retry - a map with :max_retries, :base_delay_ms, :max_delay_ms overriding the default retry policy.
  • :logger - a (level, message -> any) function for debug/warn events. Defaults to a no-op.
  • :user_agent - defaults to "monzo-elixir/<version>".
  • :token_store - an already-running Monzo.TokenStore pid or registered name to reuse, instead of starting a new one. Useful when multiple %Monzo.Client{} structs should share one token store.

Example

client = Monzo.Client.new(
  access_token: System.fetch_env!("MONZO_ACCESS_TOKEN"),
  refresh_token: System.fetch_env!("MONZO_REFRESH_TOKEN"),
  client_id: System.fetch_env!("MONZO_CLIENT_ID"),
  client_secret: System.fetch_env!("MONZO_CLIENT_SECRET"),
  on_refresh: fn access, refresh -> MyApp.Tokens.persist(access, refresh) end
)

{:ok, accounts} = Monzo.Accounts.list(client)

Summary

Functions

Returns the access token currently held by the client's token store, if any.

Builds a new client, starting a supervised token store.

Forces a refresh of the access token using the configured refresh token and client credentials. Most callers don't need this - the client refreshes automatically on 401s.

Manually sets new tokens on the client's token store.

Types

retry_policy()

@type retry_policy() :: %{
  max_retries: non_neg_integer(),
  base_delay_ms: pos_integer(),
  max_delay_ms: pos_integer()
}

t()

@type t() :: %Monzo.Client{
  adapter: module(),
  base_url: String.t(),
  logger: (atom(), String.t() -> any()),
  retry: retry_policy(),
  timeout_ms: pos_integer() | nil,
  token_store: GenServer.server() | nil,
  user_agent: String.t()
}

Functions

access_token(client)

@spec access_token(t()) :: String.t() | nil

Returns the access token currently held by the client's token store, if any.

new(opts \\ [])

@spec new(keyword()) :: t()

Builds a new client, starting a supervised token store.

refresh(client)

@spec refresh(t()) :: {:ok, String.t()} | {:error, term()}

Forces a refresh of the access token using the configured refresh token and client credentials. Most callers don't need this - the client refreshes automatically on 401s.

set_tokens(client, access_token, refresh_token \\ nil)

@spec set_tokens(t(), String.t(), String.t() | nil) :: :ok

Manually sets new tokens on the client's token store.