Skip to content

Commit

Permalink
Add Jsonrpsee::new_with_client (#735)
Browse files Browse the repository at this point in the history
* add access function and generated with new

* add wrapper and test

* add test

* udpate jsonrspee library
  • Loading branch information
haerdib committed Feb 27, 2024
1 parent a1775ad commit b44ac6b
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 10 deletions.
154 changes: 146 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ serde_json = { version = "1.0.79", default-features = false }
url = { version = "2.0.0", optional = true }

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

Expand Down
39 changes: 38 additions & 1 deletion src/rpc/jsonrpsee_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::rpc::{Error, Request, Result, RpcParams, Subscribe};
use jsonrpsee::{
client_transport::ws::{Url, WsTransportClientBuilder},
core::{
client::{Client, ClientBuilder, ClientT, SubscriptionClientT},
client::{Client, ClientBuilder, ClientT, Error as JsonrpseeError, SubscriptionClientT},
traits::ToRpcParams,
},
};
Expand All @@ -33,6 +33,7 @@ pub struct JsonrpseeClient {
}

impl JsonrpseeClient {
/// Create a new client to a local Substrate node with default port.
pub async fn with_default_url() -> Result<Self> {
Self::new("ws://127.0.0.1:9944").await
}
Expand All @@ -59,6 +60,42 @@ impl JsonrpseeClient {
let url = format!("{address}:{port:?}");
Self::new(&url).await
}

/// Create a new client with a user-generated Jsonrpsee Client.
pub fn new_with_client(client: Client) -> Self {
let inner = Arc::new(client);
Self { inner }
}
}

impl JsonrpseeClient {
/// Checks if the client is connected to the target.
pub fn is_connected(&self) -> bool {
self.inner.is_connected()
}

/// This is similar to [`Client::on_disconnect`] but it can be used to get
/// the reason why the client was disconnected but it's not cancel-safe.
///
/// The typical use-case is that this method will be called after
/// [`Client::on_disconnect`] has returned in a "select loop".
///
/// # Cancel-safety
///
/// This method is not cancel-safe
pub async fn disconnect_reason(&self) -> JsonrpseeError {
self.inner.disconnect_reason().await
}

/// Completes when the client is disconnected or the client's background task encountered an error.
/// If the client is already disconnected, the future produced by this method will complete immediately.
///
/// # Cancel safety
///
/// This method is cancel safe.
pub async fn on_disconnect(&self) {
self.inner.on_disconnect().await;
}
}

#[maybe_async::async_impl(?Send)]
Expand Down
2 changes: 2 additions & 0 deletions testing/async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ edition = "2021"
[dev-dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", features = ['derive'] }
tokio = { version = "1.24", features = ["rt-multi-thread", "macros", "time"] }
jsonrpsee = { version = "0.22", features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types", "server"] }


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

0 comments on commit b44ac6b

Please sign in to comment.