Skip to main content

context69_sdk/client/groups/
members.rs

1use context69_contracts::{GroupMemberPageResponse, NamespacePageQuery, UpsertMembershipRequest};
2use reqwest::Method;
3
4use super::Context69Client;
5use crate::{
6    Error,
7    client::transport::{encode_path_component, group_path},
8};
9
10pub struct GroupMembersApi<'a> {
11    client: &'a Context69Client,
12    group_path: String,
13}
14
15impl<'a> GroupMembersApi<'a> {
16    pub(super) fn new(client: &'a Context69Client, group_path: String) -> Self {
17        Self { client, group_path }
18    }
19
20    pub async fn list(&self) -> Result<GroupMemberPageResponse, Error> {
21        let path = group_path(&self.group_path, "/members");
22        self.client
23            .execute_json(self.client.authorized_request(Method::GET, &path).await?)
24            .await
25    }
26
27    pub async fn list_page(
28        &self,
29        query: &NamespacePageQuery,
30    ) -> Result<GroupMemberPageResponse, Error> {
31        let path = group_path(&self.group_path, "/members");
32        self.client
33            .execute_json(
34                self.client
35                    .authorized_request(Method::GET, &path)
36                    .await?
37                    .query(query),
38            )
39            .await
40    }
41
42    pub async fn upsert(&self, request: &UpsertMembershipRequest) -> Result<(), Error> {
43        let path = group_path(&self.group_path, "/members");
44        self.client
45            .execute_empty(
46                self.client
47                    .authorized_request(Method::POST, &path)
48                    .await?
49                    .json(request),
50            )
51            .await
52    }
53}
54
55pub struct GroupMemberApi<'a> {
56    client: &'a Context69Client,
57    group_path: String,
58    login_name: String,
59}
60
61impl<'a> GroupMemberApi<'a> {
62    pub(super) fn new(client: &'a Context69Client, group_path: String, login_name: String) -> Self {
63        Self {
64            client,
65            group_path,
66            login_name,
67        }
68    }
69
70    pub async fn delete(&self) -> Result<(), Error> {
71        let suffix = format!("/members/{}", encode_path_component(&self.login_name));
72        let path = group_path(&self.group_path, &suffix);
73        self.client
74            .execute_empty(
75                self.client
76                    .authorized_request(Method::DELETE, &path)
77                    .await?,
78            )
79            .await
80    }
81}