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
metawithpageandtotal_pages(orhas_next). The stream requestspage + 1until the last page. - Cursor based - the body has
paging.cursors.after. The stream passes it asafteruntil 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
@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.