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