files_sdk/storage/
projects.rs1use crate::{FilesClient, PaginationInfo, Result};
4use serde::{Deserialize, Serialize};
5use serde_json::json;
6
7#[derive(Debug, Serialize, Deserialize, Clone)]
9pub struct ProjectEntity {
10 pub id: Option<i64>,
11 pub global_access: Option<String>,
12}
13
14#[derive(Debug, Clone)]
15pub struct ProjectHandler {
16 client: FilesClient,
17}
18
19impl ProjectHandler {
20 pub fn new(client: FilesClient) -> Self {
21 Self { client }
22 }
23
24 pub async fn list(
25 &self,
26 cursor: Option<String>,
27 per_page: Option<i64>,
28 ) -> Result<(Vec<ProjectEntity>, PaginationInfo)> {
29 let mut endpoint = "/projects".to_string();
30 let mut params = Vec::new();
31
32 if let Some(c) = cursor {
33 params.push(format!("cursor={}", c));
34 }
35 if let Some(pp) = per_page {
36 params.push(format!("per_page={}", pp));
37 }
38
39 if !params.is_empty() {
40 endpoint.push('?');
41 endpoint.push_str(¶ms.join("&"));
42 }
43
44 let url = format!("{}{}", self.client.inner.base_url, endpoint);
45 let response = reqwest::Client::new()
46 .get(&url)
47 .header("X-FilesAPI-Key", &self.client.inner.api_key)
48 .send()
49 .await?;
50
51 let headers = response.headers().clone();
52 let pagination = PaginationInfo::from_headers(&headers);
53 let status = response.status();
54
55 if !status.is_success() {
56 return Err(crate::FilesError::ApiError {
57 endpoint: None,
58 code: status.as_u16(),
59 message: response.text().await.unwrap_or_default(),
60 });
61 }
62
63 let projects: Vec<ProjectEntity> = response.json().await?;
64 Ok((projects, pagination))
65 }
66
67 pub async fn get(&self, id: i64) -> Result<ProjectEntity> {
68 let endpoint = format!("/projects/{}", id);
69 let response = self.client.get_raw(&endpoint).await?;
70 Ok(serde_json::from_value(response)?)
71 }
72
73 pub async fn create(&self, global_access: &str) -> Result<ProjectEntity> {
74 let body = json!({"global_access": global_access});
75 let response = self.client.post_raw("/projects", body).await?;
76 Ok(serde_json::from_value(response)?)
77 }
78
79 pub async fn update(&self, id: i64, global_access: &str) -> Result<ProjectEntity> {
80 let body = json!({"global_access": global_access});
81 let endpoint = format!("/projects/{}", id);
82 let response = self.client.patch_raw(&endpoint, body).await?;
83 Ok(serde_json::from_value(response)?)
84 }
85
86 pub async fn delete(&self, id: i64) -> Result<()> {
87 let endpoint = format!("/projects/{}", id);
88 self.client.delete_raw(&endpoint).await?;
89 Ok(())
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 #[test]
98 fn test_handler_creation() {
99 let client = FilesClient::builder().api_key("test-key").build().unwrap();
100 let _handler = ProjectHandler::new(client);
101 }
102}