JSONRPC2.Spec.Batch (jsonrpc2_spec v0.2.0)

Copy Markdown

JSONRPC2.Spec.Batch allows you to pack operations (function calls and notification sending) that can later be executed into a single request.

Examples:

iex> alias JSONRPC2.Spec.Batch
iex> Batch.notify("event_name", ["arg1", "arg2"]) |> Batch.call("func_name", ["arg1", "arg2"], 1)
%JSONRPC2.Spec.Batch{
  calls: %{
    1 => %JSONRPC2.Spec.Request{
      id: 1,
      method: "func_name",
      params: ["arg1", "arg2"],
      jsonrpc: "2.0"
    }
  },
  notifies: [
    %JSONRPC2.Spec.Request{
      id: nil,
      method: "event_name",
      params: ["arg1", "arg2"],
      jsonrpc: "2.0"
    }
  ]
}

Summary

Types

t()

@type t() :: %JSONRPC2.Spec.Batch{calls: map(), notifies: [map()]}

Functions

call(method, params, id)

Prepare data to call function.

The params could be either map or list.

The id must be unique within Batch.

Examples:

iex> alias JSONRPC2.Spec.Batch
iex> Batch.call("name", ["arg1", "arg2"], 1)
%JSONRPC2.Spec.Batch{
  calls: %{
    1 => %JSONRPC2.Spec.Request{
      id: 1,
      method: "name",
      params: ["arg1", "arg2"],
      jsonrpc: "2.0"
    }
  },
  notifies: []
}

Functions can be piped:

iex> Batch.notify("event_name", ["arg1", "arg2"]) |> Batch.call("func_name", ["arg1", "arg2"], 1)
%JSONRPC2.Spec.Batch{
  calls: %{
    1 => %JSONRPC2.Spec.Request{
      id: 1,
      method: "func_name",
      params: ["arg1", "arg2"],
      jsonrpc: "2.0"
    }
  },
  notifies: [
    %JSONRPC2.Spec.Request{
      id: nil,
      method: "event_name",
      params: ["arg1", "arg2"],
      jsonrpc: "2.0"
    }
  ]
}

call(req, method, params, id)

notify(method, params)

@spec notify(JSONRPC2.Spec.method(), JSONRPC2.Spec.params()) :: t()

Prepare data to send notification.

The params could be either map or list.

Examples:

iex> alias JSONRPC2.Spec.Batch
iex> Batch.notify("name", ["arg1", "arg2"])
%JSONRPC2.Spec.Batch{
  calls: %{},
  notifies: [
    %JSONRPC2.Spec.Request{
      id: nil,
      method: "name",
      params: ["arg1", "arg2"],
      jsonrpc: "2.0"
    }
  ]
}

Functions can be piped:

iex> Batch.notify("event_name", ["arg1", "arg2"]) |> Batch.call("func_name", ["arg1", "arg2"], 1)
%JSONRPC2.Spec.Batch{
  calls: %{
    1 => %JSONRPC2.Spec.Request{
      id: 1,
      method: "func_name",
      params: ["arg1", "arg2"],
      jsonrpc: "2.0"
    }
  },
  notifies: [
    %JSONRPC2.Spec.Request{
      id: nil,
      method: "event_name",
      params: ["arg1", "arg2"],
      jsonrpc: "2.0"
    }
  ]
}

notify(req, method, params)

@spec notify(t(), JSONRPC2.Spec.method(), JSONRPC2.Spec.params()) :: t()