The goal is to build a tiny client library which can construct a high or low level RPC client with a couple of lines of code.
The library is perfect to write small code snippets for testing JSON-RPC servers but can be also used in production applications as well.
```rust use bmajrpc::{httpclient, rpc_client}; use serde::Deserialize; use std::time::Duration;
// Define a trait to map all RPC methods
trait My { // the method returns null and has no params fn test(&self); // the method returns a structure fn login(&self, user: &str, password: &str) -> LoginResponse; // the method is mapped to RPC method "login" // it returns a structure but "resultfield" attribute argument // automatically extracts "token" field only #[rpc(name = "login", resultfield = "token")] fn authenticate(&self, user: &str, password: &str) -> String; }
// The structure MyClient is automatically created for the above trait with a method "new"
// the response structure for the full "login" method output
struct LoginResponse { api_version: u16, token: String, }
// create a low-level HTTP RPC client let httpclient = httpclient("http://localhost:7727").timeout(Duration::fromsecs(2)); // create the high-level client let client = MyClient::new(httpclient); let token = client.authenticate("admin", "xxx").unwrap(); dbg!(token); let result: LoginResponse = client.login("admin", "xxx").unwrap(); dbg!(result); ```
```rust use bmajrpc::{httpclient, Rpc}; use serde::{Deserialize, Serialize}; use std::time::Duration;
struct LoginResponse { api_version: u16, token: String, }
struct LoginPayload<'a> { user: &'a str, password: &'a str, }
// create a low-level HTTP RPC client let httpclient = httpclient("http://localhost:7727").timeout(Duration::fromsecs(2)); // use it directly. the params can be any which implements Serialize, the // repsonse can be any which implements Deserialize let result: LoginResponse = httpclient.call( "login", LoginPayload { user: "admin", password: "xxx", }, ).unwrap(); ```
with "msgpack" crate feature an optional MessagePack RPC de/serialization can be enabled:
```rust use bma_jrpc::{HttpClient, MsgPack};
// create a low-level HTTP RPC client
let httpclient = HttpClient::
Bulk RPC requests
RPC requests with no reply required (with no ID)
Async in high-level clients