Skip to main content

rings_rpc/
types.rs

1use std::collections::HashMap;
2use std::time::Duration;
3
4use serde::Deserialize;
5use serde::Serialize;
6
7/// Timeout in milliseconds.
8#[derive(Deserialize, Debug, Serialize, Clone)]
9pub struct Timeout(u64);
10
11impl Default for Timeout {
12    fn default() -> Self {
13        Timeout(30 * 1000)
14    }
15}
16
17impl From<Timeout> for Duration {
18    fn from(val: Timeout) -> Self {
19        Duration::from_millis(val.0)
20    }
21}
22
23impl From<u64> for Timeout {
24    fn from(v: u64) -> Self {
25        Self(v)
26    }
27}
28
29fn default_http_request_body() -> Option<Vec<u8>> {
30    None
31}
32
33/// HttpRequest
34/// - `method`: request methods
35///    * GET
36///    * POST
37///    * PUT
38///    * DELETE
39///    * OPTION
40///    * HEAD
41///    * TRACE
42///    * CONNECT
43/// - `path`: hidden service path
44/// - `timeout`: timeout in milliseconds
45#[derive(Debug, Clone, Deserialize, Serialize)]
46pub struct HttpRequest {
47    /// service name
48    pub name: String,
49    /// method
50    pub method: String,
51    /// url
52    pub path: String,
53    /// timeout
54    #[serde(default)]
55    pub timeout: Timeout,
56    /// headers
57    pub headers: HashMap<String, String>,
58    /// body
59    #[serde(default = "default_http_request_body")]
60    pub body: Option<Vec<u8>>,
61}
62
63impl From<(String, http::Method, String, Timeout)> for HttpRequest {
64    fn from((name, method, path, timeout): (String, http::Method, String, Timeout)) -> Self {
65        Self {
66            name,
67            method: method.to_string(),
68            path,
69            timeout,
70            headers: HashMap::new(),
71            body: None,
72        }
73    }
74}
75
76impl From<(&str, http::Method, &str, u64)> for HttpRequest {
77    fn from((name, method, url, timeout): (&str, http::Method, &str, u64)) -> Self {
78        (
79            name.to_owned(),
80            method,
81            url.to_owned(),
82            Timeout::from(timeout),
83        )
84            .into()
85    }
86}
87
88impl HttpRequest {
89    /// new HttpRequest
90    /// - `name`
91    /// - `method`
92    /// - `url`
93    /// - `timeout`
94    /// - `headers`
95    /// - `body`: optional
96    pub fn new<U>(
97        name: U,
98        method: http::Method,
99        path: U,
100        timeout: Timeout,
101        headers: &[(U, U)],
102        body: Option<Vec<u8>>,
103    ) -> Self
104    where
105        U: ToString,
106    {
107        Self {
108            name: name.to_string(),
109            method: method.to_string(),
110            path: path.to_string(),
111            timeout,
112            headers: headers
113                .iter()
114                .map(|(k, v)| (k.to_string(), v.to_string()))
115                .collect(),
116            body,
117        }
118    }
119
120    /// new `GET` HttpRequest
121    /// - `name`
122    /// - `method`
123    /// - `url`
124    /// - `timeout`
125    /// - `headers`
126    /// - `body`: optional
127    pub fn get<U>(
128        name: U,
129        url: U,
130        timeout: Timeout,
131        headers: &[(U, U)],
132        body: Option<Vec<u8>>,
133    ) -> Self
134    where
135        U: ToString,
136    {
137        Self::new(name, http::Method::GET, url, timeout, headers, body)
138    }
139}