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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use super::RestSettings;
use crate::jormungandr::RestError;
use bech32::FromBase32;
use chain_core::property::Serialize;
use chain_crypto::PublicKey;
use chain_impl_mockchain::{account, fragment::Fragment, header::HeaderId};
use jormungandr_lib::{
    crypto::account::Identifier,
    interfaces::{Address, FragmentsBatch, VotePlanId},
};
use jortestkit::process::Wait;
use reqwest::{
    blocking::{Client, Response},
    header::{HeaderMap, HeaderValue, CONTENT_TYPE},
};
use std::fmt;

const ORIGIN: &str = "Origin";
enum ApiVersion {
    V0,
    V1,
}

impl fmt::Display for ApiVersion {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ApiVersion::V0 => write!(f, "v0"),
            ApiVersion::V1 => write!(f, "v1"),
        }
    }
}

/// struct intends to return raw reqwest response
/// can be used to verify requests error codes or
/// to poll until data is available
#[derive(Debug, Clone)]
pub struct RawRest {
    uri: String,
    client: Client,
    settings: RestSettings,
}

impl RawRest {
    pub fn new(uri: String, settings: RestSettings) -> Self {
        let client = match &settings.certificate {
            None => Client::new(),
            Some(cert) => Client::builder()
                .use_rustls_tls()
                .add_root_certificate(cert.clone())
                .build()
                .unwrap(),
        };
        let uri = if settings.use_https {
            let url = url::Url::parse(&uri).unwrap();
            format!(
                "https://{}:443/{}",
                url.domain().unwrap(),
                url.path_segments().unwrap().next().unwrap()
            )
        } else {
            uri
        };

        Self {
            uri,
            client,
            settings,
        }
    }

    pub fn uri(&self) -> String {
        self.uri.clone()
    }

    pub fn rest_settings(&self) -> &RestSettings {
        &self.settings
    }

    pub fn rest_settings_mut(&mut self) -> &mut RestSettings {
        &mut self.settings
    }

    pub fn enable_logger(&mut self) {
        self.rest_settings_mut().enable_debug = true;
    }

    pub fn disable_logger(&mut self) {
        self.rest_settings_mut().enable_debug = false;
    }

    pub fn epoch_reward_history(&self, epoch: u32) -> Result<Response, reqwest::Error> {
        let request = format!("rewards/epoch/{}", epoch);
        self.get(&request)
    }

    pub(crate) fn updates(&self) -> Result<Response, reqwest::Error> {
        self.get("updates/active")
    }

    pub fn reward_history(&self, length: u32) -> Result<Response, reqwest::Error> {
        let request = format!("rewards/history/{}", length);
        self.get(&request)
    }

    pub fn remaining_rewards(&self) -> Result<Response, reqwest::Error> {
        self.get("rewards/remaining")
    }

    fn print_request_path(&self, text: &str) {
        if self.rest_settings().enable_debug {
            println!("Request: {}", text);
        }
    }

    fn get(&self, path: &str) -> Result<reqwest::blocking::Response, reqwest::Error> {
        let request = self.path(ApiVersion::V0, path);
        self.print_request_path(&request);

        let mut client_builder = reqwest::blocking::Client::builder();

        if let Some(cert) = self.rest_settings().certificate.as_ref() {
            client_builder = client_builder
                .use_rustls_tls()
                .add_root_certificate(cert.clone())
        }
        let client = client_builder.build()?;
        let mut res = client.get(&request);

        if let Some(origin) = self.rest_settings().cors.as_ref() {
            res = res.header(ORIGIN, origin.to_string());
        }

        res.send()
    }

    fn path(&self, api_version: ApiVersion, path: &str) -> String {
        format!("{}/{}/{}", self.uri, api_version, path)
    }

    pub fn stake_distribution(&self) -> Result<Response, reqwest::Error> {
        self.get("stake")
    }

    pub fn account_state(&self, id: &Identifier) -> Result<Response, reqwest::Error> {
        self.account_state_by_pk(&id.to_bech32_str())
    }

    pub fn account_state_by_pk(&self, bech32_str: &str) -> Result<Response, reqwest::Error> {
        let key = hex::encode(Self::try_from_str(bech32_str).as_ref());
        self.get(&format!("account/{}", key))
    }

    fn try_from_str(src: &str) -> account::Identifier {
        let (_, data, _variant) = bech32::decode(src).unwrap();
        let dat = Vec::from_base32(&data).unwrap();
        let pk = PublicKey::from_binary(&dat).unwrap();
        account::Identifier::from(pk)
    }

    pub fn stake_pools(&self) -> Result<Response, reqwest::Error> {
        self.get("stake_pools")
    }

    pub fn account_votes(&self, address: Address) -> Result<Response, reqwest::Error> {
        let pk = address.1.public_key().unwrap();
        let key = hex::encode(account::Identifier::from(pk.clone()).as_ref());

        let request = format!("votes/plan/account-votes/{}", key);
        self.client.get(self.path(ApiVersion::V1, &request)).send()
    }

