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/page.ex
defmodule Mercury.Page do
@moduledoc """
A single page of results from a `list/2` call, along with cursors for
fetching the next or previous page.
See `Mercury.Pagination.stream/1` for lazily walking every page.
"""
@type t(item) :: %__MODULE__{
items: [item],
next_page: String.t() | nil,
previous_page: String.t() | nil
}
@type t :: t(any())
defstruct items: [], next_page: nil, previous_page: nil
@doc false
@spec from_json(body :: map(), items_key :: String.t(), from_json_fun :: (map() -> item)) ::
t(item)
when item: struct()
def from_json(body, items_key, from_json_fun)
when is_map(body) and is_function(from_json_fun, 1) do
items =
body
|> Map.get(items_key, [])
|> List.wrap()
|> Enum.map(from_json_fun)
page_info = Map.get(body, "page", %{}) || %{}
%__MODULE__{
items: items,
next_page: Map.get(page_info, "nextPage"),
previous_page: Map.get(page_info, "previousPage")
}
end
end