JSON-RPC 2.0 request builder and response matcher.
Simple API builder for WebSocket APIs using JSON-RPC 2.0 protocol. Generates request functions with automatic ID tracking and correlation.
API Functions
| Function | Arity | Description | Param Kinds |
|---|---|---|---|
match_response | 1 | Match a JSON-RPC response as result, error, or notification. | response: value |
build_request | 2 | Build a JSON-RPC 2.0 request with unique ID. | method: value, params: value |
Summary
Functions
Builds a JSON-RPC 2.0 request with unique ID.
Generates RPC method functions with automatic request building.
Matches a JSON-RPC response to determine if it's a result or error.
Functions
Imports the defrpc/2 and defrpc/3 macros for defining JSON-RPC methods.
Builds a JSON-RPC 2.0 request with unique ID.
Examples
iex> {:ok, request} = JsonRpc.build_request("public/auth", %{grant_type: "client_credentials"})
iex> request["method"]
"public/auth"
Generates RPC method functions with automatic request building.
Examples
defmodule MyApi do
use ZenWebsocket.JsonRpc
defrpc :authenticate, "public/auth"
defrpc :subscribe, "public/subscribe"
defrpc :get_order_book, "public/get_order_book"
end
@spec match_response(map()) :: {:ok, term()} | {:error, {integer(), String.t()}} | {:notification, String.t(), map()}
Matches a JSON-RPC response to determine if it's a result or error.
Returns:
{:ok, result}for successful responses{:error, {code, message}}for JSON-RPC errors{:notification, method, params}for notifications