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 crate::config::Config;
use crate::mode::service::manager::{file_lister::FolderDump, State};
use reqwest::blocking::Response;
use thiserror::Error;

pub struct VitupRest {
    token: Option<String>,
    address: String,
}

impl VitupRest {
    pub fn is_up(&self) -> bool {
        let response = reqwest::blocking::get(self.path("api/health"));

        if response.is_err() {
            return false;
        }

        response.unwrap().status() == 200
    }
}

impl VitupRest {
    pub fn derive_with_port<S: Into<String>>(original: VitupRest, port: S) -> Self {
        Self {
            token: original.token().clone(),
            address: format!("{}:{}", original.address, port.into()),
        }
    }

    pub fn new_with_token(token: String, address: String) -> Self {
        Self {
            token: Some(token),
            address,
        }
    }

    pub fn new(address: String) -> Self {
        Self {
            token: None,
            address,
        }
    }

    pub fn token(&self) -> &Option<String> {
        &self.token
    }

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

    pub fn path<S: Into<String>>(&self, path: S) -> String {
        format!("{}/{}", self.address, path.into())
    }

    pub fn post_skip_response<S: Into<String>>(&self, local_path: S) -> Result<(), Error> {
        self.post(local_path).map(|_| ())
    }

    pub fn post<S: Into<String>>(&self, local_path: S) -> Result<Response, Error> {
        let path = self.path(local_path);
        println!("Calling: {}", path);
        let client = reqwest::blocking::Client::new();
        client.post(&path).send().map_err(Into::into)
    }

    pub fn get<S: Into<String>>(&self, local_path: S) -> Result<String, Error> {
        let path = self.path(local_path);
        println!("Calling: {}", path);
        Ok(reqwest::blocking::get(&path)?.text()?)
    }
}

pub struct VitupDisruptionRestClient {
    inner: VitupRest,
}

impl From<VitupRest> for VitupDisruptionRestClient {
    fn from(inner: VitupRest) -> Self {
        VitupDisruptionRestClient { inner }
    }
}

impl VitupDisruptionRestClient {
    pub fn get_logs(&self) -> Result<Vec<String>, Error> {
        serde_json::from_str(&self.inner.get("api/control/logs/get")?).map_err(Into::into)
    }

    pub fn clear_logs(&self) -> Result<(), Error> {
        self.inner.post_skip_response("api/control/logs/clear")
    }

    pub fn list_files(&self) -> Result<FolderDump, Error> {
        serde_json::from_str(&self.inner.get("api/control/files/list")?).map_err(Into::into)
    }

    pub fn reset(&self) -> Result<(), Error> {
        self.inner.post_skip_response("api/control/command/reset")
    }

    pub fn reset_with_config(&self, config: &Config) -> Result<Response, Error> {
        let client = reqwest::blocking::Client::new();
        client
            .post(self.inner.path("api/control/command/reset"))
            .json(&config)
            .send()
            .map_err(Into::into)
    }

    pub fn make_unavailable(&self) -> Result<(), Error> {
        self.inner.post_skip_response("api/control/available/false")
    }

    pub fn set_error_code(&self, error_code: u16) -> Result<(), Error> {
        self.inner
            .post_skip_response(format!("api/control/availabl/error-code/{}", error_code))
    }

    pub fn make_available(&self) -> Result<(), Error> {
        self.inner.post_skip_response("api/control/available/true")
    }

    pub fn set_fund_id(&self, fund_id: u32) -> Result<(), Error> {
        self.inner
            .post_skip_response(format!("api/control/command/fund/id/{}", fund_id))
    }

    pub fn reject_all_fragments(&self) -> Result<(), Error> {
        self.inner
            .post_skip_response("api/control/command/fragments/reject")
    }

    pub fn hold_all_fragments(&self) -> Result<(), Error> {
        self.inner
            .post_skip_response("api/control/command/fragments/pending")
    }

    pub fn accept_all_fragments(&self) -> Result<(), Error> {
        self.inner
            .post_skip_response("api/control/command/fragments/accept")
    }

    pub fn reset_fragments_behavior(&self) -> Result<(), Error> {
        self.inner
            .post_skip_response("api/control/command/fragments/reset")
    }

    pub fn is_up(&self) -> bool {
        if let Ok(path) = self.inner.get("api/health") {
            if let Ok(response) = reqwest::blocking::get(path) {
                return response.status() == reqwest::StatusCode::OK;
            }
        }
        false
    }
}

pub struct VitupAdminRestClient {
    inner: VitupRest,
}

impl From<VitupRest> for VitupAdminRestClient {
    fn from(inner: VitupRest) -> Self {
        let port = "3030";
        Self {
            inner: VitupRest::derive_with_port(inner, port),
        }
    }
}

impl VitupAdminRestClient {
    pub fn list_files(&self) -> Result<FolderDump, Error> {
        serde_json::from_str(&self.inner.get("api/files/list")?).map_err(Into::into)
    }

    pub fn start_custom(&self, params: Config) -> Result<String, Error> {
        let path = self.inner.path("control/command/start/custom");
        let client = reqwest::blocking::Client::new();
        let response = client.post(path).json(&params).send()?;
        Ok(response.text()?)
    }

    pub fn start_default(&self) -> Result<String, Error> {
        self.inner
            .post("control/command/start/default")?
            .text()
            .map_err(Into::into)
    }

    pub fn stop(&self) -> Result<String, Error> {
        self.inner
            .post("control/command/stop")?
            .text()
            .map_err(Into::into)
    }

    pub fn status(&self) -> Result<State, Error> {
        let text = self.inner.get("status")?;
        serde_json::from_str(&text).map_err(Into::into)
    }
}

#[derive(Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Reqwest(#[from] reqwest::Error),
    #[error(transparent)]
    Serde(#[from] serde_json::Error),
}