CampaignFlow.Client.TokenManager (CampaignFlow Client v2.0.0)

View Source

GenServer that manages OAuth2 access tokens for the Campaign Flow API.

This module ensures that:

  • Tokens are shared across multiple processes
  • Only one token refresh happens at a time (GenServer serialization)
  • Concurrent requests are automatically queued by GenServer's message queue

Usage

TokenManagers are typically started automatically by the Application supervisor based on configuration, but can also be started manually:

{:ok, pid} = CampaignFlow.Client.TokenManager.start_link(
  name: CampaignFlow.TokenManager.Prod,
  environment: :prod,
  base_url: "https://app.campaignflow.com.au/api/v2",
  client_id: "your_client_id",
  client_secret: "your_client_secret"
)

To get a token:

{:ok, token} = CampaignFlow.Client.TokenManager.get_token(CampaignFlow.TokenManager.Prod)

Summary

Functions

Returns a specification to start this module under a supervisor.

Gets a valid access token, refreshing if necessary.

Starts a TokenManager process.

Types

state()

@type state() :: %{
  environment: atom(),
  base_url: String.t(),
  client_id: String.t(),
  client_secret: String.t(),
  access_token: String.t() | nil,
  token_expires_at: DateTime.t() | nil
}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_token(name_or_pid)

@spec get_token(atom() | pid()) :: {:ok, String.t()} | {:error, term()}

Gets a valid access token, refreshing if necessary.

Concurrent calls will be queued by GenServer and processed sequentially. The first call will refresh the token if needed, and subsequent calls will receive the cached token.

Examples

{:ok, token} = TokenManager.get_token(CampaignFlow.TokenManager.Prod)
{:error, reason} = TokenManager.get_token(CampaignFlow.TokenManager.Test)

start_link(opts)

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

Starts a TokenManager process.

Options

  • :name - The registered name for this process (required)
  • :environment - The environment name (e.g., :prod, :test)
  • :base_url - The API base URL (required)
  • :client_id - OAuth2 client ID (required)
  • :client_secret - OAuth2 client secret (required)