Increase.Client (Increase v1.0.0)

Copy Markdown View Source

The low-level HTTP client used by every resource module to talk to the Increase API. You normally don't need to use this module directly -- resource modules like Increase.Accounts build the right client for you from configuration -- but it's available if you want full control over how requests are made.

Configuration

A %Increase.Client{} struct holds everything needed to make a request: the API key, the base URL (sandbox or production), and any default Req options you want applied to every request (timeouts, retry policy, custom middleware, etc).

By default, configuration is read from your application config:

config :increase,
  api_key: System.fetch_env!("INCREASE_API_KEY"),
  environment: :sandbox # or :production

You can also build a client explicitly and pass it to any resource function as the last argument:

client = Increase.Client.new(api_key: "sandbox_...", environment: :sandbox)
{:ok, account} = Increase.Accounts.retrieve(client, "account_123")

Retries

Requests are retried automatically on rate limiting (HTTP 429), server errors (500/502/503/504), and transient transport failures (timeouts, connection refused), with exponential backoff honoring the API's Retry-After header when present. This applies to every HTTP method, including POST/PATCH -- safe to do because, whenever retries are enabled, this client automatically attaches an idempotency key to every mutating request that doesn't already have one, generated once before the first attempt and reused for every retry of that same request. Increase guarantees retrying a request with the same Idempotency-Key returns the original result rather than creating a duplicate object, so this can never double-create anything.

Retry behavior is just Req's own :retry option (see Req.Steps.retry/1 for full details) and can be reconfigured or disabled per-client:

# Disable retries entirely:
Increase.Client.new(retry: false)

# Cap at 1 retry instead of the default:
Increase.Client.new(max_retries: 1)

Telemetry

Every request emits :telemetry events under the [:increase, :request] prefix:

  • [:increase, :request, :start] -- before a request is sent. Measurements: :system_time. Metadata: :method, :path.
  • [:increase, :request, :stop] -- after a response (success or API error) is received. Measurements: :duration (native time units). Metadata: :method, :path, and either :status (on success or an API-level error) or :error (on a transport-level failure).
  • [:increase, :request, :exception] -- if the request raises instead of returning. Metadata includes :kind, :reason, and :stacktrace.

Attach a handler with :telemetry.attach/4 or :telemetry.attach_many/4 to log, trace, or collect metrics on outgoing requests:

:telemetry.attach(
  "log-increase-requests",
  [:increase, :request, :stop],
  fn _event, measurements, metadata, _config ->
    duration_ms = System.convert_time_unit(measurements.duration, :native, :millisecond)
    Logger.info("Increase #{metadata.method} #{metadata.path} -> #{metadata[:status]} in #{duration_ms}ms")
  end,
  nil
)

Summary

Functions

Builds a new client.

Resolves a client argument that may already be a %Increase.Client{}, a keyword list of options, or nil (in which case the default client built from application config is used). Every resource function accepts any of these forms for its trailing client argument.

Types

t()

@type t() :: %Increase.Client{
  api_key: String.t() | nil,
  base_url: String.t(),
  req_options: keyword()
}

Functions

new(opts \\ [])

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

Builds a new client.

Options

  • :api_key - your Increase API key. Defaults to the :api_key value configured under config :increase, ..., or the INCREASE_API_KEY environment variable.
  • :environment - either :sandbox or :production. Defaults to the configured :environment, or :sandbox if nothing is configured.
  • :base_url - override the base URL entirely (useful for testing against a local mock server). Takes precedence over :environment.
  • any other option is passed straight through to Req.new/1 for every request made with this client (e.g. :receive_timeout, :retry, :plug). See the "Retries" section above for retry-specific options.

resolve(client)

@spec resolve(t() | keyword() | nil) :: t()

Resolves a client argument that may already be a %Increase.Client{}, a keyword list of options, or nil (in which case the default client built from application config is used). Every resource function accepts any of these forms for its trailing client argument.