Skip to main content

context69_sdk/client/library/
files.rs

1use context69_contracts::{LibraryFileDetailResponse, LibraryUploadResponse, MoveFileRequest};
2use reqwest::{Method, multipart::Part};
3use uuid::Uuid;
4
5use super::Context69Client;
6use crate::{Error, client::transport::file_upload_form};
7
8pub struct LibraryFilesApi<'a> {
9    client: &'a Context69Client,
10    base_path: String,
11}
12
13impl<'a> LibraryFilesApi<'a> {
14    pub(super) fn new(client: &'a Context69Client, base_path: String) -> Self {
15        Self { client, base_path }
16    }
17
18    pub async fn upload(
19        &self,
20        folder_id: Option<Uuid>,
21        files: Vec<Part>,
22    ) -> Result<LibraryUploadResponse, Error> {
23        let path = format!("{}/files/upload", self.base_path);
24        self.client
25            .execute_json(
26                self.client
27                    .authorized_request(Method::POST, &path)
28                    .await?
29                    .multipart(file_upload_form(folder_id, files)),
30            )
31            .await
32    }
33}
34
35pub struct LibraryFileApi<'a> {
36    client: &'a Context69Client,
37    base_path: String,
38    file_id: Uuid,
39}
40
41impl<'a> LibraryFileApi<'a> {
42    pub(super) fn new(client: &'a Context69Client, base_path: String, file_id: Uuid) -> Self {
43        Self {
44            client,
45            base_path,
46            file_id,
47        }
48    }
49
50    pub async fn get(&self) -> Result<LibraryFileDetailResponse, Error> {
51        let path = format!("{}/files/{}", self.base_path, self.file_id);
52        self.client
53            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
54            .await
55    }
56
57    pub async fn move_to(
58        &self,
59        request: &MoveFileRequest,
60    ) -> Result<LibraryFileDetailResponse, Error> {
61        let path = format!("{}/files/{}/move", self.base_path, self.file_id);
62        self.client
63            .execute_json(
64                self.client
65                    .authorized_request(Method::POST, &path)
66                    .await?
67                    .json(request),
68            )
69            .await
70    }
71
72    pub async fn delete(&self) -> Result<(), Error> {
73        let path = format!("{}/files/{}", self.base_path, self.file_id);
74        self.client
75            .execute_empty(
76                self.client
77                    .authorized_request(Method::DELETE, &path)
78                    .await?,
79            )
80            .await
81    }
82}