Crate jsonrpc_client

Source
Expand description

A macro-driven JSON-RPC client.

This crate offers a macro-driven approach to interacting with JSON-RPC APIs. JSON-RPC itself is pretty lightweight, yet it is pretty verbose if you have to roll a client from scratch on top of say reqwest.

This crate abstracts away this boilerplate by allowing you to define a trait that resembles the API you want to talk to. At the same time, we give full control the user over which HTTP client they want to use to actually send the request.

§Example

#[cfg(all(feature = "macros", feature = "reqwest"))]
#[jsonrpc_client::api]
pub trait Math {
    async fn subtract(&self, subtrahend: i64, minuend: i64) -> i64;
}

#[cfg(all(feature = "macros", feature = "reqwest"))]
#[jsonrpc_client::implement(Math)]
struct Client {
    inner: reqwest::Client,
    base_url: reqwest::Url,
}
#[cfg(all(feature = "macros", feature = "reqwest"))]
#[cfg(all(feature = "macros", feature = "reqwest"))]

let client = Client::new("http://example-jsonrpc.org/".to_owned())?;

client.subtract(10, 5).await?;

§Backends

This crate supports several backends out of the box. Concretely:

  • reqwest
  • surf
  • isahc

To use any (or all) of these backends, simply activate the corresponding feature-flag:

[dependencies]
jsonrpc_client = { version = "*", features = ["reqwest", "surf", "isahc"] }

Modules§

export

Structs§

JsonRpcError
A JSON-RPC error.
Request
A JSON-RPC request.
Response
A JSON-RPC response.
Url
A parsed URL record.

Enums§

Error
Id
The ID of a JSON-RPC request.
Params
Version
The JSON-RPC version.

Traits§

SendRequest
A trait abstracting over how a request is actually sent to a server.

Attribute Macros§

api
Define the API of the JSON-RPC server you want to talk to.
implement
Implement a given API trait on this client.