tencent_sdk/services/ssl/
download.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 DownloadCertificateResponse {
11 #[serde(rename = "Response")]
12 pub response: DownloadCertificateResult,
13}
14
15#[derive(Debug, Deserialize)]
16pub struct DownloadCertificateResult {
17 #[serde(rename = "Content")]
18 pub content: Option<String>,
19 #[serde(rename = "ContentType")]
20 pub content_type: Option<String>,
21 #[serde(rename = "RequestId")]
22 pub request_id: String,
23}
24
25#[derive(Serialize)]
26#[serde(rename_all = "PascalCase")]
27struct DownloadCertificatePayload<'a> {
28 certificate_id: &'a str,
29}
30
31pub struct DownloadCertificate<'a> {
33 pub certificate_id: &'a str,
34}
35
36impl<'a> DownloadCertificate<'a> {
37 pub fn new(certificate_id: &'a str) -> Self {
39 Self { certificate_id }
40 }
41}
42
43impl<'a> Endpoint for DownloadCertificate<'a> {
44 type Output = DownloadCertificateResponse;
45
46 fn service(&self) -> Cow<'static, str> {
47 Cow::Borrowed("ssl")
48 }
49
50 fn action(&self) -> Cow<'static, str> {
51 Cow::Borrowed("DownloadCertificate")
52 }
53
54 fn version(&self) -> Cow<'static, str> {
55 Cow::Borrowed("2019-12-05")
56 }
57
58 fn region(&self) -> Option<Cow<'_, str>> {
59 None
61 }
62
63 fn payload(&self) -> Value {
64 serde_json::to_value(DownloadCertificatePayload {
65 certificate_id: self.certificate_id,
66 })
67 .expect("serialize DownloadCertificate payload")
68 }
69}
70
71pub async fn download_certificate_async(
73 client: &TencentCloudAsync,
74 request: &DownloadCertificate<'_>,
75) -> TencentCloudResult<DownloadCertificateResponse> {
76 client.request(request).await
77}
78
79pub fn download_certificate_blocking(
81 client: &TencentCloudBlocking,
82 request: &DownloadCertificate<'_>,
83) -> TencentCloudResult<DownloadCertificateResponse> {
84 client.request(request)
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90 use serde_json::json;
91
92 #[test]
93 fn download_certificate_payload_serialization() {
94 let request = DownloadCertificate::new("cert-xyz789");
95 let payload = request.payload();
96 assert_eq!(payload["CertificateId"], json!("cert-xyz789"));
97 }
98
99 #[test]
100 fn deserialize_download_certificate_response() {
101 let payload = r#"{
102 "Response": {
103 "Content": "UEsDBBQACAgIABdFg1YAAAAAAAAAAAAAAA...",
104 "ContentType": "application/zip",
105 "RequestId": "req-download-123"
106 }
107 }"#;
108 let parsed: DownloadCertificateResponse =
109 serde_json::from_str(payload).expect("deserialize DownloadCertificateResponse");
110 assert!(parsed
111 .response
112 .content
113 .as_deref()
114 .expect("content present")
115 .starts_with("UEsDBBQACAgIABdFg1Y"));
116 assert_eq!(
117 parsed.response.content_type,
118 Some("application/zip".to_string())
119 );
120 assert_eq!(parsed.response.request_id, "req-download-123");
121 }
122}