Skip to main content

context69_sdk/client/groups/
mod.rs

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