Packages

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
mercury_client lib mercury users.ex
Raw

lib/mercury/users.ex

defmodule Mercury.User do
@moduledoc "A Mercury platform user."
@type t :: %__MODULE__{
id: String.t(),
name: String.t(),
email: String.t(),
role: String.t(),
created_at: DateTime.t() | nil
}
defstruct [:id, :name, :email, :role, :created_at]
@doc false
def from_json(map) when is_map(map) do
%__MODULE__{
id: map["id"],
name: map["name"],
email: map["email"],
role: map["role"],
created_at: Mercury.Types.datetime(map["createdAt"])
}
end
end
defmodule Mercury.Users do
@moduledoc "The Mercury Users API — platform users on the organization."
alias Mercury.{Client, HTTP, Page, Pagination, Support, User}
@doc "Returns a single user by ID. `GET /users/{id}`"
@spec get(client :: Client.t(), user_id :: String.t()) ::
{:ok, User.t()} | {:error, Exception.t()}
def get(%Client{} = client, user_id) do
with {:ok, body} <- HTTP.get(client, "users/#{user_id}") do
{:ok, User.from_json(body)}
end
end
@spec get!(client :: Client.t(), user_id :: String.t()) :: User.t()
def get!(client, user_id), do: Support.bang!(get(client, user_id))
@doc "Returns one page of users. `GET /users`"
@spec list(client :: Client.t(), opts :: keyword()) ::
{:ok, Page.t(User.t())} | {:error, Exception.t()}
def list(%Client{} = client, opts \\ []) do
with {:ok, body} <- HTTP.get(client, "users", opts) do
{:ok, Page.from_json(body, "users", &User.from_json/1)}
end
end
@spec list!(client :: Client.t(), opts :: keyword()) :: Page.t(User.t())
def list!(client, opts \\ []), do: Support.bang!(list(client, opts))
@doc "A lazy, auto-paginating stream of every user."
@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