retort v1.2.0 Retort.Response

A JSON-RPC Response object

Summary

Functions

Generates error response with the given message and corresponding code

Converts JSON decoded map with String.t keys to t

Decodes a JSON-encoded string back to a t

Types

t()
t() :: %Retort.Response{error: Retort.Response.Error.t | nil, id: integer | String.t | nil, jsonrpc: String.t, result: String.t | false | true | integer | float | [...] | %{} | nil}

Functions

error(message)
error(String.t) :: t

Generates error response with the given message and corresponding code

error(response, message)
error(response, changeset, render)
error(t, Ecto.Changeset.t, (Ecto.Changeset.t -> map)) :: t
from_json(map)
from_json(%{required(binary) => any}) :: t | no_return

Converts JSON decoded map with String.t keys to t.

from_string(string)
from_string(binary) :: t | no_return

Decodes a JSON-encoded string back to a t.

Decode a successful response

iex> Retort.Response.from_string(~S|{“id”: 1, “jsonrpc”: “2.0”, “result”: true}|) %Retort.Response{error: nil, id: 1, jsonrpc: “2.0”, result: true}

Decode an error response with data

iex> Retort.Response.from_string( …> ~S|{“error”: {“code”: -32602, “data”: [“foo”], “message”: “Invalid params”}, “id”: 2, “jsonrpc”: “2.0”}| …> ) %Retort.Response{

  error: %Retort.Response.Error{code: -32602, data: ["foo"], message: "Invalid params"},
  id: 2,
  jsonrpc: "2.0",
  result: nil

}

Decode an error response without data

iex> Retort.Response.from_string( …> ~S|{“error”: {“code”: -32600, “message”: “Invalid Request”}, “id”: 3, “jsonrpc”: “2.0”}| …> ) %Retort.Response{

error: %Retort.Response.Error{code: -32600, message: "Invalid Request"},
id: 3,
jsonrpc: "2.0",
result: nil

}