1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use request::RpcRequest;
use reqwest;
use reqwest::Client as HTTPClient;
use response::RpcResponse;
use serde::{de::DeserializeOwned, Serialize};
use serde_json;
use std::fmt::Debug;
use std::io::Read;
use RpcError;
pub struct RpcClient {
client: HTTPClient,
url: String,
}
#[derive(Debug)]
pub enum Error {
Transport(reqwest::Error),
Json(serde_json::Error),
}
impl RpcClient {
pub fn new(client: HTTPClient, url: &str) -> Self {
RpcClient {
client,
url: url.to_string(),
}
}
pub fn send<R: Debug, T: Debug>(
&self,
request: &RpcRequest<T>,
) -> Result<Result<R, RpcError>, Error>
where
T: Serialize,
R: DeserializeOwned,
{
trace!(">>> {}", serde_json::to_string(request).unwrap());
let res = self
.client
.post(self.url.as_str())
.json(request)
.send()
.map_err(Error::Transport)
.and_then(|mut res| {
let mut buf = String::new();
let _ = res.read_to_string(&mut buf);
trace!("<<< {}", buf);
serde_json::from_str(&buf).map_err(Error::Json)
});
res.map(RpcResponse::into_result)
}
}