GraphApi.Pagination (GraphApi v1.0.0-rc.1)

Copy Markdown View Source

Stream-based pagination for Microsoft Graph API responses.

Follows @odata.nextLink to lazily fetch subsequent pages.

Examples

{:ok, first_page} = GraphApi.Users.list(client: client)
all_users = GraphApi.Pagination.stream(first_page, client: client)
  |> Enum.to_list()

Summary

Functions

Collects all items from all pages into a single list.

Extracts page data from a Graph API response.

Creates a lazy Stream that follows @odata.nextLink to yield all items.

Functions

collect_all(first_page, opts \\ [])

@spec collect_all(
  map(),
  keyword()
) :: {:ok, list()} | {:error, term()}

Collects all items from all pages into a single list.

A convenience wrapper around stream/2 that eagerly fetches all pages.

extract_page(body, opts \\ [])

@spec extract_page(
  map(),
  keyword()
) :: {list(), String.t() | nil}

Extracts page data from a Graph API response.

Returns {items, next_link} where items is the list of items from "value" and next_link is the URL for the next page (or nil).

Optionally accepts an as: module to cast each item.

stream(first_page, opts \\ [])

@spec stream(
  map(),
  keyword()
) :: Enumerable.t()

Creates a lazy Stream that follows @odata.nextLink to yield all items.

Takes the first page response body and options (must include :client). Optionally accepts :as to cast each item into a schema/view struct.

Examples

{:ok, page} = GraphApi.Users.list(client: client)
stream = GraphApi.Pagination.stream(page, client: client)
all = Enum.to_list(stream)

# With schema casting
stream = GraphApi.Pagination.stream(page, client: client, as: GraphApi.Schema.User)