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    /// 获取指定用户的详情信息。Get detailed information for a specified user.
27    pub async fn get_users_username(
28        &self,
29        username: String,
30    ) -> Result<Value> {
31        let path = format!("/users/{}", username);
32        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
33        
34
35                let request = self.client.request(
36            reqwest::Method::GET,
37            url
38        );
39        
40
41
42
43        let response = request.send().await?;
44        
45        if response.status().is_success() {
46            let json: Value = response.json().await?;
47            Ok(json)
48        } else {
49            Err(ApiError::HttpError(response.status().as_u16()))
50        }
51    }
52
53    /// 获取指定用户的详情信息。Get detailed information for a specified user.
54    pub async fn get_user(
55        &self,
56    ) -> Result<Value> {
57        let path = format!("/user");
58        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
59        
60
61                let request = self.client.request(
62            reqwest::Method::GET,
63            url
64        );
65        
66
67
68
69        let response = request.send().await?;
70        
71        if response.status().is_success() {
72            let json: Value = response.json().await?;
73            Ok(json)
74        } else {
75            Err(ApiError::HttpError(response.status().as_u16()))
76        }
77    }
78
79    /// 更新指定用户的详情信息。Updates the specified user&#x27;s profile information.
80    pub async fn post_user(
81        &self,
82        request_data: serde_json::Value,
83    ) -> Result<Value> {
84        let path = format!("/user");
85        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
86        
87
88        
89        let mut request = self.client.request(
90            reqwest::Method::POST,
91            url
92        );
93
94
95
96        request = request.json(&request_data);
97
98        let response = request.send().await?;
99        
100        if response.status().is_success() {
101            let json: Value = response.json().await?;
102            Ok(json)
103        } else {
104            Err(ApiError::HttpError(response.status().as_u16()))
105        }
106    }
107
108    /// 查询当前用户用户拥有指定权限的所有资源列表。List resources that the current user has specified permissions for.
109    pub async fn get_user_autocomplete_source(
110        &self,
111        source_type: Option<String>,
112        page: Option<i64>,
113        page_size: Option<i64>,
114        search: Option<String>,
115        access: Option<String>,
116        order_by: Option<String>,
117        desc: Option<bool>,
118    ) -> Result<Value> {
119        let path = format!("/user/autocomplete_source");
120        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
121        
122        if let Some(value) = source_type {
123            url.query_pairs_mut().append_pair("source_type", &value.to_string());
124        }
125        if let Some(value) = page {
126            url.query_pairs_mut().append_pair("page", &value.to_string());
127        }
128        if let Some(value) = page_size {
129            url.query_pairs_mut().append_pair("page_size", &value.to_string());
130        }
131        if let Some(value) = search {
132            url.query_pairs_mut().append_pair("search", &value.to_string());
133        }
134        if let Some(value) = access {
135            url.query_pairs_mut().append_pair("access", &value.to_string());
136        }
137        if let Some(value) = order_by {
138            url.query_pairs_mut().append_pair("order_by", &value.to_string());
139        }
140        if let Some(value) = desc {
141            url.query_pairs_mut().append_pair("desc", &value.to_string());
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}