Creatio.Batch (creatio v0.3.1)

Copy Markdown

Constructs and executes OData 4 batch requests (/0/odata/$batch).

Supports both JSON batch format (standard in OData 4) and multipart/mixed MIME format, atomicity groups (changesets), and Prefer: continue-on-error.

Examples

JSON Batch (Default)

batch =
  Creatio.Batch.new(continue_on_error: true)
  |> Creatio.Batch.add_create("Contact", %{"Name" => "Alice"}, atomicity_group: "g1")
  |> Creatio.Batch.add_update("Contact", "c31c7862-fe...", %{"JobTitle" => "VP"}, atomicity_group: "g1")
  |> Creatio.Batch.add_delete("Contact", "b6a05348-55...")

{:ok, responses} = Creatio.Batch.execute(client, batch)

Multipart/Mixed MIME Batch

{:ok, responses} = Creatio.Batch.execute(client, batch, format: :multipart)

Summary

Functions

Adds a generic request to the batch.

Adds an entity creation operation (POST /0/odata/{Entity}) to the batch.

Adds an entity deletion operation (DELETE /0/odata/{Entity}({id})) to the batch.

Adds an entity retrieval operation (GET /0/odata/...) to the batch.

Adds a single entity retrieval operation with options to the batch.

Adds an entity update operation (PATCH /0/odata/{Entity}({id})) to the batch.

Executes the batch request against the Creatio /0/odata/$batch endpoint.

Initializes a new batch request.

Parses a multipart/mixed MIME response body into a list of %Creatio.Batch.Response{} structs.

Serializes the batch to a multipart/mixed MIME body string. Returns {boundary, body}.

Converts the batch to the JSON payload map expected by /0/odata/$batch.

Types

t()

@type t() :: %Creatio.Batch{continue_on_error: boolean(), requests: [map()]}

Functions

add(batch, method, url, opts \\ [])

@spec add(t(), String.t(), String.t(), keyword()) :: t()

Adds a generic request to the batch.

add_create(batch, entity, attrs, opts \\ [])

@spec add_create(t(), String.t(), map(), keyword()) :: t()

Adds an entity creation operation (POST /0/odata/{Entity}) to the batch.

add_delete(batch, entity, id, opts \\ [])

@spec add_delete(t(), String.t(), String.t(), keyword()) :: t()

Adds an entity deletion operation (DELETE /0/odata/{Entity}({id})) to the batch.

add_get(batch, entity_or_path, opts_or_id \\ [])

@spec add_get(t(), String.t(), keyword() | String.t()) :: t()

Adds an entity retrieval operation (GET /0/odata/...) to the batch.

Can be used to retrieve:

  • An entity collection or path: add_get(batch, "Contact") or add_get(batch, "Contact", headers: ...)
  • A single entity by ID: add_get(batch, "Contact", "some-uuid") or add_get(batch, "Contact", "some-uuid", headers: ...)

add_get(batch, entity, id, opts)

@spec add_get(t(), String.t(), String.t(), keyword()) :: t()

Adds a single entity retrieval operation with options to the batch.

add_update(batch, entity, id, attrs, opts \\ [])

@spec add_update(t(), String.t(), String.t(), map(), keyword()) :: t()

Adds an entity update operation (PATCH /0/odata/{Entity}({id})) to the batch.

execute(client_or_req, batch, opts \\ [])

@spec execute(
  Creatio.Client.t() | Req.Request.t(),
  t() | map() | list() | binary(),
  keyword()
) ::
  {:ok, [Creatio.Batch.Response.t()]} | {:error, Creatio.Error.t()}

Executes the batch request against the Creatio /0/odata/$batch endpoint.

Options

  • :format - :json (default) or :multipart.
  • :continue_on_error - boolean. If true, sets Prefer: continue-on-error.
  • :url - Overrides endpoint URL (defaults to "/0/odata/$batch").

When the client is in read-only mode (client.read_only == true), batches containing mutation requests (POST, PATCH, PUT, DELETE) or binary batch payloads are rejected with {:error, %Creatio.Error{status: 403, code: "ReadOnlyMode"}}.

new(opts \\ [])

@spec new(keyword()) :: t()

Initializes a new batch request.

Options

  • :continue_on_error - boolean. If true, adds header Prefer: continue-on-error.

parse_multipart_response(body, boundary \\ nil)

@spec parse_multipart_response(String.t(), String.t() | nil) ::
  {:ok, [Creatio.Batch.Response.t()]} | {:error, Creatio.Error.t()}

Parses a multipart/mixed MIME response body into a list of %Creatio.Batch.Response{} structs.

to_multipart(batch, opts \\ [])

@spec to_multipart(t(), keyword()) :: {String.t(), String.t()}

Serializes the batch to a multipart/mixed MIME body string. Returns {boundary, body}.

to_payload(batch)

@spec to_payload(t()) :: map()

Converts the batch to the JSON payload map expected by /0/odata/$batch.