Skip to main content

context69_sdk/client/groups/
mod.rs

1mod documents;
2mod members;
3mod source_folders;
4mod translations;
5
6use context69_contracts::{
7    CreateGroupRequest, GroupResponse, MoveGroupRequest, UpdateGroupRequest,
8};
9use reqwest::Method;
10
11use super::{Context69Client, GroupLibraryApi};
12use crate::{Error, client::transport::group_path};
13
14pub use documents::{GroupDocumentsApi, GroupMetadataIndexesApi};
15pub use members::{GroupMemberApi, GroupMembersApi};
16pub use source_folders::{GroupSourceFolderApi, GroupSourceFoldersApi};
17pub use translations::{GroupTranslationApi, TranslationJobApi};
18
19pub struct GroupsApi<'a> {
20    client: &'a Context69Client,
21}
22
23impl<'a> GroupsApi<'a> {
24    pub(crate) fn new(client: &'a Context69Client) -> Self {
25        Self { client }
26    }
27
28    pub async fn list(&self) -> Result<Vec<GroupResponse>, Error> {
29        self.client
30            .execute_json(
31                self.client
32                    .authorized_request(Method::GET, "/v1/groups")
33                    .await?,
34            )
35            .await
36    }
37
38    pub async fn create(&self, request: &CreateGroupRequest) -> Result<GroupResponse, Error> {
39        self.client
40            .execute_json(
41                self.client
42                    .authorized_request(Method::POST, "/v1/groups")
43                    .await?
44                    .json(request),
45            )
46            .await
47    }
48}
49
50pub struct GroupApi<'a> {
51    client: &'a Context69Client,
52    group_path: String,
53}
54
55impl<'a> GroupApi<'a> {
56    pub(crate) fn new(client: &'a Context69Client, group_path: String) -> Self {
57        Self { client, group_path }
58    }
59
60    pub async fn get(&self) -> Result<GroupResponse, Error> {
61        let path = group_path(&self.group_path, "");
62        self.client
63            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
64            .await
65    }
66
67    pub async fn update(&self, request: &UpdateGroupRequest) -> Result<GroupResponse, Error> {
68        let path = group_path(&self.group_path, "");
69        self.client
70            .execute_json(
71                self.client
72                    .authorized_request(Method::PATCH, &path)
73                    .await?
74                    .json(request),
75            )
76            .await
77    }
78
79    pub async fn move_to(&self, request: &MoveGroupRequest) -> Result<GroupResponse, Error> {
80        let path = group_path(&self.group_path, "/move");
81        self.client
82            .execute_json(
83                self.client
84                    .authorized_request(Method::POST, &path)
85                    .await?
86                    .json(request),
87            )
88            .await
89    }
90
91    pub async fn delete(&self) -> Result<(), Error> {
92        let path = group_path(&self.group_path, "");
93        self.client
94            .execute_empty(
95                self.client
96                    .authorized_request(Method::DELETE, &path)
97                    .await?,
98            )
99            .await
100    }
101
102    pub fn children(&self) -> GroupChildrenApi<'a> {
103        GroupChildrenApi::new(self.client, self.group_path.clone())
104    }
105
106    pub fn members(&self) -> GroupMembersApi<'a> {
107        GroupMembersApi::new(self.client, self.group_path.clone())
108    }
109
110    pub fn member(&self, login_name: impl Into<String>) -> GroupMemberApi<'a> {
111        GroupMemberApi::new(self.client, self.group_path.clone(), login_name.into())
112    }
113
114    pub fn library(&self) -> GroupLibraryApi<'a> {
115        GroupLibraryApi::new(self.client, self.group_path.clone())
116    }
117
118    pub fn source_folders(&self) -> GroupSourceFoldersApi<'a> {
119        GroupSourceFoldersApi::new(self.client, self.group_path.clone())
120    }
121
122    pub fn documents(&self) -> GroupDocumentsApi<'a> {
123        GroupDocumentsApi::new(self.client, self.group_path.clone())
124    }
125
126    pub fn metadata_indexes(&self, source_key: impl Into<String>) -> GroupMetadataIndexesApi<'a> {
127        GroupMetadataIndexesApi::new(self.client, self.group_path.clone(), source_key.into())
128    }
129
130    pub fn source_folder(&self, folder_id: uuid::Uuid) -> GroupSourceFolderApi<'a> {
131        GroupSourceFolderApi::new(self.client, self.group_path.clone(), folder_id)
132    }
133
134    pub fn translations(&self) -> GroupTranslationApi<'a> {
135        GroupTranslationApi::new(self.client, self.group_path.clone())
136    }
137
138    pub fn translation_job(&self, job_id: uuid::Uuid) -> TranslationJobApi<'a> {
139        TranslationJobApi::new(self.client, self.group_path.clone(), job_id)
140    }
141}
142
143pub struct GroupChildrenApi<'a> {
144    client: &'a Context69Client,
145    group_path: String,
146}
147
148impl<'a> GroupChildrenApi<'a> {
149    fn new(client: &'a Context69Client, group_path: String) -> Self {
150        Self { client, group_path }
151    }
152
153    pub async fn list(&self) -> Result<Vec<GroupResponse>, Error> {
154        let path = group_path(&self.group_path, "/children");
155        self.client
156            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
157            .await
158    }
159}