JsonRPC.Request (JsonRPC v0.1.0) View Source

Representation of a JsonRPC request. The JsonRPC version is always "2.0".

Examples

iex> JsonRPC.Request.cast(id: "x-1", method: "say", params: ["hello"])
{:ok, %JsonRPC.Request{id: "x-1", jsonrpc: "2.0", method: "say", params: ["hello"]}}

iex> JsonRPC.Request.cast(method: "beat")
{:ok, %JsonRPC.Request{id: :notification, jsonrpc: "2.0", method: "beat", params: nil}}

Link to this section Summary

Functions

Converts the given data according to the defined schema.

Converts the given data according to the defined schema.

Converts a request to map.

Returns true if the given data valid against the defined schema, otherwise false.

Validates the given data against the defined schema.

Validates the given data against the defined schema.

Link to this section Functions

Specs

cast(term()) ::
  {:ok, term()} | {:error, Xema.ValidationError.t() | Xema.CastError.t()}

Converts the given data according to the defined schema.

Returns an :ok tuple with the converted data for valid data, otherwise an :error tuple is returned.

Specs

cast!(term()) :: term()

Converts the given data according to the defined schema.

Returns converted data for valid data, otherwise a Xeam.CastError or Xema.ValidationError is raised.

Specs

to_map(%JsonRPC.Request{
  id: term(),
  jsonrpc: term(),
  method: term(),
  params: term()
}) :: map()

Converts a request to map.

The id is omitted when the value is :notification. params is ommitted when it is nil.

Examples

iex> JsonRPC.Request.to_map(
...>   %JsonRPC.Request{id: "x-1", jsonrpc: "2.0", method: "say", params: ["hello"]})
%{id: "x-1", jsonrpc: "2.0", method: "say", params: ["hello"]}

iex> JsonRPC.Request.to_map(
...>   %JsonRPC.Request{id: :notification, jsonrpc: "2.0", method: "beat", params: nil})
%{jsonrpc: "2.0", method: "beat"}

Specs

valid?(term()) :: boolean()

Returns true if the given data valid against the defined schema, otherwise false.

Specs

validate(term()) :: :ok | {:error, Xema.ValidationError.t()}

Validates the given data against the defined schema.

Returns :ok for valid data, otherwise an :error tuple.

Specs

validate!(term()) :: :ok

Validates the given data against the defined schema.

Returns :ok for valid data, otherwise a Xema.ValidationError is raised.