substrate_api_client/rpc/
mod.rs1use ac_primitives::RpcParams;
19#[cfg(all(not(feature = "sync-api"), not(feature = "std")))]
20use alloc::boxed::Box;
21use alloc::string::{String, ToString};
22use serde::de::DeserializeOwned;
23
24#[cfg(feature = "ws-client")]
25#[allow(deprecated)]
26pub use ws_client::WsRpcClient;
27#[cfg(feature = "ws-client")]
28#[allow(deprecated)]
29pub mod ws_client;
30
31#[cfg(feature = "tungstenite-client")]
32pub use tungstenite_client::TungsteniteRpcClient;
33#[cfg(feature = "tungstenite-client")]
34pub mod tungstenite_client;
35
36#[cfg(all(feature = "jsonrpsee-client", not(feature = "sync-api")))]
37pub use jsonrpsee_client::JsonrpseeClient;
38#[cfg(all(feature = "jsonrpsee-client", not(feature = "sync-api")))]
39#[allow(dead_code)]
40#[allow(unused_imports)]
41pub mod jsonrpsee_client;
42
43pub mod error;
44#[cfg(any(feature = "ws-client", feature = "tungstenite-client"))]
45mod helpers;
46
47pub use error::{Error, Result};
48
49#[cfg(test)]
50pub mod mocks;
51
52#[maybe_async::maybe_async(?Send)]
54pub trait Request {
55 async fn request<R: DeserializeOwned>(&self, method: &str, params: RpcParams) -> Result<R>;
57}
58
59#[maybe_async::maybe_async(?Send)]
61pub trait Subscribe {
62 type Subscription<Notification>: HandleSubscription<Notification>
63 where
64 Notification: DeserializeOwned;
65
66 async fn subscribe<Notification: DeserializeOwned>(
67 &self,
68 sub: &str,
69 params: RpcParams,
70 unsub: &str,
71 ) -> Result<Self::Subscription<Notification>>;
72}
73
74#[maybe_async::maybe_async(?Send)]
77pub trait HandleSubscription<Notification: DeserializeOwned> {
78 async fn next(&mut self) -> Option<Result<Notification>>;
86
87 async fn unsubscribe(self) -> Result<()>;
89}
90
91pub fn to_json_req(method: &str, params: RpcParams) -> Result<String> {
92 Ok(serde_json::json!({
93 "method": method,
94 "params": params.to_json_value()?,
95 "jsonrpc": "2.0",
96 "id": "1",
97 })
98 .to_string())
99}