    pub fn account_votes_with_plan_id(
        &self,
        vote_plan_id: VotePlanId,
        address: Address,
    ) -> Result<Response, reqwest::Error> {
        let pk = address.1.public_key().unwrap();
        let key = hex::encode(account::Identifier::from(pk.clone()).as_ref());

        let request = format!("votes/plan/{}/account-votes/{}", vote_plan_id, key);
        self.client.get(self.path(ApiVersion::V1, &request)).send()
    }

    pub fn stake_distribution_at(&self, epoch: u32) -> Result<Response, reqwest::Error> {
        let request = format!("stake/{}", epoch);
        self.get(&request)
    }

    pub fn account_votes_all(&self) -> Result<Response, reqwest::Error> {
        self.client
            .get(self.path(ApiVersion::V1, "votes/plan/accounts-votes-all"))
            .send()
    }

    pub fn stats(&self) -> Result<Response, reqwest::Error> {
        self.get("node/stats")
    }

    pub fn network_stats(&self) -> Result<Response, reqwest::Error> {
        self.get("network/stats")
    }

    pub fn p2p_quarantined(&self) -> Result<Response, reqwest::Error> {
        self.get("network/p2p/quarantined")
    }

    pub fn p2p_non_public(&self) -> Result<Response, reqwest::Error> {
        self.get("network/p2p/non_public")
    }

    pub fn p2p_available(&self) -> Result<Response, reqwest::Error> {
        self.get("network/p2p/available")
    }

    pub fn p2p_view(&self) -> Result<Response, reqwest::Error> {
        self.get("network/p2p/view")
    }

    pub fn leaders_log(&self) -> Result<Response, reqwest::Error> {
        self.get("leaders/logs")
    }

    pub fn tip(&self) -> Result<Response, reqwest::Error> {
        self.get("tip")
    }

    pub fn settings(&self) -> Result<Response, reqwest::Error> {
        self.get("settings")
    }

    pub fn shutdown(&self) -> Result<Response, reqwest::Error> {
        self.get("shutdown")
    }

    pub fn block(&self, header_hash: &HeaderId) -> Result<Response, reqwest::Error> {
        self.get(&format!("block/{}", header_hash))
    }

    pub fn fragment_logs(&self) -> Result<Response, reqwest::Error> {
        self.get("fragment/logs")
    }

    fn construct_headers(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert(
            CONTENT_TYPE,
            HeaderValue::from_static("application/octet-stream"),
        );
        headers
    }

    fn construct_headers_json(&self) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
        headers
    }

    fn post(
        &self,
        path: &str,
        body: Vec<u8>,
    ) -> Result<reqwest::blocking::Response, reqwest::Error> {
        self.client
            .post(self.path(ApiVersion::V0, path))
            .headers(self.construct_headers())
            .body(body)
            .send()
    }

    pub fn send_fragment(&self, fragment: Fragment) -> Result<Response, reqwest::Error> {
        let raw = fragment.serialize_as_vec().unwrap();
        self.send_raw_fragment(raw)
    }

    pub fn send_raw_fragment(
        &self,
        body: Vec<u8>,
    ) -> Result<reqwest::blocking::Response, reqwest::Error> {
        self.post("message", body)
    }

    pub fn send_raw_fragments(&self, bodies: Vec<Vec<u8>>) -> Result<(), reqwest::Error> {
        let clients: Vec<reqwest::blocking::RequestBuilder> = bodies
            .into_iter()
            .map(|body| {
                self.client
                    .post(self.path(ApiVersion::V0, "message"))
                    .headers(self.construct_headers())
                    .body(body)
            })
            .collect();

        for client in clients {
            client.send()?;
        }
        Ok(())
    }

    pub fn fragments_logs(&self) -> Result<Response, reqwest::Error> {
        self.client
            .get(self.path(ApiVersion::V1, "fragments/logs"))
            .send()
    }

    pub fn fragments_statuses(&self, ids: Vec<String>) -> Result<Response, reqwest::Error> {
        self.client
            .get(self.path(ApiVersion::V1, "fragments/statuses"))
            .query(&[("fragment_ids", ids.join(","))])
            .send()
    }

    pub fn send_fragment_batch(
        &self,
        fragments: Vec<Fragment>,
        fail_fast: bool,
    ) -> Result<Response, reqwest::Error> {
        self.client
            .post(self.path(ApiVersion::V1, "fragments"))
            .headers(self.construct_headers_json())
            .json(&FragmentsBatch {
                fail_fast,
                fragments,
            })
            .send()
    }

    pub fn vote_plan_statuses(&self) -> Result<Response, reqwest::Error> {
        self.get("vote/active/plans")
    }

    pub fn send_until_ok<F>(&self, action: F, mut wait: Wait) -> Result<(), RestError>
    where
        F: Fn(&RawRest) -> Result<Response, reqwest::Error>,
    {
        loop {
            let response = action(self);
            println!("Waiting for 200... {:?}", response);
            if let Ok(response) = response {
                if response.status().is_success() {
                    return Ok(());
                }
            }
            wait.check_timeout()?;
            wait.advance();
        }
    }
}