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 9, 2023
1 parent b8c3f0d commit 235f416
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ 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 Box<RawValue>>,
) -> Request<'a> {
let nonce = self.nonce.fetch_add(1, atomic::Ordering::Relaxed);
Request {
method,
Expand Down Expand Up @@ -115,7 +119,7 @@ impl Client {
pub fn call<R: for<'a> serde::de::Deserialize<'a>>(
&self,
method: &str,
args: &[Box<RawValue>],
args: Option<&Box<RawValue>>,
) -> Result<R, Error> {
let request = self.build_request(method, args);
let id = request.id.clone();
Expand Down Expand Up @@ -224,9 +228,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 Box<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

0 comments on commit 235f416

Please sign in to comment.