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