Increase.Page (Increase v1.0.0)

Copy Markdown View Source

Represents a single page of results from a list endpoint, along with enough information to fetch subsequent pages.

Every list/2 function in a resource module (e.g. Increase.Accounts.list/2) returns {:ok, %Increase.Page{}}. Use auto_paging_each/2 or auto_paging_stream/1 to walk every page without manually managing the cursor.

Examples

{:ok, page} = Increase.Accounts.list(client, limit: 100)
page.data        #=> [%{"id" => "account_...", ...}, ...]
page.next_cursor #=> "v57w5d" | nil

# Lazily stream every Account across every page:
Increase.Page.auto_paging_stream(page)
|> Stream.each(&IO.inspect/1)
|> Stream.run()

Summary

Functions

Calls fun once for every item across this page and all subsequent pages. Stops early and returns {:error, error} if fetching a later page fails.

Returns a lazy Stream of every individual item across this page and all subsequent pages, fetching new pages on demand as the stream is consumed.

Returns true if there is another page of results after this one.

Fetches the next page of results. Returns {:error, :no_more_pages} if this is already the last page.

Types

t()

@type t() :: %Increase.Page{
  data: [term()],
  fetch_next: (String.t() -> {:ok, t()} | {:error, Increase.Error.t()}),
  next_cursor: String.t() | nil
}

Functions

auto_paging_each(page, fun)

@spec auto_paging_each(t(), (term() -> any())) :: :ok | {:error, Increase.Error.t()}

Calls fun once for every item across this page and all subsequent pages. Stops early and returns {:error, error} if fetching a later page fails.

auto_paging_stream(page)

@spec auto_paging_stream(t()) :: Enumerable.t()

Returns a lazy Stream of every individual item across this page and all subsequent pages, fetching new pages on demand as the stream is consumed.

Increase.Accounts.list(client)
|> case do
  {:ok, page} -> Increase.Page.auto_paging_stream(page)
  {:error, error} -> raise error
end
|> Enum.take(250)

has_next?(page)

@spec has_next?(t()) :: boolean()

Returns true if there is another page of results after this one.

next_page(page)

@spec next_page(t()) :: {:ok, t()} | {:error, :no_more_pages | Increase.Error.t()}

Fetches the next page of results. Returns {:error, :no_more_pages} if this is already the last page.