Ramp.Poller (ramp v1.0.0)

Copy Markdown View Source

Polls Ramp's deferred-task status endpoint until a task submitted by an asynchronous write (card issuance, user invite, limit creation, etc.) finishes, using capped exponential backoff between checks.

Most resource modules that trigger async work return a Ramp.DeferredTaskRef.t(); pass its id here:

{:ok, task_ref} = Ramp.Cards.create(client, params)
{:ok, card} = Ramp.Poller.poll(client, task_ref.id)

or, for full control:

{:ok, task_ref} = Ramp.Users.create(client, params)

case Ramp.Poller.poll(client, task_ref.id, timeout_ms: 15_000) do
  {:ok, user_json} -> user_json
  {:error, %Ramp.Error{type: :timeout_error}} -> :still_pending
  {:error, %Ramp.Error{type: :deferred_task_error} = e} -> raise e
end

Summary

Functions

Backs the poll: false-aware create/terminate-style functions in Ramp.Cards, Ramp.Users, and Ramp.Limits.

Blocks until the deferred task task_id reaches a terminal state, or :timeout_ms elapses.

Fetches the current status of a deferred task without waiting.

Types

poll_opt()

@type poll_opt() ::
  {:timeout_ms, pos_integer()}
  | {:interval_ms, pos_integer()}
  | {:max_interval_ms, pos_integer()}

Functions

await(client, ref, opts, on_success)

@spec await(Ramp.Client.t(), Ramp.DeferredTaskRef.t(), keyword(), (map() -> term())) ::
  term()

Backs the poll: false-aware create/terminate-style functions in Ramp.Cards, Ramp.Users, and Ramp.Limits.

Given the Ramp.DeferredTaskRef returned by the initial write, this either blocks and polls until the task completes -- passing the resulting JSON to on_success to build the final {:ok, _} (or :ok) return value -- or, if opts[:poll] is false, returns {:ok, ref} immediately without polling.

poll(client, task_id, opts \\ [])

@spec poll(Ramp.Client.t(), String.t(), [poll_opt()]) ::
  {:ok, map()} | {:error, Ramp.Error.t()}

Blocks until the deferred task task_id reaches a terminal state, or :timeout_ms elapses.

Options

  • :timeout_ms - overall deadline, default 60_000
  • :interval_ms - initial poll interval, default 500
  • :max_interval_ms - cap for the (exponentially growing, jittered) poll interval, default 5_000

Returns {:ok, data} with the task's resulting resource payload (raw JSON map -- pass it to the relevant resource's from_json/1 if you need a typed struct) on success, or {:error, %Ramp.Error{}} if the task failed or the timeout was reached.

status(client, task_id)

@spec status(Ramp.Client.t(), String.t()) :: {:ok, map()} | {:error, Ramp.Error.t()}

Fetches the current status of a deferred task without waiting.