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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
use rustc_serialize::{json, Decodable, Encodable};
use time;
use http::{AuthClient, Client};
use super::Url;
/// Encode the body of a JSON-RPC call.
#[derive(RustcDecodable, RustcEncodable)]
pub struct RpcRequest<E: Encodable> {
pub jsonrpc: String,
pub id: u64,
pub method: String,
pub params: E
}
impl<E: Encodable> RpcRequest<E> {
/// Instantiate a new `RpcRequest` with the default version (2.0) and an id
/// generated from the current time.
pub fn new(method: &str, params: E) -> RpcRequest<E> {
RpcRequest {
jsonrpc: "2.0".to_string(),
id: time::precise_time_ns(),
method: method.to_string(),
params: params
}
}
/// Send a JSON-RPC POST request to the specified URL.
pub fn send(&self, url: Url) -> Result<String, String> {
let client = AuthClient::default();
let body = json::encode(self).expect("couldn't encode RpcRequest");
let resp_rx = client.post(url, Some(body.into_bytes()));
let resp = resp_rx.recv().expect("no RpcRequest response received");
let data = try!(resp.map_err(|err| format!("{}", err)));
String::from_utf8(data).map_err(|err| format!("{}", err))
}
}
/// Encapsulates a successful JSON-RPC response.
#[derive(RustcDecodable, RustcEncodable)]
pub struct RpcOk<D: Decodable> {
pub jsonrpc: String,
pub id: u64,
pub result: Option<D>
}
impl<D: Decodable> RpcOk<D> {
/// Instantiate a new successful JSON-RPC response type.
pub fn new(id: u64, result: Option<D>) -> RpcOk<D> {
RpcOk {
jsonrpc: "2.0".to_string(),
id: id,
result: result
}
}
}
/// The error code as [specified by jsonrpc](http://www.jsonrpc.org/specification#error_object).
#[derive(RustcDecodable, RustcEncodable)]
pub struct ErrorCode {
pub code: i32,
pub message: String,
pub data: String
}
/// Encapsulates a failed JSON-RPC response.
#[derive(RustcDecodable, RustcEncodable)]
pub struct RpcErr {
pub jsonrpc: String,
pub id: u64,
pub error: ErrorCode
}
impl RpcErr {
/// Instantiate a new `RpcErr` type with the default JSON-RPC version (2.0).
pub fn new(id: u64, error: ErrorCode) -> Self {
RpcErr { jsonrpc: "2.0".to_string(), id: id, error: error }
}
/// Create a new `RpcErr` with a reason of "Invalid Request".
pub fn invalid_request(id: u64, data: String) -> Self {
Self::new(id, ErrorCode { code: -32600, message: "Invalid Request".to_string(), data: data })
}
/// Create a new `RpcErr` with a reason of "Method not found".
pub fn method_not_found(id: u64, data: String) -> Self {
Self::new(id, ErrorCode { code: -32601, message: "Method not found".to_string(), data: data })
}
/// Create a new `RpcErr` with a reason of "Parse error".
pub fn parse_error(data: String) -> Self {
Self::new(0, ErrorCode { code: -32700, message: "Parse error".to_string(), data: data })
}
/// Create a new `RpcErr` with a reason of "Invalid params".
pub fn invalid_params(id: u64, data: String) -> Self {
Self::new(id, ErrorCode { code: -32602, message: "Invalid params".to_string(), data: data })
}
/// Create a new `RpcErr` with a reason of "Couldn't handle request".
pub fn unspecified(id: u64, data: String) -> Self {
Self::new(id, ErrorCode { code: -32100, message: "Couldn't handle request".to_string(), data: data })
}
}
|