jsonrpsee_wasm_client/
lib.rs

1// Copyright 2019-2021 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any
4// person obtaining a copy of this software and associated
5// documentation files (the "Software"), to deal in the
6// Software without restriction, including without
7// limitation the rights to use, copy, modify, merge,
8// publish, distribute, sublicense, and/or sell copies of
9// the Software, and to permit persons to whom the Software
10// is furnished to do so, subject to the following
11// conditions:
12//
13// The above copyright notice and this permission notice
14// shall be included in all copies or substantial portions
15// of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
24// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25// DEALINGS IN THE SOFTWARE.
26
27//! # jsonrpsee-wasm-client
28
29#![warn(missing_docs, missing_debug_implementations, missing_copy_implementations, unreachable_pub)]
30#![cfg_attr(not(test), warn(unused_crate_dependencies))]
31#![cfg_attr(docsrs, feature(doc_cfg))]
32#![cfg(target_arch = "wasm32")]
33
34pub use jsonrpsee_core::client::Client;
35pub use jsonrpsee_types as types;
36
37use std::time::Duration;
38
39use jsonrpsee_client_transport::web;
40use jsonrpsee_core::client::{ClientBuilder, Error, IdKind};
41
42/// Builder for [`Client`].
43///
44/// # Examples
45///
46/// ```no_run
47///
48/// use jsonrpsee_wasm_client::WasmClientBuilder;
49///
50/// #[tokio::main]
51/// async fn main() {
52///     // build client
53///     let client = WasmClientBuilder::default()
54///          .build("wss://localhost:443")
55///          .await
56///          .unwrap();
57///
58///     // use client....
59/// }
60///
61/// ```
62#[derive(Copy, Clone, Debug)]
63pub struct WasmClientBuilder {
64	id_kind: IdKind,
65	max_concurrent_requests: usize,
66	max_buffer_capacity_per_subscription: usize,
67	max_log_length: u32,
68	request_timeout: Duration,
69}
70
71impl Default for WasmClientBuilder {
72	fn default() -> Self {
73		Self {
74			id_kind: IdKind::Number,
75			max_log_length: 4096,
76			max_concurrent_requests: 256,
77			max_buffer_capacity_per_subscription: 1024,
78			request_timeout: Duration::from_secs(60),
79		}
80	}
81}
82
83impl WasmClientBuilder {
84	/// Create a new WASM client builder.
85	pub fn new() -> WasmClientBuilder {
86		WasmClientBuilder::default()
87	}
88
89	/// See documentation [`ClientBuilder::request_timeout`] (default is 60 seconds).
90	pub fn request_timeout(mut self, timeout: Duration) -> Self {
91		self.request_timeout = timeout;
92		self
93	}
94
95	/// See documentation [`ClientBuilder::max_concurrent_requests`] (default is 256).
96	pub fn max_concurrent_requests(mut self, max: usize) -> Self {
97		self.max_concurrent_requests = max;
98		self
99	}
100
101	/// See documentation [`ClientBuilder::max_buffer_capacity_per_subscription`] (default is 1024).
102	pub fn max_buffer_capacity_per_subscription(mut self, max: usize) -> Self {
103		self.max_buffer_capacity_per_subscription = max;
104		self
105	}
106
107	/// See documentation for [`ClientBuilder::id_format`] (default is Number).
108	pub fn id_format(mut self, kind: IdKind) -> Self {
109		self.id_kind = kind;
110		self
111	}
112
113	/// Set maximum length for logging calls and responses.
114	///
115	/// Logs bigger than this limit will be truncated.
116	pub fn set_max_logging_length(mut self, max: u32) -> Self {
117		self.max_log_length = max;
118		self
119	}
120
121	/// Build the client with specified URL to connect to.
122	pub async fn build(self, url: impl AsRef<str>) -> Result<Client, Error> {
123		let Self {
124			max_log_length,
125			id_kind,
126			request_timeout,
127			max_concurrent_requests,
128			max_buffer_capacity_per_subscription,
129		} = self;
130		let (sender, receiver) = web::connect(url).await.map_err(|e| Error::Transport(e.into()))?;
131
132		let builder = ClientBuilder::default()
133			.set_max_logging_length(max_log_length)
134			.request_timeout(request_timeout)
135			.id_format(id_kind)
136			.max_buffer_capacity_per_subscription(max_buffer_capacity_per_subscription)
137			.max_concurrent_requests(max_concurrent_requests);
138
139		Ok(builder.build_with_wasm(sender, receiver))
140	}
141}