-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change(tests): adds RPC request client for condensing shared code (#5619
) * Adds RPCRequestClient * uses RPCRequestClient in the rest of the rpc calls * removes duplicate expect call * fixed mistaken "get_info" method call with "getinfo" Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
816d845
commit 61af406
Showing
6 changed files
with
75 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! A client for calling Zebra's Json-RPC methods | ||
use std::net::SocketAddr; | ||
|
||
use reqwest::Client; | ||
|
||
/// An http client for making Json-RPC requests | ||
pub struct RPCRequestClient { | ||
client: Client, | ||
rpc_address: SocketAddr, | ||
} | ||
|
||
impl RPCRequestClient { | ||
/// Creates new RPCRequestSender | ||
pub fn new(rpc_address: SocketAddr) -> Self { | ||
Self { | ||
client: Client::new(), | ||
rpc_address, | ||
} | ||
} | ||
|
||
/// Builds rpc request | ||
pub async fn call( | ||
&self, | ||
method: &'static str, | ||
params: impl Into<String>, | ||
) -> reqwest::Result<reqwest::Response> { | ||
let params = params.into(); | ||
self.client | ||
.post(format!("http://{}", &self.rpc_address)) | ||
.body(format!( | ||
r#"{{"jsonrpc": "2.0", "method": "{method}", "params": {params}, "id":123 }}"# | ||
)) | ||
.header("Content-Type", "application/json") | ||
.send() | ||
.await | ||
} | ||
|
||
/// Builds rpc request and gets text from response | ||
pub async fn text_from_call( | ||
&self, | ||
method: &'static str, | ||
params: impl Into<String>, | ||
) -> reqwest::Result<String> { | ||
self.call(method, params).await?.text().await | ||
} | ||
} |