Skip to main content

context69_sdk/client/groups/
mod.rs

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