cloudreve_api/api/v3/
mod.rs

1//! API v3 implementation
2
3use crate::Error;
4use log::debug;
5use serde::Serialize;
6
7pub mod aria2;
8pub mod directory;
9pub mod file;
10pub mod models;
11pub mod object;
12pub mod session;
13pub mod share;
14pub mod site;
15pub mod user;
16
17/// API v3 client structure
18#[derive(Debug, Clone)]
19pub struct ApiV3Client {
20    pub base_url: String,
21    pub http_client: reqwest::Client,
22    pub session_cookie: Option<String>,
23}
24
25impl ApiV3Client {
26    pub fn new(base_url: &str) -> Self {
27        Self {
28            base_url: base_url.to_string(),
29            http_client: reqwest::Client::new(),
30            session_cookie: None,
31        }
32    }
33
34    pub fn set_session_cookie(&mut self, cookie: String) {
35        self.session_cookie = Some(cookie);
36    }
37
38    pub fn get_url(&self, endpoint: &str) -> String {
39        format!(
40            "{}/api/v3/{}",
41            self.base_url.trim_end_matches('/'),
42            endpoint.trim_start_matches('/')
43        )
44    }
45
46    pub async fn get<T>(&self, endpoint: &str) -> Result<T, Error>
47    where
48        T: serde::de::DeserializeOwned + std::fmt::Debug,
49    {
50        let url = self.get_url(endpoint);
51        let mut request = self.http_client.get(&url);
52
53        if let Some(cookie) = &self.session_cookie {
54            request = request.header("Cookie", format!("cloudreve-session={}", cookie));
55        }
56
57        debug!("GET URL: {}", url);
58
59        let response = request.send().await?;
60        let status = response.status();
61        let json: T = response.json().await?;
62        debug!("Response status: {}, JSON: {:?}", status, json);
63        Ok(json)
64    }
65
66    pub async fn post<T>(&self, endpoint: &str, body: &impl Serialize) -> Result<T, Error>
67    where
68        T: serde::de::DeserializeOwned + std::fmt::Debug,
69    {
70        let url = self.get_url(endpoint);
71        let mut request = self.http_client.post(&url).json(body);
72
73        if let Some(cookie) = &self.session_cookie {
74            request = request.header("Cookie", format!("cloudreve-session={}", cookie));
75        }
76
77        debug!("POST URL: {}", url);
78
79        let response = request.send().await?;
80        let status = response.status();
81        let json: T = response.json().await?;
82        debug!("Response status: {}, JSON: {:?}", status, json);
83        Ok(json)
84    }
85
86    pub async fn put<T>(&self, endpoint: &str, body: &impl Serialize) -> Result<T, Error>
87    where
88        T: serde::de::DeserializeOwned + std::fmt::Debug,
89    {
90        let url = self.get_url(endpoint);
91        let mut request = self.http_client.put(&url).json(body);
92
93        if let Some(cookie) = &self.session_cookie {
94            request = request.header("Cookie", format!("cloudreve-session={}", cookie));
95        }
96
97        debug!("PUT URL: {}", url);
98
99        let response = request.send().await?;
100        let status = response.status();
101        let json: T = response.json().await?;
102        debug!("Response status: {}, JSON: {:?}", status, json);
103        Ok(json)
104    }
105
106    pub async fn patch<T>(&self, endpoint: &str, body: &impl Serialize) -> Result<T, Error>
107    where
108        T: serde::de::DeserializeOwned + std::fmt::Debug,
109    {
110        let url = self.get_url(endpoint);
111        let mut request = self.http_client.patch(&url).json(body);
112
113        if let Some(cookie) = &self.session_cookie {
114            request = request.header("Cookie", format!("cloudreve-session={}", cookie));
115        }
116
117        debug!("PATCH URL: {}", url);
118
119        let response = request.send().await?;
120        let status = response.status();
121        let json: T = response.json().await?;
122        debug!("Response status: {}, JSON: {:?}", status, json);
123        Ok(json)
124    }
125
126    pub async fn delete<T>(&self, endpoint: &str) -> Result<T, Error>
127    where
128        T: serde::de::DeserializeOwned + std::fmt::Debug,
129    {
130        let url = self.get_url(endpoint);
131        let mut request = self.http_client.delete(&url);
132
133        if let Some(cookie) = &self.session_cookie {
134            request = request.header("Cookie", format!("cloudreve-session={}", cookie));
135        }
136
137        debug!("DELETE URL: {}", url);
138
139        let response = request.send().await?;
140        let status = response.status();
141        let json: T = response.json().await?;
142        debug!("Response status: {}, JSON: {:?}", status, json);
143        Ok(json)
144    }
145
146    pub async fn delete_with_body<T>(
147        &self,
148        endpoint: &str,
149        body: &impl Serialize,
150    ) -> Result<T, Error>
151    where
152        T: serde::de::DeserializeOwned + std::fmt::Debug,
153    {
154        let url = self.get_url(endpoint);
155        let mut request = self.http_client.delete(&url).json(body);
156
157        if let Some(cookie) = &self.session_cookie {
158            request = request.header("Cookie", format!("cloudreve-session={}", cookie));
159        }
160
161        debug!("DELETE WITH BODY URL: {}", url);
162
163        let response = request.send().await?;
164        let status = response.status();
165        let json: T = response.json().await?;
166        debug!("Response status: {}, JSON: {:?}", status, json);
167        Ok(json)
168    }
169}