Skip to main content

context69_sdk/client/library/
mod.rs

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