rust_cnb/
users.rs

1//! Users API 客户端
2
3use crate::error::{ApiError, Result};
4use reqwest::Client;
5use serde_json::Value;
6use url::Url;
7
8/// Users API 客户端
9pub struct UsersClient {
10    base_url: String,
11    client: Client,
12}
13
14impl UsersClient {
15    /// 创建新的 Users API 客户端
16    pub fn new(base_url: String, client: Client) -> Self {
17        Self { base_url, client }
18    }
19
20    /// 设置认证信息
21    pub fn with_auth(self, token: &str) -> Self {
22        // 这里可以扩展认证逻辑
23        self
24    }
25
26    /// 查询当前用户用户拥有指定权限的所有资源列表。List resources that the current user has specified permissions for.
27    pub async fn get_user_autocomplete_source(
28        &self,
29        source_type: Option<String>,
30        page: Option<i64>,
31        page_size: Option<i64>,
32        search: Option<String>,
33        access: Option<String>,
34        order_by: Option<String>,
35        desc: Option<bool>,
36    ) -> Result<Value> {
37        let path = format!("/user/autocomplete_source");
38        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
39        
40        if let Some(value) = source_type {
41            url.query_pairs_mut().append_pair("source_type", &value.to_string());
42        }
43        if let Some(value) = page {
44            url.query_pairs_mut().append_pair("page", &value.to_string());
45        }
46        if let Some(value) = page_size {
47            url.query_pairs_mut().append_pair("page_size", &value.to_string());
48        }
49        if let Some(value) = search {
50            url.query_pairs_mut().append_pair("search", &value.to_string());
51        }
52        if let Some(value) = access {
53            url.query_pairs_mut().append_pair("access", &value.to_string());
54        }
55        if let Some(value) = order_by {
56            url.query_pairs_mut().append_pair("order_by", &value.to_string());
57        }
58        if let Some(value) = desc {
59            url.query_pairs_mut().append_pair("desc", &value.to_string());
60        }
61
62                let request = self.client.request(
63            reqwest::Method::GET,
64            url
65        );
66        
67
68
69
70        let response = request.send().await?;
71        
72        if response.status().is_success() {
73            let json: Value = response.json().await?;
74            Ok(json)
75        } else {
76            Err(ApiError::HttpError(response.status().as_u16()))
77        }
78    }
79
80    /// 获取指定用户的详情信息。Get detailed information for a specified user.
81    pub async fn get_user(
82        &self,
83    ) -> Result<Value> {
84        let path = format!("/user");
85        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
86        
87
88                let request = self.client.request(
89            reqwest::Method::GET,
90            url
91        );
92        
93
94
95
96        let response = request.send().await?;
97        
98        if response.status().is_success() {
99            let json: Value = response.json().await?;
100            Ok(json)
101        } else {
102            Err(ApiError::HttpError(response.status().as_u16()))
103        }
104    }
105
106    /// 更新指定用户的详情信息。Updates the specified user&#x27;s profile information.
107    pub async fn post_user(
108        &self,
109        request_data: serde_json::Value,
110    ) -> Result<Value> {
111        let path = format!("/user");
112        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
113        
114
115        
116        let mut request = self.client.request(
117            reqwest::Method::POST,
118            url
119        );
120
121
122
123        request = request.json(&request_data);
124
125        let response = request.send().await?;
126        
127        if response.status().is_success() {
128            let json: Value = response.json().await?;
129            Ok(json)
130        } else {
131            Err(ApiError::HttpError(response.status().as_u16()))
132        }
133    }
134
135    /// 获取指定用户的详情信息。Get detailed information for a specified user.
136    pub async fn get_users_username(
137        &self,
138        username: String,
139    ) -> Result<Value> {
140        let path = format!("/users/{}", username);
141        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
142        
143
144                let request = self.client.request(
145            reqwest::Method::GET,
146            url
147        );
148        
149
150
151
152        let response = request.send().await?;
153        
154        if response.status().is_success() {
155            let json: Value = response.json().await?;
156            Ok(json)
157        } else {
158            Err(ApiError::HttpError(response.status().as_u16()))
159        }
160    }
161
162}