tencent_sdk/services/dns/
delete.rs

1use crate::{
2    client::{TencentCloudAsync, TencentCloudBlocking},
3    core::{Endpoint, TencentCloudResult},
4};
5use serde::Deserialize;
6use serde_json::{json, Value};
7use std::borrow::Cow;
8
9#[derive(Debug, Deserialize)]
10pub struct DeleteRecordResponse {
11    #[serde(rename = "Response")]
12    pub response: DeleteRecordResult,
13}
14
15#[derive(Debug, Deserialize)]
16pub struct DeleteRecordResult {
17    #[serde(rename = "RequestId")]
18    pub request_id: String,
19}
20
21/// Request parameters for deleting a record.
22pub struct DeleteRecord<'a> {
23    pub domain: &'a str,
24    pub record_id: u64,
25    pub domain_id: Option<u64>,
26}
27
28impl<'a> DeleteRecord<'a> {
29    pub fn new(domain: &'a str, record_id: u64) -> Self {
30        Self {
31            domain,
32            record_id,
33            domain_id: None,
34        }
35    }
36
37    pub fn with_domain_id(mut self, domain_id: u64) -> Self {
38        self.domain_id = Some(domain_id);
39        self
40    }
41}
42
43impl<'a> Endpoint for DeleteRecord<'a> {
44    type Output = DeleteRecordResponse;
45
46    fn service(&self) -> Cow<'static, str> {
47        Cow::Borrowed("dnspod")
48    }
49
50    fn action(&self) -> Cow<'static, str> {
51        Cow::Borrowed("DeleteRecord")
52    }
53
54    fn version(&self) -> Cow<'static, str> {
55        Cow::Borrowed("2021-03-23")
56    }
57
58    fn region(&self) -> Option<Cow<'_, str>> {
59        None
60    }
61
62    fn payload(&self) -> Value {
63        let mut payload = json!({
64            "Domain": self.domain,
65            "RecordId": self.record_id,
66        });
67
68        if let Some(domain_id) = self.domain_id {
69            payload["DomainId"] = json!(domain_id);
70        }
71
72        payload
73    }
74}
75
76/// Call DNSPod `DeleteRecord` with the async client.
77pub async fn delete_record_async(
78    client: &TencentCloudAsync,
79    request: &DeleteRecord<'_>,
80) -> TencentCloudResult<DeleteRecordResponse> {
81    client.request(request).await
82}
83
84/// Call DNSPod `DeleteRecord` with the blocking client.
85pub fn delete_record_blocking(
86    client: &TencentCloudBlocking,
87    request: &DeleteRecord<'_>,
88) -> TencentCloudResult<DeleteRecordResponse> {
89    client.request(request)
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn test_delete_record_payload() {
98        let request = DeleteRecord::new("example.com", 123).with_domain_id(456);
99
100        let payload = request.payload();
101        assert_eq!(payload["Domain"], json!("example.com"));
102        assert_eq!(payload["RecordId"], json!(123));
103        assert_eq!(payload["DomainId"], json!(456));
104    }
105
106    #[test]
107    fn test_deserialize_delete_response() {
108        let json = r#"{
109            "Response": {
110                "RequestId": "req-789012"
111            }
112        }"#;
113
114        let response: DeleteRecordResponse =
115            serde_json::from_str(json).expect("deserialize DeleteRecordResponse");
116        assert_eq!(response.response.request_id, "req-789012");
117    }
118
119    #[test]
120    fn test_endpoint_implementation() {
121        let delete_request = DeleteRecord::new("test.com", 123);
122        assert_eq!(delete_request.service().as_ref(), "dnspod");
123        assert_eq!(delete_request.action().as_ref(), "DeleteRecord");
124        assert_eq!(delete_request.version().as_ref(), "2021-03-23");
125        assert!(delete_request.region().is_none());
126    }
127}