rust_cnb/
contributors.rs

1//! Contributors API 客户端
2
3use crate::error::{ApiError, Result};
4use reqwest::Client;
5use serde_json::Value;
6use url::Url;
7
8/// Contributors API 客户端
9pub struct ContributorsClient {
10    base_url: String,
11    client: Client,
12}
13
14impl ContributorsClient {
15    /// 创建新的 Contributors 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 permission information for accessing members at current level.
27    pub async fn get_group_members_access_level(
28        &self,
29        group: String,
30        include_inherit: Option<bool>,
31    ) -> Result<Value> {
32        let path = format!("/{}/-/members/access-level", group);
33        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
34        
35        if let Some(value) = include_inherit {
36            url.query_pairs_mut().append_pair("include_inherit", &value.to_string());
37        }
38
39                let request = self.client.request(
40            reqwest::Method::GET,
41            url
42        );
43        
44
45
46
47        let response = request.send().await?;
48        
49        if response.status().is_success() {
50            let json: Value = response.json().await?;
51            Ok(json)
52        } else {
53            Err(ApiError::HttpError(response.status().as_u16()))
54        }
55    }
56
57    /// 获取指定组织或仓库内指定成员的权限信息, 结果按组织层级来展示, 包含上层组织的权限继承信息。Get specified member&#x27;s permissions with organizational hierarchy.
58    pub async fn get_repo_members_username_access_level(
59        &self,
60        repo: String,
61        username: String,
62    ) -> Result<Value> {
63        let path = format!("/{}/-/members/{}/access-level", repo, username);
64        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
65        
66
67                let request = self.client.request(
68            reqwest::Method::GET,
69            url
70        );
71        
72
73
74
75        let response = request.send().await?;
76        
77        if response.status().is_success() {
78            let json: Value = response.json().await?;
79            Ok(json)
80        } else {
81            Err(ApiError::HttpError(response.status().as_u16()))
82        }
83    }
84
85    /// 获取指定组织或仓库内, 访问成员在当前层级内的权限信息。Get permission information for accessing members at current level.
86    pub async fn get_repo_members_access_level(
87        &self,
88        repo: String,
89        include_inherit: Option<bool>,
90    ) -> Result<Value> {
91        let path = format!("/{}/-/members/access-level", repo);
92        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
93        
94        if let Some(value) = include_inherit {
95            url.query_pairs_mut().append_pair("include_inherit", &value.to_string());
96        }
97
98                let request = self.client.request(
99            reqwest::Method::GET,
100            url
101        );
102        
103
104
105
106        let response = request.send().await?;
107        
108        if response.status().is_success() {
109            let json: Value = response.json().await?;
110            Ok(json)
111        } else {
112            Err(ApiError::HttpError(response.status().as_u16()))
113        }
114    }
115
116    /// 获取指定组织或仓库内指定成员的权限信息, 结果按组织层级来展示, 包含上层组织的权限继承信息。Get specified member&#x27;s permissions with organizational hierarchy.
117    pub async fn get_group_members_username_access_level(
118        &self,
119        group: String,
120        username: String,
121    ) -> Result<Value> {
122        let path = format!("/{}/-/members/{}/access-level", group, username);
123        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
124        
125
126                let request = self.client.request(
127            reqwest::Method::GET,
128            url
129        );
130        
131
132
133
134        let response = request.send().await?;
135        
136        if response.status().is_success() {
137            let json: Value = response.json().await?;
138            Ok(json)
139        } else {
140            Err(ApiError::HttpError(response.status().as_u16()))
141        }
142    }
143
144}