files_sdk/users/
group_users.rs1use crate::{FilesClient, PaginationInfo, Result};
4use serde::{Deserialize, Serialize};
5use serde_json::json;
6
7#[derive(Debug, Serialize, Deserialize, Clone)]
9pub struct GroupUserEntity {
10 pub id: Option<i64>,
11 pub group_name: Option<String>,
12 pub group_id: Option<i64>,
13 pub user_id: Option<i64>,
14 pub username: Option<String>,
15 pub admin: Option<bool>,
16}
17
18#[derive(Debug, Clone)]
19pub struct GroupUserHandler {
20 client: FilesClient,
21}
22
23impl GroupUserHandler {
24 pub fn new(client: FilesClient) -> Self {
25 Self { client }
26 }
27
28 pub async fn list(
29 &self,
30 user_id: Option<i64>,
31 cursor: Option<String>,
32 per_page: Option<i64>,
33 ) -> Result<(Vec<GroupUserEntity>, PaginationInfo)> {
34 let mut endpoint = "/group_users".to_string();
35 let mut params = Vec::new();
36
37 if let Some(uid) = user_id {
38 params.push(format!("user_id={}", uid));
39 }
40 if let Some(c) = cursor {
41 params.push(format!("cursor={}", c));
42 }
43 if let Some(pp) = per_page {
44 params.push(format!("per_page={}", pp));
45 }
46
47 if !params.is_empty() {
48 endpoint.push('?');
49 endpoint.push_str(¶ms.join("&"));
50 }
51
52 let url = format!("{}{}", self.client.inner.base_url, endpoint);
53 let response = reqwest::Client::new()
54 .get(&url)
55 .header("X-FilesAPI-Key", &self.client.inner.api_key)
56 .send()
57 .await?;
58
59 let headers = response.headers().clone();
60 let pagination = PaginationInfo::from_headers(&headers);
61 let status = response.status();
62
63 if !status.is_success() {
64 return Err(crate::FilesError::ApiError {
65 endpoint: None,
66 code: status.as_u16(),
67 message: response.text().await.unwrap_or_default(),
68 });
69 }
70
71 let users: Vec<GroupUserEntity> = response.json().await?;
72 Ok((users, pagination))
73 }
74
75 pub async fn create(
76 &self,
77 group_id: i64,
78 user_id: i64,
79 admin: bool,
80 ) -> Result<GroupUserEntity> {
81 let body = json!({
82 "group_id": group_id,
83 "user_id": user_id,
84 "admin": admin,
85 });
86
87 let response = self.client.post_raw("/group_users", body).await?;
88 Ok(serde_json::from_value(response)?)
89 }
90
91 pub async fn update(&self, id: i64, admin: bool) -> Result<GroupUserEntity> {
92 let body = json!({"admin": admin});
93 let endpoint = format!("/group_users/{}", id);
94 let response = self.client.patch_raw(&endpoint, body).await?;
95 Ok(serde_json::from_value(response)?)
96 }
97
98 pub async fn delete(&self, id: i64) -> Result<()> {
99 let endpoint = format!("/group_users/{}", id);
100 self.client.delete_raw(&endpoint).await?;
101 Ok(())
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108
109 #[test]
110 fn test_handler_creation() {
111 let client = FilesClient::builder().api_key("test-key").build().unwrap();
112 let _handler = GroupUserHandler::new(client);
113 }
114}