#json-rpc-client #json-rpc #macro #rpc #client #generate #strongly-typed

jsonrpc-client-core

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

6 releases (breaking)

Uses old Rust 2015

0.5.0 Jun 25, 2018
0.4.0 Jun 18, 2018
0.3.0 Mar 6, 2018
0.2.1 Sep 11, 2017
0.1.0 Jul 19, 2017

#2269 in Parser implementations

Download history 42/week @ 2023-12-18 28/week @ 2023-12-25 7/week @ 2024-01-01 47/week @ 2024-01-08 33/week @ 2024-01-15 20/week @ 2024-01-22 11/week @ 2024-01-29 25/week @ 2024-02-05 55/week @ 2024-02-12 29/week @ 2024-02-19 64/week @ 2024-02-26 55/week @ 2024-03-04 47/week @ 2024-03-11 65/week @ 2024-03-18 59/week @ 2024-03-25 186/week @ 2024-04-01

368 downloads per month
Used in 8 crates (7 directly)

MIT/Apache

20KB
319 lines

jsonrpc-client-core

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 generates structs that can be used for calling JSON-RPC 2.0 APIs. The macro lets you list methods on the struct with arguments and a return type. The macro then generates a struct which will automatically serialize the arguments, send the request and deserialize the response into the target type.

Transports

The jsonrpc-client-core crate itself and the structs generated by the jsonrpc_client macro are transport agnostic. They can use any type implementing the Transport trait.

The main (and so far only) transport implementation is the Hyper based HTTP implementation in the jsonrpc-client-http crate.

Example

#[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) -> RpcRequest<String>;
});

fn main() {
    let transport = HttpTransport::new().standalone().unwrap();
    let transport_handle = transport
        .handle("http://api.fizzbuzzexample.org/rpc/")
        .unwrap();
    let mut client = FizzBuzzClient::new(transport_handle);
    let result1 = client.fizz_buzz(3).call().unwrap();
    let result2 = client.fizz_buzz(4).call().unwrap();
    let result3 = client.fizz_buzz(5).call().unwrap();

    // Should print "fizz 4 buzz" if the server implemented the service correctly
    println!("{} {} {}", result1, result2, result3);
}

License: MIT/Apache-2.0

Dependencies

~3.5–5.5MB
~109K SLoC