1use super::ApiPaths;
5use anyhow::Context as _;
6use enumflags2::BitFlags;
7use jsonrpsee::core::traits::ToRpcParams;
8use serde::{Deserialize, Serialize};
9use std::{marker::PhantomData, time::Duration};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct Request<T = serde_json::Value> {
16 pub method_name: std::borrow::Cow<'static, str>,
17 pub params: serde_json::Value,
18 #[serde(skip)]
19 pub result_type: PhantomData<T>,
20 #[serde(skip)]
21 pub api_path: ApiPaths,
22 #[serde(skip)]
23 pub timeout: Duration,
24}
25
26impl<T> Request<T> {
27 pub fn new(method_name: &'static str, params: serde_json::Value, api_path: ApiPaths) -> Self {
29 Self {
30 method_name: method_name.into(),
31 params,
32 result_type: PhantomData,
33 api_path,
34 timeout: *super::DEFAULT_REQUEST_TIMEOUT,
35 }
36 }
37
38 pub fn set_timeout(&mut self, timeout: Duration) {
39 self.timeout = timeout;
40 }
41
42 pub fn with_timeout(mut self, timeout: Duration) -> Self {
43 self.set_timeout(timeout);
44 self
45 }
46
47 pub fn set_api_path(&mut self, api_path: ApiPaths) {
48 self.api_path = api_path;
49 }
50
51 pub fn with_api_path(mut self, api_path: ApiPaths) -> Self {
52 self.set_api_path(api_path);
53 self
54 }
55
56 pub fn map_ty<U>(self) -> Request<U> {
58 Request {
59 method_name: self.method_name,
60 params: self.params,
61 result_type: PhantomData,
62 api_path: self.api_path,
63 timeout: self.timeout,
64 }
65 }
66
67 pub fn max_api_path(api_paths: BitFlags<ApiPaths>) -> anyhow::Result<ApiPaths> {
68 api_paths.iter().max().context("No supported versions")
69 }
70}
71
72impl<T> ToRpcParams for Request<T> {
73 fn to_rpc_params(self) -> Result<Option<Box<serde_json::value::RawValue>>, serde_json::Error> {
74 Ok(Some(serde_json::value::to_raw_value(&self.params)?))
75 }
76}