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 = "tungstenite-client")]
25pub use tungstenite_client::TungsteniteRpcClient;
26#[cfg(feature = "tungstenite-client")]
27pub mod tungstenite_client;
28
29#[cfg(all(feature = "jsonrpsee-client", not(feature = "sync-api")))]
30pub use jsonrpsee_client::JsonrpseeClient;
31#[cfg(all(feature = "jsonrpsee-client", not(feature = "sync-api")))]
32#[allow(dead_code)]
33#[allow(unused_imports)]
34pub mod jsonrpsee_client;
35
36pub mod error;
37#[cfg(feature = "tungstenite-client")]
38mod helpers;
39
40pub use error::{Error, Result};
41
42#[cfg(test)]
43pub mod mocks;
44
45#[maybe_async::maybe_async(?Send)]
47pub trait Request {
48 async fn request<R: DeserializeOwned>(&self, method: &str, params: RpcParams) -> Result<R>;
50}
51
52#[maybe_async::maybe_async(?Send)]
54pub trait Subscribe {
55 type Subscription<Notification>: HandleSubscription<Notification>
56 where
57 Notification: DeserializeOwned;
58
59 async fn subscribe<Notification: DeserializeOwned>(
60 &self,
61 sub: &str,
62 params: RpcParams,
63 unsub: &str,
64 ) -> Result<Self::Subscription<Notification>>;
65}
66
67#[maybe_async::maybe_async(?Send)]
70pub trait HandleSubscription<Notification: DeserializeOwned> {
71 async fn next(&mut self) -> Option<Result<Notification>>;
79
80 async fn unsubscribe(self) -> Result<()>;
82}
83
84pub fn to_json_req(method: &str, params: RpcParams) -> Result<String> {
85 Ok(serde_json::json!({
86 "method": method,
87 "params": params.to_json_value()?,
88 "jsonrpc": "2.0",
89 "id": "1",
90 })
91 .to_string())
92}