# `KapsoEx.Pagination`
[🔗](https://github.com/rocket4ce/kapso_ex/blob/v0.1.0/lib/kapso_ex/pagination.ex#L1)

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`.

# `stream`

```elixir
@spec stream((map() -&gt; {: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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
