nodesty_api_library/services/
vps.rs1use crate::models::{
2 vps::{
3 VpsAction,
4 VpsBackup,
5 VpsChangePasswordData,
6 VpsDetails,
7 VpsGraphs,
8 VpsOsTemplate,
9 VpsReinstallData,
10 VpsTask,
11 },
12 ApiResponse,
13};
14use crate::NodestyApiClient;
15use reqwest::Method;
16use std::sync::Arc;
17
18pub struct VpsApiService {
19 client: Arc<NodestyApiClient>,
20}
21
22impl VpsApiService {
23 pub fn new(client: Arc<NodestyApiClient>) -> Self {
24 Self {
25 client,
26 }
27 }
28
29 pub async fn perform_action(
30 &self,
31 id: &str,
32 action: VpsAction,
33 ) -> Result<ApiResponse<()>, reqwest::Error> {
34 let body = serde_json::json!({ "action": action });
35 self.client.send_request(Method::POST, &format!("/services/{}/vps/action", id), Some(body)).await
36 }
37
38 pub async fn restore_backup(
39 &self,
40 id: &str,
41 data: &VpsBackup,
42 ) -> Result<ApiResponse<()>, reqwest::Error> {
43 self.client.send_request(Method::POST, &format!("/services/{}/vps/backups/{}/{}", id, data.date, data.file), None).await
44 }
45
46 pub async fn get_backups(
47 &self,
48 id: &str,
49 ) -> Result<ApiResponse<Vec<VpsBackup>>, reqwest::Error> {
50 self.client.send_request(Method::GET, &format!("/services/{}/vps/backups", id), None).await
51 }
52
53 pub async fn change_password(
54 &self,
55 id: &str,
56 data: VpsChangePasswordData,
57 ) -> Result<ApiResponse<()>, reqwest::Error> {
58 let body = serde_json::to_value(&data).ok();
59 self.client.send_request(Method::POST, &format!("/services/{}/vps/change-password", id), body).await
60 }
61
62 pub async fn get_usage_statistics(
63 &self,
64 id: &str,
65 ) -> Result<ApiResponse<VpsGraphs>, reqwest::Error> {
66 self.client.send_request(Method::GET, &format!("/services/{}/vps/graphs", id), None).await
67 }
68
69 pub async fn get_details(
70 &self,
71 id: &str,
72 ) -> Result<ApiResponse<VpsDetails>, reqwest::Error> {
73 self.client.send_request(Method::GET, &format!("/services/{}/vps/info", id), None).await
74 }
75
76 pub async fn get_os_templates(
77 &self,
78 id: &str,
79 ) -> Result<ApiResponse<Vec<VpsOsTemplate>>, reqwest::Error> {
80 self.client.send_request(Method::GET, &format!("/services/{}/vps/os-templates", id), None).await
81 }
82
83 pub async fn reinstall(
84 &self,
85 id: &str,
86 data: VpsReinstallData,
87 ) -> Result<ApiResponse<()>, reqwest::Error> {
88 let body = serde_json::to_value(&data).ok();
89 self.client.send_request(Method::POST, &format!("/services/{}/vps/reinstall", id), body).await
90 }
91
92 pub async fn get_tasks(
93 &self,
94 id: &str,
95 ) -> Result<ApiResponse<Vec<VpsTask>>, reqwest::Error> {
96 self.client.send_request(Method::GET, &format!("/services/{}/vps/tasks", id), None).await
97 }
98}