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
108
109
#[cfg(feature = "client")]
#[derive(Clone)]
pub struct HttpClient {
    client: reqwest::Client,
    url: String,
}

#[cfg(feature = "client")]
impl HttpClient {
    pub fn new(url: String) -> Self {
        Self {
            client: reqwest::Client::new(),
            url,
        }
    }

    pub fn url(&self) -> &str {
        &self.url
    }

    pub fn with_client(url: String, client: reqwest::Client) -> Self {
        Self { client, url }
    }

    pub async fn rpc(
        &self,
        method: &str,
        params: &serde_json::value::RawValue,
    ) -> anyhow::Result<serde_json::Value> {
        let response_body = self
            .client
            .post(&self.url)
            .header("content-type", "application/json")
            .body(serde_json::to_string(&serde_json::json!({
                "jsonrpc": "2.0",
                "id": 0,
                "method": method,
                "params": params,
            }))?)
            .send()
            .await?
            .error_for_status()?
            .bytes()
            .await?;
        let result = serde_json::from_slice::<jsonrpc_core::Response>(&response_body[..])?;
        let result = match result {
            jsonrpc_core::Response::Single(o) => match o {
                jsonrpc_core::Output::Success(s) => s.result,
                jsonrpc_core::Output::Failure(f) => return Err(f.error.into()),
            },
            _ => anyhow::bail!("unexpected batch response"),
        };
        Ok(result)
    }
}

#[cfg(feature = "blocking-client")]
#[derive(Clone)]
pub struct BlockingHttpClient {
    client: reqwest::blocking::Client,
    url: String,
}

#[cfg(feature = "blocking-client")]
impl BlockingHttpClient {
    pub fn new(url: String) -> Self {
        Self {
            client: reqwest::blocking::Client::new(),
            url,
        }
    }

    pub fn url(&self) -> &str {
        &self.url
    }

    pub fn with_client(url: String, client: reqwest::blocking::Client) -> Self {
        Self { client, url }
    }

    pub fn rpc(
        &self,
        method: &str,
        params: &serde_json::value::RawValue,
    ) -> anyhow::Result<serde_json::Value> {
        let response_body = self
            .client
            .post(&self.url)
            .header("content-type", "application/json")
            .body(serde_json::to_string(&serde_json::json!({
                "jsonrpc": "2.0",
                "id": 0,
                "method": method,
                "params": params,
            }))?)
            .send()?
            .error_for_status()?
            .bytes()?;
        let result = serde_json::from_slice::<jsonrpc_core::Response>(&response_body[..])?;
        let result = match result {
            jsonrpc_core::Response::Single(o) => match o {
                jsonrpc_core::Output::Success(s) => s.result,
                jsonrpc_core::Output::Failure(f) => return Err(f.error.into()),
            },
            _ => anyhow::bail!("unexpected batch response"),
        };
        Ok(result)
    }
}