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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::collections::HashMap;
use std::time::Duration;

use serde::Deserialize;
use serde::Serialize;

/// Timeout in milliseconds.
#[derive(Deserialize, Debug, Serialize, Clone)]
pub struct Timeout(u64);

impl Default for Timeout {
    fn default() -> Self {
        Timeout(30 * 1000)
    }
}

impl From<Timeout> for Duration {
    fn from(val: Timeout) -> Self {
        Duration::from_millis(val.0)
    }
}

impl From<u64> for Timeout {
    fn from(v: u64) -> Self {
        Self(v)
    }
}

fn default_http_request_body() -> Option<Vec<u8>> {
    None
}

/// HttpRequest
/// - `method`: request methods
///    * GET
///    * POST
///    * PUT
///    * DELETE
///    * OPTION
///    * HEAD
///    * TRACE
///    * CONNECT
/// - `path`: hidden service path
/// - `timeout`: timeout in milliseconds
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct HttpRequest {
    /// service name
    pub name: String,
    /// method
    pub method: String,
    /// url
    pub path: String,
    /// timeout
    #[serde(default)]
    pub timeout: Timeout,
    /// headers
    pub headers: HashMap<String, String>,
    /// body
    #[serde(default = "default_http_request_body")]
    pub body: Option<Vec<u8>>,
}

impl From<(String, http::Method, String, Timeout)> for HttpRequest {
    fn from((name, method, path, timeout): (String, http::Method, String, Timeout)) -> Self {
        Self {
            name,
            method: method.to_string(),
            path,
            timeout,
            headers: HashMap::new(),
            body: None,
        }
    }
}

impl From<(&str, http::Method, &str, u64)> for HttpRequest {
    fn from((name, method, url, timeout): (&str, http::Method, &str, u64)) -> Self {
        (
            name.to_owned(),
            method,
            url.to_owned(),
            Timeout::from(timeout),
        )
            .into()
    }
}

impl HttpRequest {
    /// new HttpRequest
    /// - `name`
    /// - `method`
    /// - `url`
    /// - `timeout`
    /// - `headers`
    /// - `body`: optional
    pub fn new<U>(
        name: U,
        method: http::Method,
        path: U,
        timeout: Timeout,
        headers: &[(U, U)],
        body: Option<Vec<u8>>,
    ) -> Self
    where
        U: ToString,
    {
        Self {
            name: name.to_string(),
            method: method.to_string(),
            path: path.to_string(),
            timeout,
            headers: headers
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect(),
            body,
        }
    }

    /// new `GET` HttpRequest
    /// - `name`
    /// - `method`
    /// - `url`
    /// - `timeout`
    /// - `headers`
    /// - `body`: optional
    pub fn get<U>(
        name: U,
        url: U,
        timeout: Timeout,
        headers: &[(U, U)],
        body: Option<Vec<u8>>,
    ) -> Self
    where
        U: ToString,
    {
        Self::new(name, http::Method::GET, url, timeout, headers, body)
    }
}