tencent_sdk/services/
tag.rs1use 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)]
10pub struct DescribeProjectsResponse {
16 #[serde(rename = "Response")]
17 pub response: DescribeProjectsResult,
18}
19
20#[derive(Debug, Deserialize)]
21pub 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
38pub 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 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
123pub async fn describe_projects_async(
135 client: &TencentCloudAsync,
136 request: &DescribeProjects,
137) -> TencentCloudResult<DescribeProjectsResponse> {
138 client.request(request).await
139}
140
141pub 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}