retort v2.7.0 Retort.Request View Source
A JSON-RPC Request object
Link to this section Summary
Functions
Decodes a JSON-encoded string back to an t
Constructs Interpreter.Rpc.Response.t
with same id
as the given response
Link to this section Types
Link to this section Functions
Decodes a JSON-encoded string back to an t
.
Decode method without parameters
iex> {:ok, request} = Retort.Request.from_string(
...> ~S|{"jsonrpc": "2.0", "id": 1, "method": "index"}|
...> )
iex> request
%Retort.Request{jsonrpc: "2.0", id: 1, method: "index"}
Decode method with positional arguments
iex> {:ok, request} = Retort.Request.from_string(
...> ~S|{"jsonrpc": "2.0", "id": 2, "method": "show", "params": [1]}|
...> )
iex> request
%Retort.Request{jsonrpc: "2.0", id: 2, method: "show", params: [1]}
Decode method with named arguments
iex> {:ok, request} = Retort.Request.from_string(
...> ~S|{"jsonrpc": "2.0", "id": 3, "method": "create", "params": {"name": "Alice"}}|
...> )
iex> request
%Retort.Request{jsonrpc: "2.0", id: 3, method: "create", params: %{"name" => "Alice"}}
Decode with missing fields is OK
iex> {:ok, request} = Retort.Request.from_string("{}")
iex> request
%Retort.Request{}
Proper handling of all return values
# See http://www.jsonrpc.org/specification#examples
response = case Retort.Request.from_string(encoded_request) do
{:ok, request = %Retort.Request{}} ->
response_for(request)
{:error, :invalid} ->
%Retort.Response{
error: %Retort.Response.Error{
code: -32600,
message: "Invalid Request"
}
}
{:error, {:invalid, token}} ->
%Retort.Response{
error: %Retort.Response.Error{
code: -32700,
message: "Parse error"
}
}
end
encoded_response = Poison.encode!(response)
Constructs Interpreter.Rpc.Response.t
with same id
as the given response.
iex> request = %Retort.Request{id: 1}
iex> response = Retort.Request.to_response(request)
iex> response.id == request.id
true