tencent_sdk/services/
tag.rs

1use crate::{
2    client::{TencentCloudAsync, TencentCloudBlocking},
3    core::{Endpoint, TencentCloudResult},
4};
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use std::borrow::Cow;
8
9#[derive(Debug, Deserialize)]
10/// Response payload returned by Tag `DescribeProjects`.
11///
12/// | Field | Type | Description |
13/// |-------|------|-------------|
14/// | `response` | [`DescribeProjectsResult`] | Result body containing project metadata. |
15pub struct DescribeProjectsResponse {
16    #[serde(rename = "Response")]
17    pub response: DescribeProjectsResult,
18}
19
20#[derive(Debug, Deserialize)]
21/// Detailed fields exposed by Tag project listings.
22///
23/// | Field | Type | Description |
24/// |-------|------|-------------|
25/// | `total_count` | `Option<u64>` | Number of projects matching the query. |
26/// | `project_set` | `Option<Vec<Value>>` | Raw project array returned by Tencent Cloud. |
27/// | `request_id` | `String` | Unique request identifier. |
28pub struct DescribeProjectsResult {
29    #[serde(rename = "TotalCount")]
30    pub total_count: Option<u64>,
31    #[serde(rename = "ProjectSet")]
32    #[serde(default)]
33    pub project_set: Vec<Value>,
34    #[serde(rename = "RequestId")]
35    pub request_id: String,
36}
37
38/// Request parameters for Tag `DescribeProjects`.
39///
40/// | Field | Type | Required | Description |
41/// |-------|------|----------|-------------|
42/// | `all_list` | `Option<i32>` | No | Whether to fetch all projects (defaults to `1`). |
43/// | `limit` | `Option<i32>` | No | Maximum number of items per page (defaults to `1000`). |
44/// | `offset` | `Option<i32>` | No | Pagination offset (defaults to `0`). |
45pub struct DescribeProjects {
46    pub all_list: Option<i32>,
47    pub limit: Option<i32>,
48    pub offset: Option<i32>,
49}
50
51impl Default for DescribeProjects {
52    fn default() -> Self {
53        Self {
54            all_list: Some(1),
55            limit: Some(1000),
56            offset: Some(0),
57        }
58    }
59}
60
61impl DescribeProjects {
62    /// Construct a request with no overrides.
63    pub fn new() -> Self {
64        Self {
65            all_list: None,
66            limit: None,
67            offset: None,
68        }
69    }
70
71    pub fn include_all(mut self, yes: bool) -> Self {
72        self.all_list = Some(if yes { 1 } else { 0 });
73        self
74    }
75
76    pub fn with_limit(mut self, limit: i32) -> Self {
77        self.limit = Some(limit);
78        self
79    }
80
81    pub fn with_offset(mut self, offset: i32) -> Self {
82        self.offset = Some(offset);
83        self
84    }
85}
86
87#[derive(Serialize)]
88#[serde(rename_all = "PascalCase")]
89struct DescribeProjectsPayload {
90    #[serde(skip_serializing_if = "Option::is_none")]
91    all_list: Option<i32>,
92    #[serde(skip_serializing_if = "Option::is_none")]
93    limit: Option<i32>,
94    #[serde(skip_serializing_if = "Option::is_none")]
95    offset: Option<i32>,
96}
97
98impl Endpoint for DescribeProjects {
99    type Output = DescribeProjectsResponse;
100
101    fn service(&self) -> Cow<'static, str> {
102        Cow::Borrowed("tag")
103    }
104
105    fn action(&self) -> Cow<'static, str> {
106        Cow::Borrowed("DescribeProjects")
107    }
108
109    fn version(&self) -> Cow<'static, str> {
110        Cow::Borrowed("2018-08-13")
111    }
112
113    fn payload(&self) -> Value {
114        let payload = DescribeProjectsPayload {
115            all_list: self.all_list,
116            limit: self.limit,
117            offset: self.offset,
118        };
119        serde_json::to_value(payload).expect("serialize DescribeProjects payload")
120    }
121}
122
123/// List Tag service projects asynchronously via `DescribeProjects`.
124///
125/// # Tencent Cloud Reference
126/// | Item | Value |
127/// |------|-------|
128/// | Service | `tag` |
129/// | Action | `DescribeProjects` |
130/// | Version | `2018-08-13` |
131/// | Rate Limit | 20 req/s |
132///
133/// Returns [`DescribeProjectsResponse`].
134pub async fn describe_projects_async(
135    client: &TencentCloudAsync,
136    request: &DescribeProjects,
137) -> TencentCloudResult<DescribeProjectsResponse> {
138    client.request(request).await
139}
140
141/// List Tag service projects with the blocking client.
142///
143/// Behaviour and parameters match [`describe_projects_async`].
144pub fn describe_projects_blocking(
145    client: &TencentCloudBlocking,
146    request: &DescribeProjects,
147) -> TencentCloudResult<DescribeProjectsResponse> {
148    client.request(request)
149}
150
151#[cfg(test)]
152mod tests {
153    use super::*;
154
155    #[test]
156    fn build_payload_with_defaults() {
157        let request = DescribeProjects {
158            all_list: Some(1),
159            limit: Some(1000),
160            offset: Some(0),
161        };
162        let payload = request.payload();
163        assert_eq!(payload["AllList"], serde_json::json!(1));
164        assert_eq!(payload["Limit"], serde_json::json!(1000));
165        assert_eq!(payload["Offset"], serde_json::json!(0));
166    }
167
168    #[test]
169    fn describe_projects_builder_configures_fields() {
170        let request = DescribeProjects::new()
171            .include_all(true)
172            .with_limit(50)
173            .with_offset(5);
174
175        assert_eq!(request.all_list, Some(1));
176        assert_eq!(request.limit, Some(50));
177        assert_eq!(request.offset, Some(5));
178    }
179
180    #[test]
181    fn deserialize_projects_response() {
182        let payload = r#"{
183            "Response": {
184                "TotalCount": 2,
185                "ProjectSet": [
186                    { "ProjectId": 1, "Name": "sample" },
187                    { "ProjectId": 2, "Name": "test" }
188                ],
189                "RequestId": "req-123"
190            }
191        }"#;
192        let parsed: DescribeProjectsResponse = serde_json::from_str(payload).unwrap();
193        assert_eq!(parsed.response.total_count, Some(2));
194    }
195}