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

Copy Markdown View Source

JSON batch requests for Microsoft Graph API.

Allows sending up to 20 requests in a single HTTP call via POST /$batch. Build batch requests using _query functions from resource modules.

Examples

alias GraphApi.{Batch, OData, Users, Groups}
alias GraphApi.Schema.{User, Group}

query = OData.new() |> OData.select(["id", "displayName"]) |> OData.top(5)

{:ok, responses} =
  Batch.new()
  |> Batch.add("1", Users.list_query(query: query, as: User))
  |> Batch.add("2", Groups.get_query("group-id", as: Group))
  |> Batch.add("3", Users.create_query(%{"displayName" => "New"}, as: User))
  |> Batch.execute(client: client)

%{status: 200, body: %{"value" => users}} = Batch.get(responses, "1")

Summary

Functions

Adds a request to the batch with the given ID.

Executes the batch, sending all requests in a single POST /$batch call.

Finds a response by its request ID.

Creates a new empty batch.

Types

t()

@type t() :: %GraphApi.Batch{entries: [GraphApi.Batch.Entry.t()]}

Functions

add(batch, id, request, add_opts \\ [])

@spec add(t(), String.t(), GraphApi.Batch.Request.t(), keyword()) :: t()

Adds a request to the batch with the given ID.

Options

  • :depends_on - List of request IDs that must complete before this one.

Examples

Batch.new()
|> Batch.add("1", Users.list_query(as: User))
|> Batch.add("2", Users.create_query(attrs, as: User), depends_on: ["1"])

execute(batch, opts \\ [])

@spec execute(
  t(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}

Executes the batch, sending all requests in a single POST /$batch call.

Returns {:ok, [response]} where each response has :id, :status, :headers, and :body. Response bodies are automatically cast using the :as schema specified on each request.

Options

Same as other resource functions: :client, :access_token, :api_version

get(responses, id)

@spec get([map()], String.t()) :: map() | nil

Finds a response by its request ID.

Examples

{:ok, responses} = Batch.execute(batch, client: client)
%{status: 200, body: user} = Batch.get(responses, "1")

new()

@spec new() :: t()

Creates a new empty batch.