bitcoind_request/
client.rs

1use std::time::Duration;
2
3use jsonrpc::{
4    simple_http::{self, SimpleHttpTransport},
5    Client as JsonRPCClient, Request as JsonRPCRequest, Response as JsonRPCResponse,
6};
7use serde_json::value::RawValue;
8
9pub struct Client {
10    json_rpc_client: JsonRPCClient,
11}
12
13pub struct Request<'a>(JsonRPCRequest<'a>);
14
15impl<'a> Request<'a> {}
16
17impl Client {
18    // TODO: Add error handling if this fails
19    pub fn new(url: &str, user: &str, pass: &str) -> Result<Self, simple_http::Error> {
20        // The default in the library is 15 seconds, but we're setting to very high here to prevent error
21        // during the call to gettxoutsetinfo.
22        let timeout = Duration::from_secs(300);
23        let simple_http_transport = SimpleHttpTransport::builder()
24            .url(url)?
25            .auth(user, Some(pass))
26            .timeout(timeout)
27            .build();
28        let client = Client {
29            json_rpc_client: JsonRPCClient::with_transport(simple_http_transport),
30        };
31        Ok(client)
32    }
33    pub fn build_request<'a>(
34        &self,
35        command: &'a str,
36        params: &'a Vec<Box<RawValue>>,
37    ) -> Request<'a> {
38        let json_rpc_request = self.json_rpc_client.build_request(command, &params);
39        let request = Request(json_rpc_request);
40        request
41    }
42    pub fn send_request(&self, request: Request) -> Result<JsonRPCResponse, jsonrpc::Error> {
43        let response = self.json_rpc_client.send_request(request.0);
44        response
45    }
46}