KapsoEx.Pagination (kapso_ex v0.1.0)

Copy Markdown View Source

Lazily walks every page of a list endpoint.

Kapso list endpoints paginate in one of two ways, and stream/2 handles both by looking at the response:

  • Page based - the body has meta with page and total_pages (or has_next). The stream requests page + 1 until the last page.
  • Cursor based - the body has paging.cursors.after. The stream passes it as after until there is no next page.

Example

KapsoEx.Pagination.stream(&KapsoEx.Platform.Customers.list(client, &1), per_page: 100)
|> Stream.filter(&(&1["external_customer_id"] != nil))
|> Enum.to_list()

KapsoEx.Pagination.stream(
  &KapsoEx.WhatsApp.Messages.list(client, "647015955153740", &1),
  limit: 100, direction: "inbound"
)
|> Enum.take(500)

Requests are made only as items are consumed. When a page request fails the stream raises the KapsoEx.Error.

Summary

Functions

Returns a stream of the items in data across all pages.

Functions

stream(fun, params \\ [])

@spec stream((map() -> {:ok, map()} | {:error, KapsoEx.Error.t()}), map() | keyword()) ::
  Enumerable.t()

Returns a stream of the items in data across all pages.

fun receives the params for each page (a map with atom keys, starting from params) and must return the result of a list function.