Skip to main content

context69_sdk/client/
library.rs

1use context69_contracts::{
2    CreateFolderRequest, CreateTextRequest, LibraryFileDetailResponse, LibraryFolderResponse,
3    LibraryIngestJobResponse, LibraryTreeResponse, LibraryUploadResponse, MoveFileRequest,
4    MoveFolderRequest, UpsertLibraryTextRequest,
5};
6use reqwest::{Method, multipart::Part};
7use uuid::Uuid;
8
9use super::file_upload_form;
10use crate::{Context69Client, Error};
11
12impl Context69Client {
13    pub async fn get_library_tree(&self) -> Result<LibraryTreeResponse, Error> {
14        self.execute_json(
15            self.authorized_request(Method::GET, "/v1/library/tree")
16                .await?,
17        )
18        .await
19    }
20
21    pub async fn create_library_folder(
22        &self,
23        request: &CreateFolderRequest,
24    ) -> Result<LibraryFolderResponse, Error> {
25        self.execute_json(
26            self.authorized_request(Method::POST, "/v1/library/folders")
27                .await?
28                .json(request),
29        )
30        .await
31    }
32
33    pub async fn create_library_text(
34        &self,
35        request: &CreateTextRequest,
36    ) -> Result<LibraryUploadResponse, Error> {
37        self.execute_json(
38            self.authorized_request(Method::POST, "/v1/library/texts")
39                .await?
40                .json(request),
41        )
42        .await
43    }
44
45    pub async fn move_library_folder(
46        &self,
47        folder_id: Uuid,
48        request: &MoveFolderRequest,
49    ) -> Result<LibraryFolderResponse, Error> {
50        let path = format!("/v1/library/folders/{folder_id}/move");
51        self.execute_json(
52            self.authorized_request(Method::POST, &path)
53                .await?
54                .json(request),
55        )
56        .await
57    }
58
59    pub async fn delete_library_folder(&self, folder_id: Uuid) -> Result<(), Error> {
60        let path = format!("/v1/library/folders/{folder_id}");
61        self.execute_empty(self.authorized_request(Method::DELETE, &path).await?)
62            .await
63    }
64
65    pub async fn upload_library_files(
66        &self,
67        folder_id: Option<Uuid>,
68        files: Vec<Part>,
69    ) -> Result<LibraryUploadResponse, Error> {
70        self.execute_json(
71            self.authorized_request(Method::POST, "/v1/library/files/upload")
72                .await?
73                .multipart(file_upload_form(folder_id, files)),
74        )
75        .await
76    }
77
78    pub async fn get_library_file(
79        &self,
80        file_id: Uuid,
81    ) -> Result<LibraryFileDetailResponse, Error> {
82        let path = format!("/v1/library/files/{file_id}");
83        self.execute_json(self.authorized_request(Method::GET, &path).await?)
84            .await
85    }
86
87    pub async fn move_library_file(
88        &self,
89        file_id: Uuid,
90        request: &MoveFileRequest,
91    ) -> Result<LibraryFileDetailResponse, Error> {
92        let path = format!("/v1/library/files/{file_id}/move");
93        self.execute_json(
94            self.authorized_request(Method::POST, &path)
95                .await?
96                .json(request),
97        )
98        .await
99    }
100
101    pub async fn delete_library_file(&self, file_id: Uuid) -> Result<(), Error> {
102        let path = format!("/v1/library/files/{file_id}");
103        self.execute_empty(self.authorized_request(Method::DELETE, &path).await?)
104            .await
105    }
106
107    pub async fn get_library_job(&self, job_id: Uuid) -> Result<LibraryIngestJobResponse, Error> {
108        let path = format!("/v1/library/jobs/{job_id}");
109        self.execute_json(self.authorized_request(Method::GET, &path).await?)
110            .await
111    }
112
113    pub async fn get_project_library_tree(
114        &self,
115        group_key: &str,
116        project_key: &str,
117    ) -> Result<LibraryTreeResponse, Error> {
118        let path = format!("/v1/groups/{group_key}/projects/{project_key}/library/tree");
119        self.execute_json(self.authorized_request(Method::GET, &path).await?)
120            .await
121    }
122
123    pub async fn create_project_library_folder(
124        &self,
125        group_key: &str,
126        project_key: &str,
127        request: &CreateFolderRequest,
128    ) -> Result<LibraryFolderResponse, Error> {
129        let path = format!("/v1/groups/{group_key}/projects/{project_key}/library/folders");
130        self.execute_json(
131            self.authorized_request(Method::POST, &path)
132                .await?
133                .json(request),
134        )
135        .await
136    }
137
138    pub async fn create_project_library_text(
139        &self,
140        group_key: &str,
141        project_key: &str,
142        request: &CreateTextRequest,
143    ) -> Result<LibraryUploadResponse, Error> {
144        let path = format!("/v1/groups/{group_key}/projects/{project_key}/library/texts");
145        self.execute_json(
146            self.authorized_request(Method::POST, &path)
147                .await?
148                .json(request),
149        )
150        .await
151    }
152
153    pub async fn upsert_project_library_text(
154        &self,
155        group_key: &str,
156        project_key: &str,
157        request: &UpsertLibraryTextRequest,
158    ) -> Result<LibraryUploadResponse, Error> {
159        let path = format!("/v1/groups/{group_key}/projects/{project_key}/library/texts");
160        self.execute_json(
161            self.authorized_request(Method::PUT, &path)
162                .await?
163                .json(request),
164        )
165        .await
166    }
167
168    pub async fn move_project_library_folder(
169        &self,
170        group_key: &str,
171        project_key: &str,
172        folder_id: Uuid,
173        request: &MoveFolderRequest,
174    ) -> Result<LibraryFolderResponse, Error> {
175        let path = format!(
176            "/v1/groups/{group_key}/projects/{project_key}/library/folders/{folder_id}/move"
177        );
178        self.execute_json(
179            self.authorized_request(Method::POST, &path)
180                .await?
181                .json(request),
182        )
183        .await
184    }
185
186    pub async fn delete_project_library_folder(
187        &self,
188        group_key: &str,
189        project_key: &str,
190        folder_id: Uuid,
191    ) -> Result<(), Error> {
192        let path =
193            format!("/v1/groups/{group_key}/projects/{project_key}/library/folders/{folder_id}");
194        self.execute_empty(self.authorized_request(Method::DELETE, &path).await?)
195            .await
196    }
197
198    pub async fn upload_project_library_files(
199        &self,
200        group_key: &str,
201        project_key: &str,
202        folder_id: Option<Uuid>,
203        files: Vec<Part>,
204    ) -> Result<LibraryUploadResponse, Error> {
205        let path = format!("/v1/groups/{group_key}/projects/{project_key}/library/files/upload");
206        self.execute_json(
207            self.authorized_request(Method::POST, &path)
208                .await?
209                .multipart(file_upload_form(folder_id, files)),
210        )
211        .await
212    }
213
214    pub async fn get_project_library_file(
215        &self,
216        group_key: &str,
217        project_key: &str,
218        file_id: Uuid,
219    ) -> Result<LibraryFileDetailResponse, Error> {
220        let path = format!("/v1/groups/{group_key}/projects/{project_key}/library/files/{file_id}");
221        self.execute_json(self.authorized_request(Method::GET, &path).await?)
222            .await
223    }
224
225    pub async fn move_project_library_file(
226        &self,
227        group_key: &str,
228        project_key: &str,
229        file_id: Uuid,
230        request: &MoveFileRequest,
231    ) -> Result<LibraryFileDetailResponse, Error> {
232        let path =
233            format!("/v1/groups/{group_key}/projects/{project_key}/library/files/{file_id}/move");
234        self.execute_json(
235            self.authorized_request(Method::POST, &path)
236                .await?
237                .json(request),
238        )
239        .await
240    }
241
242    pub async fn delete_project_library_file(
243        &self,
244        group_key: &str,
245        project_key: &str,
246        file_id: Uuid,
247    ) -> Result<(), Error> {
248        let path = format!("/v1/groups/{group_key}/projects/{project_key}/library/files/{file_id}");
249        self.execute_empty(self.authorized_request(Method::DELETE, &path).await?)
250            .await
251    }
252
253    pub async fn get_project_library_job(
254        &self,
255        group_key: &str,
256        project_key: &str,
257        job_id: Uuid,
258    ) -> Result<LibraryIngestJobResponse, Error> {
259        let path = format!("/v1/groups/{group_key}/projects/{project_key}/library/jobs/{job_id}");
260        self.execute_json(self.authorized_request(Method::GET, &path).await?)
261            .await
262    }
263}