Skip to content

Commit b44ac6b

Browse files
authored
Add Jsonrpsee::new_with_client (#735)
* add access function and generated with new * add wrapper and test * add test * udpate jsonrspee library
1 parent a1775ad commit b44ac6b

File tree

5 files changed

+242
-10
lines changed

5 files changed

+242
-10
lines changed

Diff for: Cargo.lock

+146-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ serde_json = { version = "1.0.79", default-features = false }
4242
url = { version = "2.0.0", optional = true }
4343

4444
# websocket dependent features
45-
jsonrpsee = { version = "0.21", optional = true, features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types"] }
45+
jsonrpsee = { version = "0.22", optional = true, features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types"] }
4646
tungstenite = { version = "0.21", optional = true, features = ["native-tls"] }
4747
ws = { version = "0.9.2", optional = true, features = ["ssl"] }
4848

Diff for: src/rpc/jsonrpsee_client/mod.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::rpc::{Error, Request, Result, RpcParams, Subscribe};
1515
use jsonrpsee::{
1616
client_transport::ws::{Url, WsTransportClientBuilder},
1717
core::{
18-
client::{Client, ClientBuilder, ClientT, SubscriptionClientT},
18+
client::{Client, ClientBuilder, ClientT, Error as JsonrpseeError, SubscriptionClientT},
1919
traits::ToRpcParams,
2020
},
2121
};
@@ -33,6 +33,7 @@ pub struct JsonrpseeClient {
3333
}
3434

3535
impl JsonrpseeClient {
36+
/// Create a new client to a local Substrate node with default port.
3637
pub async fn with_default_url() -> Result<Self> {
3738
Self::new("ws://127.0.0.1:9944").await
3839
}
@@ -59,6 +60,42 @@ impl JsonrpseeClient {
5960
let url = format!("{address}:{port:?}");
6061
Self::new(&url).await
6162
}
63+
64+
/// Create a new client with a user-generated Jsonrpsee Client.
65+
pub fn new_with_client(client: Client) -> Self {
66+
let inner = Arc::new(client);
67+
Self { inner }
68+
}
69+
}
70+
71+
impl JsonrpseeClient {
72+
/// Checks if the client is connected to the target.
73+
pub fn is_connected(&self) -> bool {
74+
self.inner.is_connected()
75+
}
76+
77+
/// This is similar to [`Client::on_disconnect`] but it can be used to get
78+
/// the reason why the client was disconnected but it's not cancel-safe.
79+
///
80+
/// The typical use-case is that this method will be called after
81+
/// [`Client::on_disconnect`] has returned in a "select loop".
82+
///
83+
/// # Cancel-safety
84+
///
85+
/// This method is not cancel-safe
86+
pub async fn disconnect_reason(&self) -> JsonrpseeError {
87+
self.inner.disconnect_reason().await
88+
}
89+
90+
/// Completes when the client is disconnected or the client's background task encountered an error.
91+
/// If the client is already disconnected, the future produced by this method will complete immediately.
92+
///
93+
/// # Cancel safety
94+
///
95+
/// This method is cancel safe.
96+
pub async fn on_disconnect(&self) {
97+
self.inner.on_disconnect().await;
98+
}
6299
}
63100

64101
#[maybe_async::async_impl(?Send)]

Diff for: testing/async/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ edition = "2021"
88
[dev-dependencies]
99
codec = { package = "parity-scale-codec", version = "3.6.1", features = ['derive'] }
1010
tokio = { version = "1.24", features = ["rt-multi-thread", "macros", "time"] }
11+
jsonrpsee = { version = "0.22", features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types", "server"] }
12+
1113

1214
# Substrate dependencies
1315
frame-support = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "master" }

0 commit comments

Comments
 (0)