#json-rpc #rpc #json #json-rpc-client #serde #json-rpc-server

tetsy-jsonrpc-client-transports

Tetsy Transport agnostic JSON-RPC 2.0 client implementation

Show the crate…

3 stable releases

15.1.0 Mar 13, 2021
14.2.1 Mar 1, 2021

#56 in #json-rpc-client

Download history 39/week @ 2023-12-07 41/week @ 2023-12-14 43/week @ 2023-12-21 14/week @ 2023-12-28 34/week @ 2024-01-04 74/week @ 2024-01-11 33/week @ 2024-01-18 23/week @ 2024-01-25 13/week @ 2024-02-01 37/week @ 2024-02-08 60/week @ 2024-02-15 55/week @ 2024-02-22 60/week @ 2024-02-29 78/week @ 2024-03-07 48/week @ 2024-03-14 59/week @ 2024-03-21

258 downloads per month
Used in 38 crates (2 directly)

MIT license

175KB
4.5K SLoC

Parity JSON-RPC

Rust implementation of JSON-RPC 2.0 Specification. Transport-agnostic core and transport servers for http, ipc, websockets and tcp.

New! Support for clients.

Documentation

Sub-projects

Examples

Basic Usage (with HTTP transport)

use jsonrpc_http_server::jsonrpc_core::{IoHandler, Value, Params};
use jsonrpc_http_server::ServerBuilder;

fn main() {
	let mut io = IoHandler::default();
	io.add_method("say_hello", |_params: Params| async {
		Ok(Value::String("hello".to_owned()))
	});

	let server = ServerBuilder::new(io)
		.threads(3)
		.start_http(&"127.0.0.1:3030".parse().unwrap())
		.unwrap();

	server.wait();
}

Basic usage with derive

use jsonrpc_core::Result;
use jsonrpc_derive::rpc;

#[rpc]
pub trait Rpc {
	/// Adds two numbers and returns a result
	#[rpc(name = "add")]
	fn add(&self, u64, u64) -> Result<u64>;
}

pub struct RpcImpl;
impl Rpc for RpcImpl {
	fn add(&self, a: u64, b: u64) -> Result<u64> {
		Ok(a + b)
	}
}

fn main() {
	let mut io = jsonrpc_core::IoHandler::new();
	io.extend_with(RpcImpl.to_delegate())
}

Client support

use jsonrpc_core_client::transports::local;
use jsonrpc_core::{Error, IoHandler, Result};
use jsonrpc_derive::rpc;

/// Rpc trait
#[rpc]
pub trait Rpc {
	/// Returns a protocol version
	#[rpc(name = "protocolVersion")]
	fn protocol_version(&self) -> Result<String>;

	/// Adds two numbers and returns a result
	#[rpc(name = "add", alias("callAsyncMetaAlias"))]
	fn add(&self, a: u64, b: u64) -> Result<u64>;

	/// Performs asynchronous operation
	#[rpc(name = "callAsync")]
	fn call(&self, a: u64) -> FutureResult<String, Error>;
}

struct RpcImpl;

impl Rpc for RpcImpl {
	fn protocol_version(&self) -> Result<String> {
		Ok("version1".into())
	}

	fn add(&self, a: u64, b: u64) -> Result<u64> {
		Ok(a + b)
	}

	fn call(&self, _: u64) -> FutureResult<String, Error> {
		future::ok("OK".to_owned())
	}
}

fn main() {
	let mut io = IoHandler::new();
	io.extend_with(RpcImpl.to_delegate());

	let fut = {
		let (client, server) = local::connect::<gen_client::Client, _, _>(io);
		client.add(5, 6).map(|res| println!("5 + 6 = {}", res)).join(server)
	};
	fut.wait().unwrap();
}

Dependencies

~3–18MB
~257K SLoC