Packages
mercury_client
1.0.0
A complete, production-grade Elixir client for the Mercury Banking API (accounts, transactions, recipients, invoices, payments, treasury, webhooks, and more), with typed errors, retry with backoff, and lazy auto-paginating streams.
Current section
Files
Jump to
Current section
Files
lib/mercury/events.ex
defmodule Mercury.Event do
@moduledoc "A Mercury audit-log event."
@type t :: %__MODULE__{
id: String.t(),
type: String.t(),
created_at: DateTime.t() | nil,
before_state: map() | nil,
after_state: map() | nil,
resource_id: String.t() | nil,
resource_type: String.t() | nil
}
defstruct [:id, :type, :created_at, :before_state, :after_state, :resource_id, :resource_type]
@doc false
def from_json(map) when is_map(map) do
%__MODULE__{
id: map["id"],
type: map["type"],
created_at: Mercury.Types.datetime(map["createdAt"]),
before_state: map["before"],
after_state: map["after"],
resource_id: map["resourceId"],
resource_type: map["resourceType"]
}
end
end
defmodule Mercury.Events do
@moduledoc "The Mercury Events API — an audit log of account and org changes."
alias Mercury.{Client, Event, HTTP, Page, Pagination, Support}
@doc "Returns a single event by ID. `GET /events/{id}`"
@spec get(client :: Client.t(), event_id :: String.t()) ::
{:ok, Event.t()} | {:error, Exception.t()}
def get(%Client{} = client, event_id) do
with {:ok, body} <- HTTP.get(client, "events/#{event_id}") do
{:ok, Event.from_json(body)}
end
end
@spec get!(client :: Client.t(), event_id :: String.t()) :: Event.t()
def get!(client, event_id), do: Support.bang!(get(client, event_id))
@doc "Returns one page of events. `GET /events`"
@spec list(client :: Client.t(), opts :: keyword()) ::
{:ok, Page.t(Event.t())} | {:error, Exception.t()}
def list(%Client{} = client, opts \\ []) do
with {:ok, body} <- HTTP.get(client, "events", opts) do
{:ok, Page.from_json(body, "events", &Event.from_json/1)}
end
end
@spec list!(client :: Client.t(), opts :: keyword()) :: Page.t(Event.t())
def list!(client, opts \\ []), do: Support.bang!(list(client, opts))
@doc "A lazy, auto-paginating stream of every event."
@spec stream(client :: Client.t(), opts :: keyword()) :: Enumerable.t()
def stream(%Client{} = client, opts \\ []) do
opts = Keyword.new(opts)
Pagination.stream(fn cursor -> list(client, Keyword.put(opts, :start_after, cursor)) end)
end
end