rust_cnb/
repo_labels.rs

1//! RepoLabels API 客户端
2
3use crate::error::{ApiError, Result};
4use reqwest::Client;
5use serde_json::Value;
6use url::Url;
7
8/// RepoLabels API 客户端
9pub struct RepoLabelsClient {
10    base_url: String,
11    client: Client,
12}
13
14impl RepoLabelsClient {
15    /// 创建新的 RepoLabels 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    /// 删除指定的仓库标签 label。Delete the specified repository label.
27    pub async fn delete_repo_labels_name(
28        &self,
29        repo: String,
30        name: String,
31    ) -> Result<Value> {
32        let path = format!("/{}/-/labels/{}", repo, name);
33        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
34        
35
36                let request = self.client.request(
37            reqwest::Method::DELETE,
38            url
39        );
40        
41
42
43
44        let response = request.send().await?;
45        
46        if response.status().is_success() {
47            let json: Value = response.json().await?;
48            Ok(json)
49        } else {
50            Err(ApiError::HttpError(response.status().as_u16()))
51        }
52    }
53
54    /// 更新标签信息。Update label information.
55    pub async fn patch_repo_labels_name(
56        &self,
57        repo: String,
58        name: String,
59        patch_label_form: serde_json::Value,
60    ) -> Result<Value> {
61        let path = format!("/{}/-/labels/{}", repo, name);
62        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
63        
64
65        
66        let mut request = self.client.request(
67            reqwest::Method::PATCH,
68            url
69        );
70
71
72
73        request = request.json(&patch_label_form);
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    /// 查询仓库的标签(label) 列表。List repository labels.
86    pub async fn get_repo_labels(
87        &self,
88        repo: String,
89        page: Option<i64>,
90        page_size: Option<i64>,
91        keyword: Option<String>,
92    ) -> Result<Value> {
93        let path = format!("/{}/-/labels", repo);
94        let mut url = Url::parse(&format!("{}{}", self.base_url, path))?;
95        
96        if let Some(value) = page {
97            url.query_pairs_mut().append_pair("page", &value.to_string());
98        }
99        if let Some(value) = page_size {
100            url.query_pairs_mut().append_pair("page_size", &value.to_string());
101        }
102        if let Some(value) = keyword {
103            url.query_pairs_mut().append_pair("keyword", &value.to_string());
104        }
105
106                let request = self.client.request(
107            reqwest::Method::GET,
108            url
109        );
110        
111
112
113
114        let response = request.send().await?;
115        
116        if response.status().is_success() {
117            let json: Value = response.json().await?;
118            Ok(json)
119        } else {
120            Err(ApiError::HttpError(response.status().as_u16()))
121        }
122    }
123
124    /// 创建一个 标签。Create a label.
125    pub async fn post_repo_labels(
126        &self,
127        repo: String,
128        post_label_form: serde_json::Value,
129    ) -> Result<Value> {
130        let path = format!("/{}/-/labels", repo);
131        let url = Url::parse(&format!("{}{}", self.base_url, path))?;
132        
133
134        
135        let mut request = self.client.request(
136            reqwest::Method::POST,
137            url
138        );
139
140
141
142        request = request.json(&post_label_form);
143
144        let response = request.send().await?;
145        
146        if response.status().is_success() {
147            let json: Value = response.json().await?;
148            Ok(json)
149        } else {
150            Err(ApiError::HttpError(response.status().as_u16()))
151        }
152    }
153
154}