Crate jsonrpc_client_core [] [src]

A crate for generating transport agnostic, auto serializing, strongly typed JSON-RPC 2.0 clients

This crate mainly provides a macro, jsonrpc_client. The macro is be used to generate structs for calling JSON-RPC 2.0 APIs. The macro lets you list methods on the struct with arguments and a return value. The macro then generates a struct which will automatically serialize the arguments, send the request and deserialize the response into the target type.

Example

Look at the ExampleRpcClient struct in this crate. It uses the library to generate itself.

Here is an example of how to generate and use a client struct:

#[macro_use] extern crate jsonrpc_client_core;
extern crate jsonrpc_client_http;

use jsonrpc_client_http::HttpTransport;

jsonrpc_client!(pub struct FizzBuzzClient {
    /// Returns the fizz-buzz string for the given number.
    pub fn fizz_buzz(&mut self, number: u64) -> Result<String>;
});

fn main() {
    let transport = HttpTransport::new("https://api.fizzbuzzexample.org/rpc/").unwrap();
    let mut client = FizzBuzzClient::new(transport);
    let result1 = client.fizz_buzz(3).unwrap();
    let result2 = client.fizz_buzz(4).unwrap();
    let result3 = client.fizz_buzz(5).unwrap();
    // Should print "fizz 4 buzz" if the server implemented the service correctly
    println!("{} {} {}", result1, result2, result3);
}

Macros

expand_params

Expands a variable list of parameters into its serializable form. Is needed to make the params of a nullary method [] instead of () and thus make sure it serializes to [] instead of null.

jsonrpc_client

The main macro of this crate. Generates JSON-RPC 2.0 client structs with automatic serialization and deserialization. Method calls get correct types automatically.

Structs

Error

The Error type.

ExampleRpcClient

Just an example RPC client to showcase how to use the jsonrpc_client macro and what the resulting structs look like.

Enums

ErrorKind

The kind of an error.

Traits

ResultExt

Additional methods for Result, for easy interaction with this crate.

Transport

Trait for types acting as a transport layer for the JSON-RPC 2.0 clients generated by the jsonrpc_client macro.

Functions

call_method

Call a method with a given transport, method and parameters. Not intended for direct use. Is being called from the client structs generated by the jsonrpc_client macro.

Type Definitions

Result

Convenient wrapper around std::Result.