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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use super::Error;
use super::{RestClientLogger, RestPathBuilder};
use reqwest::blocking::Response;
use std::time::Duration;
use url::Url;
use vit_servicing_station_lib::v0::api_token::API_TOKEN_HEADER;

#[derive(Debug, Clone)]
pub struct RestClient {
    path_builder: RestPathBuilder,
    api_token: Option<String>,
    logger: RestClientLogger,
    origin: Option<String>,
    timeout: Option<Duration>,
}

const ORIGIN: &str = "Origin";

impl RestClient {
    pub fn new(url: Url) -> Self {
        Self {
            api_token: None,
            path_builder: RestPathBuilder::new(url),
            logger: RestClientLogger::default(),
            origin: None,
            timeout: None,
        }
    }

    pub fn health(&self) -> Result<Response, Error> {
        self.get(&self.path_builder.health()).map_err(Into::into)
    }

    pub fn funds(&self) -> Result<Response, Error> {
        self.get(&self.path_builder.funds()).map_err(Into::into)
    }

    pub fn proposals(&self, group: &str) -> Result<Response, Error> {
        self.get(&self.path_builder.proposals_with_group(group))
            .map_err(Into::into)
    }

    pub fn put_snapshot_info(&self, tag: &str, content: String) -> Result<Response, Error> {
        self.put(
            &self.path_builder.clone().admin().snapshot_info(tag),
            content,
        )
        .map_err(Into::into)
    }

    pub fn put_raw_snapshot(&self, tag: &str, content: String) -> Result<Response, Error> {
        self.put(
            &self.path_builder.clone().admin().raw_snapshot(tag),
            content,
        )
        .map_err(Into::into)
    }

    pub fn snapshot_tags(&self) -> Result<Response, Error> {
        self.get(&self.path_builder.snapshot_tags())
            .map_err(Into::into)
    }

    pub fn voter_info(&self, tag: &str, key: &str) -> Result<Response, Error> {
        self.get(&self.path_builder.snapshot_voter_info(tag, key))
            .map_err(Into::into)
    }

    pub fn delegator_info(&self, tag: &str, key: &str) -> Result<Response, Error> {
        self.get(&self.path_builder.snapshot_delegator_info(tag, key))
            .map_err(Into::into)
    }

    pub fn proposal(&self, id: &str, group: &str) -> Result<Response, Error> {
        self.get(&self.path_builder().proposal(id, group))
            .map_err(Into::into)
    }

    pub fn proposals_by_voteplan_id_and_index(
        &self,
        request_as_string: &str,
    ) -> Result<Response, Error> {
        self.post(
            &self.path_builder().proposals(),
            request_as_string.to_string(),
        )
        .map_err(Into::into)
    }

    pub fn fund(&self, id: &str) -> Result<Response, Error> {
        self.get(&self.path_builder().fund(id)).map_err(Into::into)
    }

    pub fn challenges(&self) -> Result<Response, Error> {
        self.get(&self.path_builder().challenges())
            .map_err(Into::into)
    }

    pub fn genesis(&self) -> Result<Response, Error> {
        self.get(&self.path_builder.genesis()).map_err(Into::into)
    }

    pub fn service_version(&self) -> Result<Response, Error> {
        self.get(&self.path_builder.service_version())
            .map_err(Into::into)
    }

    pub fn advisor_reviews(&self, proposal_id: &str) -> Result<Response, Error> {
        self.get(&self.path_builder.advisor_reviews(proposal_id))
            .map_err(Into::into)
    }

    pub fn search(&self, query: impl Into<String>) -> Result<Response, Error> {
        self.post(&self.path_builder.search(), query.into())
            .map_err(Into::into)
    }

    pub fn search_count(&self, query: impl Into<String>) -> Result<Response, Error> {
        self.post(&self.path_builder.search_count(), query.into())
            .map_err(Into::into)
    }

    pub fn client(&self) -> Result<reqwest::blocking::Client, reqwest::Error> {
        reqwest::blocking::Client::builder()
            .danger_accept_invalid_certs(true)
            .build()
            .map_err(Into::into)
    }

    pub fn set_api_token(&mut self, token: String) {
        self.api_token = Some(token);
    }

    pub fn set_origin<S: Into<String>>(&mut self, origin: S) {
        self.origin = Some(origin.into());
    }

    pub fn set_timeout(&mut self, timeout: Duration) {
        self.timeout = Some(timeout);
    }

    pub fn disable_log(&mut self) {
        self.logger.set_enabled(false);
    }

    pub fn enable_log(&mut self) {
        self.logger.set_enabled(true);
    }

    pub fn log_response(&self, response: &Response) {
        self.logger.log_response(response);
    }

    pub fn log_text(&self, content: &str) {
        self.logger.log_text(content);
    }

    pub fn path_builder(&self) -> &RestPathBuilder {
        &self.path_builder
    }

    fn post(
        &self,
        path: &str,
        data: String,
    ) -> Result<reqwest::blocking::Response, reqwest::Error> {
        self.logger.log_post_body(&data);

        let mut res = self.client()?.post(path).body(String::into_bytes(data));

        if let Some(api_token) = &self.api_token {
            res = res.header(API_TOKEN_HEADER, api_token.to_string());
        }
        let response = res.send()?;
        Ok(response)
    }

    fn get(&self, path: &str) -> Result<reqwest::blocking::Response, reqwest::Error> {
        self.logger.log_request(path);
        let mut res = self.client()?.get(path);

        if let Some(api_token) = &self.api_token {
            res = res.header(API_TOKEN_HEADER, api_token.to_string());
        }
        if let Some(origin) = &self.origin {
            res = res.header(ORIGIN, origin.to_string());
        }
        let response = res.send()?;
        self.logger.log_response(&response);
        Ok(response)
    }

    fn put(&self, path: &str, body: String) -> Result<reqwest::blocking::Response, reqwest::Error> {
        self.logger.log_request(path);
        let mut res = self.client()?.put(path).body(body);

        if let Some(api_token) = &self.api_token {
            res = res.header(API_TOKEN_HEADER, api_token.to_string());
        }
        if let Some(origin) = &self.origin {
            res = res.header(ORIGIN, origin.to_string());
        }
        if let Some(timeout) = self.timeout {
            res = res.timeout(timeout);
        }
        let response = res.send()?;
        self.logger.log_response(&response);
        Ok(response)
    }
}