HTTP client for JSONRPC 2.0 protocol.
If available in Hex, the package can be installed
by adding jsonrpc2_client to your list of dependencies in mix.exs:
def deps do
[
{:jsonrpc2_client, "~> 2.0.0"}
]
endThe library provides with default JSONRPC2.Client.Adapters.Default adapter. Nonetheless, it's possible to use custom adapter. It can be any module which implements JSONRPC2.Client.Adapters.Behaviour behaviour. For example, it can be adapter built by mox library, to make easy to test application what using this library.
config :jsonrpc2_client, :adapter, MyClient.MyAdapterdefmodule MyClient.MyAdapter do
@behaviour JSONRPC2.Client.Adapters.Behaviour
def execute(url, data, opts) do
# ...
end
endFirst, we collect operations and then send it in one single batch into the server.
alias JSONRPC2.Spec.Result
alias JSONRPC2.Spec.Error
[%Result{}, %Error{}] =
JSONRPC2.Client.call("add", [100, 120], 1)
|> JSONRPC2.Client.call("div", [120, 0], 2)
|> JSONRPC2.Client.notify("send_event", ["user.action", %{payload: "payload"}])
|> JSONRPC2.Client.send("http://127.0.0.1:4000")Mox.defmock(MyClient.MockAdapter, for: JSONRPC2.Client.Adapters.Behaviour)
Application.put_env(:jsonrpc2_client, :adapter, MyClient.MockAdapter)
expect(MyClient.MockAdapter, :execute, fn url, data, headers, opts ->
# assert args if required
# and define a return
{:ok, %JSONRPC2.Spec.Result{value: "value"}}
end)Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/jsonrpc2_client.