async_jsonrpc_client/
transport.rs

1use std::error::Error;
2
3use futures::stream::Stream;
4use jsonrpc_types::*;
5
6/// A JSON-RPC 2.0 transport.
7#[async_trait::async_trait]
8pub trait Transport {
9    /// The transport error type.
10    type Error: Error;
11
12    /// Send a RPC call with the given method and parameters.
13    async fn request<M>(&self, method: M, params: Option<Params>) -> Result<Output, Self::Error>
14    where
15        M: Into<String> + Send;
16}
17
18/// A JSON-RPC 2.0 transport supporting batch requests.
19#[async_trait::async_trait]
20pub trait BatchTransport: Transport {
21    /// Send a batch of RPC calls with the given method and parameters.
22    async fn request_batch<I, M>(&self, batch: I) -> Result<Vec<Output>, Self::Error>
23    where
24        I: IntoIterator<Item = (M, Option<Params>)> + Send,
25        I::IntoIter: Send,
26        M: Into<String>;
27}
28
29/// A JSON-RPC 2.0 transport supporting subscriptions.
30#[async_trait::async_trait]
31pub trait PubsubTransport: Transport {
32    /// The subscription stream.
33    type NotificationStream: Stream<Item = SubscriptionNotification>;
34
35    /// Add a subscription to this transport.
36    ///
37    /// Will send unsubscribe request to the server when drop the notification stream.
38    async fn subscribe<M>(
39        &self,
40        subscribe_method: M,
41        params: Option<Params>,
42    ) -> Result<(Id, Self::NotificationStream), Self::Error>
43    where
44        M: Into<String> + Send;
45
46    /// Send an unsubscribe request to the server manually.
47    async fn unsubscribe<M>(&self, unsubscribe_method: M, subscription_id: Id) -> Result<bool, Self::Error>
48    where
49        M: Into<String> + Send;
50}