Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Accept also None or Objects as params
Browse files Browse the repository at this point in the history
according to the jsonrpc spec params can be missing, or an object too
  • Loading branch information
RCasatta committed Nov 14, 2023
1 parent 1d047b0 commit 34356a9
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn client(url: &str, user: &str, pass: &str) -> Result<Client, simple_http::Erro
// Demonstrate an example JSON-RCP call against bitcoind.
fn main() {
let client = client("localhost:18443", "user", "pass").expect("failed to create client");
let request = client.build_request("uptime", &[]);
let request = client.build_request("uptime", None);
let response = client.send_request(request).expect("send_request failed");

// For other commands this would be a struct matching the returned json.
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/minreq_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn do_test(data: &[u8]) {
.build();

let client = Client::with_transport(t);
let request = client.build_request("uptime", &[]);
let request = client.build_request("uptime", None);
let _ = client.send_request(request);
}
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/simple_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn do_test(data: &[u8]) {
.build();

let client = Client::with_transport(t);
let request = client.build_request("uptime", &[]);
let request = client.build_request("uptime", None);
let _ = client.send_request(request);
}
}
Expand Down
2 changes: 1 addition & 1 deletion integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn main() {
fn test_get_network_info(cl: &Client) {
let request = Request {
method: "getnetworkinfo".into(),
params: &[],
params: None,
id: serde_json::json!(1),
jsonrpc: Some("2.0"),
};
Expand Down
8 changes: 4 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Client {
///
/// To construct the arguments, one can use one of the shorthand methods
/// [`crate::arg`] or [`crate::try_arg`].
pub fn build_request<'a>(&self, method: &'a str, params: &'a [Box<RawValue>]) -> Request<'a> {
pub fn build_request<'a>(&self, method: &'a str, params: Option<&'a RawValue>) -> Request<'a> {
let nonce = self.nonce.fetch_add(1, atomic::Ordering::Relaxed);
Request {
method,
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Client {
pub fn call<R: for<'a> serde::de::Deserialize<'a>>(
&self,
method: &str,
args: &[Box<RawValue>],
args: Option<&RawValue>,
) -> Result<R, Error> {
let request = self.build_request(method, args);
let id = request.id.clone();
Expand Down Expand Up @@ -224,9 +224,9 @@ mod tests {
fn sanity() {
let client = Client::with_transport(DummyTransport);
assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 1);
let req1 = client.build_request("test", &[]);
let req1 = client.build_request("test", None);
assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 2);
let req2 = client.build_request("test", &[]);
let req2 = client.build_request("test", None);
assert_eq!(client.nonce.load(sync::atomic::Ordering::Relaxed), 3);
assert!(req1.id != req2.id);
}
Expand Down
4 changes: 2 additions & 2 deletions src/http/simple_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,11 @@ mod tests {
let port = rx.recv().unwrap();
let client =
Client::simple_http(format!("localhost:{}", port).as_str(), None, None).unwrap();
let request = client.build_request("test_request", &[]);
let request = client.build_request("test_request", None);
let result = client.send_request(request).unwrap();
assert_eq!(result.id, Value::Number(Number::from(0)));
thread::sleep(Duration::from_secs(1));
let request = client.build_request("test_request2", &[]);
let request = client.build_request("test_request2", None);
let result2 = client.send_request(request)
.expect("This second request should not be an Err like `Err(Transport(HttpResponseTooShort { actual: 0, needed: 12 }))`");
assert_eq!(result2.id, Value::Number(Number::from(1)));
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct Request<'a> {
/// The name of the RPC call.
pub method: &'a str,
/// Parameters to the RPC call.
pub params: &'a [Box<RawValue>],
pub params: Option<&'a RawValue>,
/// Identifier for this request, which should appear in the response.
pub id: serde_json::Value,
/// jsonrpc field, MUST be "2.0".
Expand Down
2 changes: 1 addition & 1 deletion src/simple_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mod tests {
let addr = server.local_addr().unwrap();
let dummy_req = Request {
method: "arandommethod",
params: &[],
params: None,
id: serde_json::Value::Number(4242242.into()),
jsonrpc: Some("2.0"),
};
Expand Down
2 changes: 1 addition & 1 deletion src/simple_uds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mod tests {
let server = UnixListener::bind(&socket_path).unwrap();
let dummy_req = Request {
method: "getinfo",
params: &[],
params: None,
id: serde_json::Value::Number(111.into()),
jsonrpc: Some("2.0"),
};
Expand Down

0 comments on commit 34356a9

Please sign in to comment.