1#![allow(unused)]
2
3use reqwest::{
4 header::{HeaderMap, HeaderName},
5 Client, Method, Request, StatusCode,
6};
7use serde::de::DeserializeOwned;
8
9use crate::{
10 app::{backup::*, logs::*, manage::*, status::*, App, AppResponseAll, AppResponseUnique},
11 config::Config,
12 team_manager::{
13 APITeamMember, AddTeamMemberResponse, GetTeamManagerResponse, TeamMember, TeamMemberBody,
14 TeamPerms,
15 },
16 user::{Locale, LocaleResponse, User, UserResponse},
17 util::{make_request, make_request_with_body, DiscloudDefaultResponse},
18};
19
20use tracing::{debug, trace};
21
22use super::error::Error;
23
24#[derive(Clone)]
25pub struct Discloud {
26 config: Config,
27}
28
29impl Discloud {
30 pub fn new(token: &str) -> Self {
31 trace!("Creating new client");
32 Self {
33 config: Config::new(
34 token,
35 concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")),
36 ),
37 }
38 }
39
40 pub async fn remove_app_mod(&self, app_id: &str, mod_id: &str) -> Result<(), Error> {
41 let _res: DiscloudDefaultResponse = make_request(
42 &self.config,
43 Method::DELETE,
44 &format!("app/{app_id}/team/{mod_id}"),
45 )
46 .await?;
47
48 Ok(())
49 }
50
51 pub async fn edit_app_mod(
52 &self,
53 app_id: &str,
54 mod_id: &str,
55 perms: Vec<TeamPerms>,
56 ) -> Result<TeamMember, Error> {
57 let res: AddTeamMemberResponse = make_request_with_body(
58 &self.config,
59 Method::PUT,
60 &format!("app/{app_id}/team"),
61 TeamMemberBody {
62 mod_id: mod_id.to_string(),
63 perms,
64 },
65 )
66 .await?;
67 Ok(res.app)
68 }
69
70 pub async fn add_app_mod(
71 &self,
72 app_id: &str,
73 mod_id: &str,
74 perms: Vec<TeamPerms>,
75 ) -> Result<TeamMember, Error> {
76 let res: AddTeamMemberResponse = make_request_with_body(
77 &self.config,
78 Method::POST,
79 &format!("app/{app_id}/team"),
80 TeamMemberBody {
81 mod_id: mod_id.to_string(),
82 perms,
83 },
84 )
85 .await?;
86
87 Ok(res.app)
88 }
89
90 pub async fn get_app_team(&self, id: &str) -> Result<Vec<TeamMember>, Error> {
91 let res: GetTeamManagerResponse =
92 make_request(&self.config, Method::GET, &format!("app/{id}/team")).await?;
93
94 let team_members = res
95 .team
96 .iter()
97 .map(|v| TeamMember {
98 mod_id: v.id.clone(),
99 app_id: id.to_owned(),
100 perms: v.perms.clone(),
101 })
102 .collect::<Vec<_>>();
103
104 Ok(team_members)
105 }
106
107 pub async fn get_user_info(&self) -> Result<User, Error> {
108 let res: UserResponse = make_request(&self.config, Method::GET, "user").await?;
109
110 Ok(res.user)
111 }
112
113 pub async fn set_locale(&self, locale: Locale) -> Result<(), Error> {
114 let _: LocaleResponse =
115 make_request(&self.config, Method::PUT, &format!("locale/{locale}")).await?;
116
117 Ok(())
118 }
119
120 pub async fn get_app(&self, id: &str) -> Result<App, Error> {
121 if id == "all" {
122 return Err(Error::InvalidRequest(
123 "Don't use all with that function. Use get_all_apps method instead.",
124 ));
125 }
126
127 let res: AppResponseUnique =
128 make_request(&self.config, Method::GET, &format!("app/{id}")).await?;
129
130 Ok(res.apps)
131 }
132
133 pub async fn get_all_apps(&self) -> Result<Vec<App>, Error> {
134 let res: AppResponseAll = make_request(&self.config, Method::GET, "app/all").await?;
135
136 Ok(res.apps)
137 }
138
139 pub async fn get_all_apps_status(&self) -> Result<Vec<AppStatus>, Error> {
140 let res: AppStatusResponseAll =
141 make_request(&self.config, Method::GET, "app/all/status").await?;
142
143 Ok(res.apps)
144 }
145
146 pub async fn get_app_status(&self, id: &str) -> Result<AppStatus, Error> {
147 if id == "all" {
148 return Err(Error::InvalidRequest(
149 "Don't use all with that function. Use get_all_apps_status method instead.",
150 ));
151 }
152
153 let res: AppStatusResponseUnique =
154 make_request(&self.config, Method::GET, &format!("app/{id}/status")).await?;
155
156 Ok(res.apps)
157 }
158
159 pub async fn get_app_logs(&self, id: &str) -> Result<AppLogs, Error> {
160 if id == "all" {
161 return Err(Error::InvalidRequest(
162 "Don't use all with that function. Use get_all_apps_logs method instead.",
163 ));
164 }
165
166 let res: AppLogsResponseUnique =
167 make_request(&self.config, Method::GET, &format!("app/{id}/logs")).await?;
168
169 Ok(res.apps)
170 }
171
172 pub async fn get_all_apps_logs(&self) -> Result<Vec<AppLogs>, Error> {
173 let res: AppLogsResponseAll =
174 make_request(&self.config, Method::GET, "app/all/logs").await?;
175
176 Ok(res.apps)
177 }
178
179 pub async fn get_app_backup(&self, id: &str) -> Result<AppBackup, Error> {
180 if id == "all" {
181 return Err(Error::InvalidRequest(
182 "Don't use all with that function. Use get_all_apps_backup method instead.",
183 ));
184 }
185
186 let res: AppBackupResponseUnique =
187 make_request(&self.config, Method::GET, &format!("app/{id}/backup")).await?;
188
189 Ok(res.backups)
190 }
191
192 pub async fn get_all_apps_backup(&self) -> Result<Vec<AppBackup>, Error> {
193 let res: AppBackupResponseAll =
194 make_request(&self.config, Method::GET, "app/all/backup").await?;
195
196 Ok(res.backups)
197 }
198
199 pub async fn start_app(&self, id: &str) -> Result<AppStartStatus, AppStartError> {
200 if id == "all" {
201 return Err(AppStartError::Other(Error::InvalidRequest(
202 "Don't use all with that function. Use start_all_apps method instead.",
203 )));
204 }
205
206 let res: AppStartResponseUnique =
207 make_request(&self.config, Method::PUT, &format!("app/{id}/start")).await?;
208
209 if res.status == "error" {
210 return Err(AppStartError::AlreadyOnline);
211 }
212
213 res.app_status.ok_or(AppStartError::Other(Error::Unknown))
214 }
215
216 pub async fn start_all_apps(&self) -> Result<AppStartAll, Error> {
217 let res: AppStartResponseAll =
218 make_request(&self.config, Method::PUT, "app/all/start").await?;
219
220 Ok(res.apps)
221 }
222
223 pub async fn stop_app(&self, id: &str) -> Result<(), AppStopError> {
224 if id == "all" {
225 return Err(AppStopError::Other(Error::InvalidRequest(
226 "Don't use all with that function. Use stop_all_apps method instead.",
227 )));
228 }
229
230 let res: AppStartResponseUnique =
231 make_request(&self.config, Method::PUT, &format!("app/{id}/stop")).await?;
232
233 if res.status == "error" {
234 return Err(AppStopError::AlreadyOffline);
235 }
236
237 Ok(())
238 }
239
240 pub async fn stop_all_apps(&self) -> Result<AppStopAll, Error> {
241 let res: AppStopResponseAll =
242 make_request(&self.config, Method::PUT, "app/all/stop").await?;
243
244 Ok(res.apps)
245 }
246
247 pub async fn restart_app(&self, id: &str) -> Result<(), Error> {
248 if id == "all" {
249 return Err(Error::InvalidRequest(
250 "Don't use all with that function. Use restart_all_apps method instead.",
251 ));
252 }
253
254 let res: AppRestartResponseUnique =
255 make_request(&self.config, Method::PUT, &format!("app/{id}/restart")).await?;
256
257 if res.status == "error" {
258 return Err(Error::Unknown);
259 }
260
261 Ok(())
262 }
263
264 pub async fn restart_all_apps(&self) -> Result<AppRestartAll, Error> {
265 let res: AppRestartResponseAll =
266 make_request(&self.config, Method::PUT, "app/all/restart").await?;
267
268 Ok(res.apps)
269 }
270
271 pub async fn set_app_ram(&self, id: &str, quantity: u32) -> Result<(), AppRamError> {
272 let res: AppRamResponse = make_request_with_body(
273 &self.config,
274 Method::PUT,
275 &format!("app/{id}/ram"),
276 AppRamBody { ram: quantity },
277 )
278 .await?;
279
280 if res.status == "error" {
281 return Err(AppRamError::ForbiddenQuantity(res.message));
282 }
283
284 Ok(())
285 }
286
287 pub async fn commit_app(&self) {
288 todo!()
289 }
290
291 pub async fn delete_app(&self, id: &str) -> Result<(), Error> {
292 if id == "all" {
293 return Err(Error::InvalidRequest(
294 "Don't use all with that function. Use delete_all_apps method instead.",
295 ));
296 }
297
298 let res: AppDeleteResponseUnique =
299 make_request(&self.config, Method::DELETE, &format!("app/{id}/delete")).await?;
300
301 if res.status == "error" {
302 return Err(Error::Unknown);
303 }
304
305 Ok(())
306 }
307
308 pub async fn delete_all_apps(&self) -> Result<AppDeleteAll, Error> {
309 let res: AppDeleteResponseAll =
310 make_request(&self.config, Method::DELETE, "app/all/delete").await?;
311
312 if res.status == "error" {
313 return Err(Error::Unknown);
314 }
315
316 Ok(res.apps)
317 }
318}