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/customers.ex
defmodule Mercury.Customer do
@moduledoc "A Mercury customer (accounts-receivable contact)."
@type t :: %__MODULE__{
id: String.t(),
name: String.t(),
email: String.t() | nil,
created_at: DateTime.t() | nil,
updated_at: DateTime.t() | nil
}
defstruct [:id, :name, :email, :created_at, :updated_at]
@doc false
def from_json(map) when is_map(map) do
%__MODULE__{
id: map["id"],
name: map["name"],
email: map["email"],
created_at: Mercury.Types.datetime(map["createdAt"]),
updated_at: Mercury.Types.datetime(map["updatedAt"])
}
end
end
defmodule Mercury.Customers do
@moduledoc "The Mercury Customers API — accounts-receivable customers."
alias Mercury.{Client, Customer, HTTP, Page, Pagination, Support}
@doc "Creates a new customer. `POST /customers`"
@spec create(client :: Client.t(), attrs :: map() | keyword()) ::
{:ok, Customer.t()} | {:error, Exception.t()}
def create(%Client{} = client, attrs) do
with {:ok, body} <- HTTP.post(client, "customers", attrs) do
{:ok, Customer.from_json(body)}
end
end
@spec create!(client :: Client.t(), attrs :: map() | keyword()) :: Customer.t()
def create!(client, attrs), do: Support.bang!(create(client, attrs))
@doc "Returns a single customer by ID. `GET /customers/{id}`"
@spec get(client :: Client.t(), customer_id :: String.t()) ::
{:ok, Customer.t()} | {:error, Exception.t()}
def get(%Client{} = client, customer_id) do
with {:ok, body} <- HTTP.get(client, "customers/#{customer_id}") do
{:ok, Customer.from_json(body)}
end
end
@spec get!(client :: Client.t(), customer_id :: String.t()) :: Customer.t()
def get!(client, customer_id), do: Support.bang!(get(client, customer_id))
@doc "Returns one page of customers. `GET /customers`"
@spec list(client :: Client.t(), opts :: keyword()) ::
{:ok, Page.t(Customer.t())} | {:error, Exception.t()}
def list(%Client{} = client, opts \\ []) do
with {:ok, body} <- HTTP.get(client, "customers", opts) do
{:ok, Page.from_json(body, "customers", &Customer.from_json/1)}
end
end
@spec list!(client :: Client.t(), opts :: keyword()) :: Page.t(Customer.t())
def list!(client, opts \\ []), do: Support.bang!(list(client, opts))
@doc "A lazy, auto-paginating stream of every customer."
@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
@doc "Updates a customer. `PUT /customers/{id}`"
@spec update(client :: Client.t(), customer_id :: String.t(), attrs :: map() | keyword()) ::
{:ok, Customer.t()} | {:error, Exception.t()}
def update(%Client{} = client, customer_id, attrs) do
with {:ok, body} <- HTTP.put(client, "customers/#{customer_id}", attrs) do
{:ok, Customer.from_json(body)}
end
end
@spec update!(client :: Client.t(), customer_id :: String.t(), attrs :: map() | keyword()) ::
Customer.t()
def update!(client, customer_id, attrs), do: Support.bang!(update(client, customer_id, attrs))
@doc "Deletes a customer. This action is irreversible. `DELETE /customers/{id}`"
@spec delete(client :: Client.t(), customer_id :: String.t()) :: :ok | {:error, Exception.t()}
def delete(%Client{} = client, customer_id) do
case HTTP.delete(client, "customers/#{customer_id}") do
{:ok, _body} -> :ok
{:error, _error} = error -> error
end
end
@spec delete!(client :: Client.t(), customer_id :: String.t()) :: :ok
def delete!(client, customer_id), do: Support.bang!(delete(client, customer_id))
end