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 code: status.as_u16(),
66 message: response.text().await.unwrap_or_default(),
67 });
68 }
69
70 let users: Vec<GroupUserEntity> = response.json().await?;
71 Ok((users, pagination))
72 }
73
74 pub async fn create(
75 &self,
76 group_id: i64,
77 user_id: i64,
78 admin: bool,
79 ) -> Result<GroupUserEntity> {
80 let body = json!({
81 "group_id": group_id,
82 "user_id": user_id,
83 "admin": admin,
84 });
85
86 let response = self.client.post_raw("/group_users", body).await?;
87 Ok(serde_json::from_value(response)?)
88 }
89
90 pub async fn update(&self, id: i64, admin: bool) -> Result<GroupUserEntity> {
91 let body = json!({"admin": admin});
92 let endpoint = format!("/group_users/{}", id);
93 let response = self.client.patch_raw(&endpoint, body).await?;
94 Ok(serde_json::from_value(response)?)
95 }
96
97 pub async fn delete(&self, id: i64) -> Result<()> {
98 let endpoint = format!("/group_users/{}", id);
99 self.client.delete_raw(&endpoint).await?;
100 Ok(())
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn test_handler_creation() {
110 let client = FilesClient::builder().api_key("test-key").build().unwrap();
111 let _handler = GroupUserHandler::new(client);
112 }
113}