Skip to content

Commit

Permalink
refactor(jsonrpsee-ws-client): add new fns to WsClientBuilder
Browse files Browse the repository at this point in the history
- feat: add new `WsClientBuilder::build_with_transport` that builds and
  returns a `WsClient` with the given `Sender` and `Receiver`.
- feat: add new `WsClientBuilder::build_with_stream` that uses the new
  `WsTransportClientBuilder::build_with_stream`, building and returning
  the `WsClient` with the given `data_stream` as transport layer.
- refactor: update the `WsClientBuilder::build` to use the new
  `build_with_transport`, it helps not having duplicated code.
  • Loading branch information
oleonardolima committed Nov 20, 2023
1 parent 4ee65c4 commit 138067e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 24 deletions.
2 changes: 1 addition & 1 deletion client/transport/src/ws/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::net::SocketAddr;
use std::time::Duration;

use futures_util::io::{BufReader, BufWriter};
use futures_util::{AsyncRead, AsyncWrite};
pub use futures_util::{AsyncRead, AsyncWrite};
use jsonrpsee_core::client::{CertificateStore, MaybeSend, ReceivedMessage, TransportReceiverT, TransportSenderT};
use jsonrpsee_core::TEN_MB_SIZE_BYTES;
use jsonrpsee_core::{async_trait, Cow};
Expand Down
83 changes: 60 additions & 23 deletions client/ws-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ pub use http::{HeaderMap, HeaderValue};
use std::time::Duration;
use url::Url;

use jsonrpsee_client_transport::ws::WsTransportClientBuilder;
use jsonrpsee_core::client::{CertificateStore, ClientBuilder, IdKind};
use jsonrpsee_client_transport::ws::{AsyncRead, AsyncWrite, WsTransportClientBuilder};
use jsonrpsee_core::client::{
CertificateStore, ClientBuilder, IdKind, MaybeSend, TransportReceiverT, TransportSenderT,
};
use jsonrpsee_core::{Error, TEN_MB_SIZE_BYTES};

/// Builder for [`WsClient`].
Expand Down Expand Up @@ -213,40 +215,26 @@ impl WsClientBuilder {
self
}

/// Build the client with specified URL to connect to.
/// You must provide the port number in the URL.
/// Build the [`WsClient`] with specified [`TransportSenderT`] [`TransportReceiverT`] parameters
///
/// ## Panics
///
/// Panics if being called outside of `tokio` runtime context.
pub async fn build(self, url: impl AsRef<str>) -> Result<WsClient, Error> {
pub async fn build_with_transport<S, R>(self, sender: S, receiver: R) -> Result<WsClient, Error>
where
S: TransportSenderT + Send,
R: TransportReceiverT + Send,
{
let Self {
certificate_store,
max_concurrent_requests,
max_request_size,
max_response_size,
request_timeout,
connection_timeout,
ping_interval,
headers,
max_redirections,
max_buffer_capacity_per_subscription,
id_kind,
max_log_length,
..
} = self;

let transport_builder = WsTransportClientBuilder {
certificate_store,
connection_timeout,
headers,
max_request_size,
max_response_size,
max_redirections,
};

let uri = Url::parse(url.as_ref()).map_err(|e| Error::Transport(e.into()))?;
let (sender, receiver) = transport_builder.build(uri).await.map_err(|e| Error::Transport(e.into()))?;

let mut client = ClientBuilder::default()
.max_buffer_capacity_per_subscription(max_buffer_capacity_per_subscription)
.request_timeout(request_timeout)
Expand All @@ -260,4 +248,53 @@ impl WsClientBuilder {

Ok(client.build_with_tokio(sender, receiver))
}

/// Build the [`WsClient`] with specified data stream, using [`WsTransportClientBuilder::build_with_stream`].
///
/// ## Panics
///
/// Panics if being called outside of `tokio` runtime context.
pub async fn build_with_stream<T>(self, url: impl AsRef<str>, data_stream: T) -> Result<WsClient, Error>
where
T: AsyncRead + AsyncWrite + Unpin + MaybeSend + 'static,
{
let transport_builder = WsTransportClientBuilder {
certificate_store: self.certificate_store,
connection_timeout: self.connection_timeout,
headers: self.headers.clone(),
max_request_size: self.max_request_size,
max_response_size: self.max_response_size,
max_redirections: self.max_redirections,
};

let uri = Url::parse(url.as_ref()).map_err(|e| Error::Transport(e.into()))?;
let (sender, receiver) =
transport_builder.build_with_stream(uri, data_stream).await.map_err(|e| Error::Transport(e.into()))?;

let ws_client = self.build_with_transport(sender, receiver).await?;
Ok(ws_client)
}

/// Build the [`WsClient`] with specified URL to connect to, using the default
/// [`WsTransportClientBuilder::build_with_stream`], therefore with the default TCP as transport layer.
///
/// ## Panics
///
/// Panics if being called outside of `tokio` runtime context.
pub async fn build(self, url: impl AsRef<str>) -> Result<WsClient, Error> {
let transport_builder = WsTransportClientBuilder {
certificate_store: self.certificate_store,
connection_timeout: self.connection_timeout,
headers: self.headers.clone(),
max_request_size: self.max_request_size,
max_response_size: self.max_response_size,
max_redirections: self.max_redirections,
};

let uri = Url::parse(url.as_ref()).map_err(|e| Error::Transport(e.into()))?;
let (sender, receiver) = transport_builder.build(uri).await.map_err(|e| Error::Transport(e.into()))?;

let ws_client = self.build_with_transport(sender, receiver).await?;
Ok(ws_client)
}
}

0 comments on commit 138067e

Please sign in to comment.