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
#![allow(unused)]

use reqwest::{
    header::{HeaderMap, HeaderName},
    Client, Method, Request, StatusCode,
};
use serde::de::DeserializeOwned;

use crate::{
    app::{backup::*, logs::*, manage::*, status::*, App, AppResponseAll, AppResponseUnique},
    config::Config,
    user::{Locale, LocaleResponse, User, UserResponse},
    util::{make_request, make_request_with_body},
};

use tracing::{debug, trace};

use super::error::Error;

#[derive(Clone)]
pub struct Discloud {
    config: Config,
}

impl Discloud {
    pub fn new(token: &str) -> Self {
        trace!("Creating new client");
        Self {
            config: Config::new(
                token,
                concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")),
            ),
        }
    }

    pub async fn get_user_info(&self) -> Result<User, Error> {
        let res: UserResponse = make_request(&self.config, Method::GET, "user").await?;

        Ok(res.user)
    }

    pub async fn set_locale(&self, locale: Locale) -> Result<(), Error> {
        let _: LocaleResponse =
            make_request(&self.config, Method::PUT, &format!("locale/{locale}")).await?;

        Ok(())
    }

    pub async fn get_app(&self, id: &str) -> Result<App, Error> {
        if id == "all" {
            return Err(Error::InvalidRequest(
                "Don't use all with that function. Use get_all_apps method instead.",
            ));
        }

        let res: AppResponseUnique =
            make_request(&self.config, Method::GET, &format!("app/{id}")).await?;

        Ok(res.apps)
    }

    pub async fn get_all_apps(&self) -> Result<Vec<App>, Error> {
        let res: AppResponseAll = make_request(&self.config, Method::GET, "app/all").await?;

        Ok(res.apps)
    }

    pub async fn get_all_apps_status(&self) -> Result<Vec<AppStatus>, Error> {
        let res: AppStatusResponseAll =
            make_request(&self.config, Method::GET, "app/all/status").await?;

        Ok(res.apps)
    }

    pub async fn get_app_status(&self, id: &str) -> Result<AppStatus, Error> {
        if id == "all" {
            return Err(Error::InvalidRequest(
                "Don't use all with that function. Use get_all_apps_status method instead.",
            ));
        }

        let res: AppStatusResponseUnique =
            make_request(&self.config, Method::GET, &format!("app/{id}/status")).await?;

        Ok(res.apps)
    }

    pub async fn get_app_logs(&self, id: &str) -> Result<AppLogs, Error> {
        if id == "all" {
            return Err(Error::InvalidRequest(
                "Don't use all with that function. Use get_all_apps_logs method instead.",
            ));
        }

        let res: AppLogsResponseUnique =
            make_request(&self.config, Method::GET, &format!("app/{id}/logs")).await?;

        Ok(res.apps)
    }

    pub async fn get_all_apps_logs(&self) -> Result<Vec<AppLogs>, Error> {
        let res: AppLogsResponseAll =
            make_request(&self.config, Method::GET, "app/all/logs").await?;

        Ok(res.apps)
    }

    pub async fn get_app_backup(&self, id: &str) -> Result<AppBackup, Error> {
        if id == "all" {
            return Err(Error::InvalidRequest(
                "Don't use all with that function. Use get_all_apps_backup method instead.",
            ));
        }

        let res: AppBackupResponseUnique =
            make_request(&self.config, Method::GET, &format!("app/{id}/backup")).await?;

        Ok(res.backups)
    }

    pub async fn get_all_apps_backup(&self) -> Result<Vec<AppBackup>, Error> {
        let res: AppBackupResponseAll =
            make_request(&self.config, Method::GET, "app/all/backup").await?;

        Ok(res.backups)
    }

    pub async fn start_app(&self, id: &str) -> Result<AppStartStatus, AppStartError> {
        if id == "all" {
            return Err(AppStartError::Other(Error::InvalidRequest(
                "Don't use all with that function. Use start_all_apps method instead.",
            )));
        }

        let res: AppStartResponseUnique =
            make_request(&self.config, Method::PUT, &format!("app/{id}/start")).await?;

        if res.status == "error" {
            return Err(AppStartError::AlreadyOnline);
        }

        res.app_status.ok_or(AppStartError::Other(Error::Unknown))
    }

    pub async fn start_all_apps(&self) -> Result<AppStartAll, Error> {
        let res: AppStartResponseAll =
            make_request(&self.config, Method::PUT, "app/all/start").await?;

        Ok(res.apps)
    }

    pub async fn stop_app(&self, id: &str) -> Result<(), AppStopError> {
        if id == "all" {
            return Err(AppStopError::Other(Error::InvalidRequest(
                "Don't use all with that function. Use stop_all_apps method instead.",
            )));
        }

        let res: AppStartResponseUnique =
            make_request(&self.config, Method::PUT, &format!("app/{id}/stop")).await?;

        if res.status == "error" {
            return Err(AppStopError::AlreadyOffline);
        }

        Ok(())
    }

    pub async fn stop_all_apps(&self) -> Result<AppStopAll, Error> {
        let res: AppStopResponseAll =
            make_request(&self.config, Method::PUT, "app/all/stop").await?;

        Ok(res.apps)
    }

    pub async fn restart_app(&self, id: &str) -> Result<(), Error> {
        if id == "all" {
            return Err(Error::InvalidRequest(
                "Don't use all with that function. Use restart_all_apps method instead.",
            ));
        }

        let res: AppRestartResponseUnique =
            make_request(&self.config, Method::PUT, &format!("app/{id}/restart")).await?;

        if res.status == "error" {
            return Err(Error::Unknown);
        }

        Ok(())
    }

    pub async fn restart_all_apps(&self) -> Result<AppRestartAll, Error> {
        let res: AppRestartResponseAll =
            make_request(&self.config, Method::PUT, "app/all/restart").await?;

        Ok(res.apps)
    }

    pub async fn set_app_ram(&self, id: &str, quantity: u32) -> Result<(), AppRamError> {
        let res: AppRamResponse = make_request_with_body(
            &self.config,
            Method::PUT,
            &format!("app/{id}/ram"),
            AppRamBody { ram: quantity },
        )
        .await?;

        if res.status == "error" {
            return Err(AppRamError::ForbiddenQuantity(res.message));
        }

        Ok(())
    }

    pub async fn commit_app(&self) {
        todo!()
    }

    pub async fn delete_app(&self, id: &str) -> Result<(), Error> {
        if id == "all" {
            return Err(Error::InvalidRequest(
                "Don't use all with that function. Use delete_all_apps method instead.",
            ));
        }

        let res: AppDeleteResponseUnique =
            make_request(&self.config, Method::DELETE, &format!("app/{id}/delete")).await?;

        if res.status == "error" {
            return Err(Error::Unknown);
        }

        Ok(())
    }

    pub async fn delete_all_apps(&self) -> Result<AppDeleteAll, Error> {
        let res: AppDeleteResponseAll =
            make_request(&self.config, Method::DELETE, "app/all/delete").await?;

        if res.status == "error" {
            return Err(Error::Unknown);
        }

        Ok(res.apps)
    }
}