Skip to main content

context69_sdk/client/library/
mod.rs

1mod files;
2mod folders;
3mod texts;
4
5use context69_contracts::{
6    LibraryIngestJobResponse, LibraryTreeResponse, LibraryUrlImportJobResponse,
7};
8use reqwest::Method;
9use uuid::Uuid;
10
11use super::Context69Client;
12use crate::{Error, client::transport::group_path};
13
14pub use files::{LibraryFileApi, LibraryFilesApi};
15pub use folders::{LibraryFolderApi, LibraryFoldersApi};
16pub use texts::{GroupLibraryTextsApi, LibraryTextsApi};
17
18pub struct LibraryApi<'a> {
19    client: &'a Context69Client,
20}
21
22impl<'a> LibraryApi<'a> {
23    pub(crate) fn new(client: &'a Context69Client) -> Self {
24        Self { client }
25    }
26
27    pub async fn tree(&self) -> Result<LibraryTreeResponse, Error> {
28        get_tree(self.client, "/v1/library").await
29    }
30
31    pub fn folders(&self) -> LibraryFoldersApi<'a> {
32        LibraryFoldersApi::new(self.client, "/v1/library".to_string())
33    }
34
35    pub fn folder(&self, folder_id: Uuid) -> LibraryFolderApi<'a> {
36        LibraryFolderApi::new(self.client, "/v1/library".to_string(), folder_id)
37    }
38
39    pub fn texts(&self) -> LibraryTextsApi<'a> {
40        LibraryTextsApi::new(self.client, "/v1/library".to_string())
41    }
42
43    pub fn files(&self) -> LibraryFilesApi<'a> {
44        LibraryFilesApi::new(self.client, "/v1/library".to_string())
45    }
46
47    pub fn file(&self, file_id: Uuid) -> LibraryFileApi<'a> {
48        LibraryFileApi::new(self.client, "/v1/library".to_string(), file_id)
49    }
50
51    pub fn job(&self, job_id: Uuid) -> LibraryJobApi<'a> {
52        LibraryJobApi::new(self.client, "/v1/library".to_string(), job_id)
53    }
54}
55
56pub struct LibraryUrlImportJobApi<'a> {
57    client: &'a Context69Client,
58    base_path: String,
59    job_id: Uuid,
60}
61
62impl<'a> LibraryUrlImportJobApi<'a> {
63    fn new(client: &'a Context69Client, base_path: String, job_id: Uuid) -> Self {
64        Self {
65            client,
66            base_path,
67            job_id,
68        }
69    }
70
71    pub async fn get(&self) -> Result<LibraryUrlImportJobResponse, Error> {
72        let path = format!("{}/url-import-jobs/{}", self.base_path, self.job_id);
73        self.client
74            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
75            .await
76    }
77
78    pub async fn retry(&self) -> Result<LibraryUrlImportJobResponse, Error> {
79        let path = format!("{}/url-import-jobs/{}/retry", self.base_path, self.job_id);
80        self.client
81            .execute_json(self.client.authorized_request(Method::POST, &path).await?)
82            .await
83    }
84}
85
86pub struct GroupLibraryApi<'a> {
87    client: &'a Context69Client,
88    base_path: String,
89}
90
91impl<'a> GroupLibraryApi<'a> {
92    pub(crate) fn new(client: &'a Context69Client, group: String) -> Self {
93        Self {
94            client,
95            base_path: group_path(&group, "/library"),
96        }
97    }
98
99    pub async fn tree(&self) -> Result<LibraryTreeResponse, Error> {
100        get_tree(self.client, &self.base_path).await
101    }
102
103    pub fn folders(&self) -> LibraryFoldersApi<'a> {
104        LibraryFoldersApi::new(self.client, self.base_path.clone())
105    }
106
107    pub fn folder(&self, folder_id: Uuid) -> LibraryFolderApi<'a> {
108        LibraryFolderApi::new(self.client, self.base_path.clone(), folder_id)
109    }
110
111    pub fn texts(&self) -> GroupLibraryTextsApi<'a> {
112        GroupLibraryTextsApi::new(self.client, self.base_path.clone())
113    }
114
115    pub fn files(&self) -> LibraryFilesApi<'a> {
116        LibraryFilesApi::new(self.client, self.base_path.clone())
117    }
118
119    pub fn file(&self, file_id: Uuid) -> LibraryFileApi<'a> {
120        LibraryFileApi::new(self.client, self.base_path.clone(), file_id)
121    }
122
123    pub fn job(&self, job_id: Uuid) -> LibraryJobApi<'a> {
124        LibraryJobApi::new(self.client, self.base_path.clone(), job_id)
125    }
126
127    pub fn url_import_job(&self, job_id: Uuid) -> LibraryUrlImportJobApi<'a> {
128        LibraryUrlImportJobApi::new(self.client, self.base_path.clone(), job_id)
129    }
130}
131
132pub struct LibraryJobApi<'a> {
133    client: &'a Context69Client,
134    base_path: String,
135    job_id: Uuid,
136}
137
138impl<'a> LibraryJobApi<'a> {
139    fn new(client: &'a Context69Client, base_path: String, job_id: Uuid) -> Self {
140        Self {
141            client,
142            base_path,
143            job_id,
144        }
145    }
146
147    pub async fn get(&self) -> Result<LibraryIngestJobResponse, Error> {
148        let path = format!("{}/jobs/{}", self.base_path, self.job_id);
149        self.client
150            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
151            .await
152    }
153}
154
155async fn get_tree(client: &Context69Client, base_path: &str) -> Result<LibraryTreeResponse, Error> {
156    let path = format!("{base_path}/tree");
157    client
158        .execute_json(client.authorized_request(Method::GET, &path).await?)
159        .await
160}