Longbridge.Config (longbridge v0.1.0)

Copy Markdown View Source

Configuration for Longbridge OpenAPI connections.

Examples

iex> config = Longbridge.Config.new(
...>   token: "your-oauth-token",
...>   app_key: "your-app-key",
...>   app_secret: "your-app-secret"
...> )
iex> config.token
"your-oauth-token"

Endpoints

By default, the international endpoints are used:

  • Quote: wss://openapi-quote.longbridge.com
  • Trade: wss://openapi-trade.longbridge.com

For mainland China, set china: true:

Longbridge.Config.new(token: "...", china: true)

This uses openapi-quote.longbridge.cn and openapi-trade.longbridge.cn.

Summary

Functions

Creates a new config struct.

Refreshes the access token using the Longbridge /v1/token/refresh HTTP API.

Fetches a one-time-password (OTP) for socket authentication.

Types

t()

@type t() :: %Longbridge.Config{
  app_key: String.t() | nil,
  app_secret: String.t() | nil,
  china: boolean(),
  expired_at: non_neg_integer() | nil,
  gzip_threshold: non_neg_integer() | nil,
  headers: [{String.t(), String.t()}] | nil,
  heartbeat_interval: non_neg_integer(),
  http_url: String.t(),
  idle_timeout: non_neg_integer(),
  quote_ws_url: String.t() | nil,
  request_timeout: non_neg_integer(),
  token: String.t() | nil,
  trade_ws_url: String.t() | nil
}

Functions

new(opts \\ [])

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

Creates a new config struct.

Options

  • :token — OAuth token for authentication
  • :app_key — Application key
  • :app_secret — Application secret
  • :china — Use mainland China endpoints (default: false)
  • :quote_ws_url — Override quote WebSocket URL
  • :trade_ws_url — Override trade WebSocket URL
  • :gzip_threshold — Min body size for gzip compression (bytes)
  • :heartbeat_interval — Heartbeat interval in ms (default: 15000)
  • :request_timeout — Request timeout in ms (default: 10000)
  • :idle_timeout — Close connection after this many ms of inactivity (default: 600000)
  • :headers — List of {name, value} tuples added to every HTTP and WebSocket request. Useful for injecting X-Forwarded-For, custom auth headers, or tenant routing headers. Mirrors Config::header(key, value) from longbridge/openapi Rust SDK (4.0.6).

refresh_access_token(config, opts \\ [])

@spec refresh_access_token(
  t(),
  keyword()
) :: {:ok, t()} | {:error, term()}

Refreshes the access token using the Longbridge /v1/token/refresh HTTP API.

For Legacy API Key authentication only. The current access_token expires after 90 days by default. Call this before it expires to obtain a new one, then update your stored LONGBRIDGE_ACCESS_TOKEN (or persist the new config) accordingly.

Returns {:ok, new_config} with the new token and expired_at, or {:error, reason}.

Options

  • :expired_at — When the new token should expire (Unix timestamp, seconds). Defaults to 90 days from now. Longbridge allows up to 3 years.

Example

{:ok, new_config} =
  config
  |> Longbridge.Config.refresh_access_token()

with_socket_token(config)

@spec with_socket_token(t()) :: {:ok, t()} | {:error, term()}

Fetches a one-time-password (OTP) for socket authentication.

The Longbridge socket protocol requires an OTP obtained via GET /v1/socket/token. Returns a new config with the OTP set as the token field, suitable for passing to QuoteContext or TradeContext.