Skip to content

Commit 6868df8

Browse files
authored
rpc: Add new_with_timeout for RpcClient (#158)
Signed-off-by: Eval EXEC <execvy@gmail.com>
2 parents 3a418e7 + ad5f1d4 commit 6868df8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/rpc/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ macro_rules! jsonrpc {
107107
Ok($struct_name { id: 0.into(), client })
108108
}
109109

110+
pub fn new_with_timeout(uri: &str, timeout: std::time::Duration) -> Result<Self, anyhow::Error> {
111+
let client = $crate::rpc::RpcClient::new_with_timeout(uri, timeout)?;
112+
Ok($struct_name { id: 0.into(), client })
113+
}
114+
110115
pub fn post<PARAM, RET>(&self, method:&str, params: PARAM)->Result<RET, $crate::rpc::RpcError>
111116
where
112117
PARAM:serde::ser::Serialize + Send + 'static,
@@ -195,6 +200,12 @@ macro_rules! jsonrpc_async {
195200
Ok($struct_name { id: 0.into(), client })
196201
}
197202

203+
#[cfg(not(target_arch="wasm32"))]
204+
pub fn new_with_timeout(uri: &str, timeout: std::time::Duration) -> Result<Self, anyhow::Error> {
205+
let client = $crate::rpc::RpcClient::new_with_timeout(uri, timeout)?;
206+
Ok($struct_name { id: 0.into(), client })
207+
}
208+
198209
#[cfg(not(target_arch="wasm32"))]
199210
pub fn post<PARAM, RET>(&self, method:&str, params: PARAM)->impl std::future::Future<Output =Result<RET, $crate::rpc::RpcError>> + Send + 'static
200211
where
@@ -288,6 +299,14 @@ impl RpcClient {
288299
Ok(Self { client, url })
289300
}
290301

302+
#[cfg(not(target_arch = "wasm32"))]
303+
pub fn new_with_timeout(
304+
uri: &str,
305+
timeout: std::time::Duration,
306+
) -> Result<Self, anyhow::Error> {
307+
Self::with_builder(uri, |builder| builder.timeout(timeout))
308+
}
309+
291310
#[cfg(not(target_arch = "wasm32"))]
292311
pub fn new_with_cookie(uri: &str) -> Self {
293312
let url = reqwest::Url::parse(uri).expect("ckb uri, e.g. \"http://127.0.0.1:8114\"");

0 commit comments

Comments
 (0)