#jsonrpc #macro #http #client

jsonrpc_client

An async, macro-driven JSON-RPC client with pluggable backends

7 releases (breaking)

0.6.0 Apr 26, 2021
0.5.1 Feb 22, 2021
0.5.0 Jan 11, 2021
0.4.0 Jan 11, 2021
0.1.0 Aug 24, 2018

#26 in HTTP client

Download history 22/week @ 2021-01-15 19/week @ 2021-01-22 45/week @ 2021-01-29 134/week @ 2021-02-05 135/week @ 2021-02-12 455/week @ 2021-02-19 776/week @ 2021-02-26 352/week @ 2021-03-05 527/week @ 2021-03-12 451/week @ 2021-03-19 553/week @ 2021-03-26 265/week @ 2021-04-02 438/week @ 2021-04-09 441/week @ 2021-04-16 692/week @ 2021-04-23 907/week @ 2021-04-30

1,565 downloads per month
Used in bitcoin_rpc_client

MIT/Apache

24KB
443 lines

No bullshit JSON-RPC client for Rust

Features

  • No boilerplate: Driven by proc-macros
  • No client lock-in: Complete freedom over underlying HTTP client
  • Lightweight: Only depends on syn and serde
  • Async-ready

How does it work?

We take a trait as input to a proc-macro and output another one that has default implementations for all the functions. This allows us to take away all the boilerplate of making JSON-RPC calls and you get to define a nice interface at the same time!

How do I use it?

  1. Depend on jsonrpc_client:

    [dependencies]
    jsonrpc_client = { version = "*", features = ["reqwest"] }
    
  2. Define a trait that describes the JSON-RPC API you want to talk to and annotate it with #[jsonrpc_client::api]:

    #[jsonrpc_client::api]
    pub trait Math {
        async fn subtract(&self, subtrahend: i64, minuend: i64) -> i64;
    }
    
  3. Define your client:

    #[jsonrpc_client::implement(Math)]
    struct Client {
        inner: reqwest::Client,
        base_url: reqwest::Url,
    }
    
  4. Start using your client!

Backends

Currently, the client supports several backends, all of them can be activated via a separate feature-flag:

  • reqwest
  • surf
  • isahc

Support for more backends is welcomed.

Unfortunately, not all can be supported. In particular:

  • hreq: Cannot be supported because their Agent takes &mut self for sending a request which doesn't work with the SendRequest trait.
  • awc: Cannot be supported because their Client does not implement Send.

Dependencies

~2.2–8.5MB
~198K SLoC