Skip to content
This repository was archived by the owner on Jan 2, 2020. It is now read-only.

Commit 089b931

Browse files
Change response of Client to directly return Result
1 parent 87115e2 commit 089b931

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

examples/hello_world.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use jsonrpc::*;
44

55
/// A trait defining the API you want to talk to. Not strictly necessary but nice for mocking.
66
trait HelloWorldApi {
7-
fn say_hello(&self, to: &str) -> Result<RpcResponse<String>, HTTPError>;
7+
fn say_hello(&self, to: &str) -> Result<Result<String, RpcError>, HTTPError>;
88
}
99

1010
/// An actual implementation of your client
@@ -13,7 +13,7 @@ struct HelloWorld {
1313
}
1414

1515
impl HelloWorldApi for HelloWorld {
16-
fn say_hello(&self, to: &str) -> Result<RpcResponse<String>, HTTPError> {
16+
fn say_hello(&self, to: &str) -> Result<Result<String, RpcError>, HTTPError> {
1717
self.client.send(&RpcRequest::new1(
1818
JsonRpcVersion::V1,
1919
"test",
@@ -31,7 +31,6 @@ fn main() {
3131
let result = api_client
3232
.say_hello("World")
3333
.unwrap() // Handle network error
34-
.into_result()
3534
.unwrap(); // Handle RpcError
3635

3736
println!("{}", result);

src/client.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use reqwest::{Client as HTTPClient, Error};
33
use response::RpcResponse;
44
use serde::{de::DeserializeOwned, Serialize};
55
use std::fmt::Debug;
6+
use RpcError;
67

78
pub struct RpcClient {
89
client: HTTPClient,
@@ -17,20 +18,26 @@ impl RpcClient {
1718
}
1819
}
1920

20-
pub fn send<R: Debug, T: Debug>(&self, request: &RpcRequest<T>) -> Result<RpcResponse<R>, Error>
21+
pub fn send<R: Debug, T: Debug>(
22+
&self,
23+
request: &RpcRequest<T>,
24+
) -> Result<Result<R, RpcError>, Error>
2125
where
2226
T: Serialize,
2327
R: DeserializeOwned,
2428
{
2529
debug!("Request: {:?}", request);
30+
2631
let res = self
2732
.client
2833
.post(self.url.as_str())
2934
.json(request)
3035
.send()
3136
.and_then(|mut res| res.json::<RpcResponse<R>>());
37+
3238
debug!("Response: {:?}", res);
33-
res
39+
40+
res.map(RpcResponse::into_result)
3441

3542
// TODO: Maybe check if req.id == res.id. Should always hold since it is a synchronous call.
3643
}

src/version.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pub enum JsonRpcVersion {
33
#[serde(rename = "1.0")]
44
V1,
5-
6-
#[serde(rename = "2.0")]
7-
V2,
5+
// At the moment, only version 1 is supported.
6+
// #[serde(rename = "2.0")]
7+
// V2,
88
}

0 commit comments

Comments
 (0)