volcengine_rust_sdk/service/rds/
api_describe_db_databases_model.rs

1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-11-05 10:39:54
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-06 11:22:31
6 * @Description: Implementation of request and response handling for describing RDS databases.
7 */
8use crate::volcengine::error::error;
9use crate::volcengine::request::{request, response};
10use std::collections::HashMap;
11use volcengine_sdk_protobuf::protobuf::rds_database;
12
13/// Implementation of the `ApiRequest` trait for the `DescribeDatabasesReq` structure.
14/// This implementation provides the necessary methods to convert the request into a format suitable for sending over HTTP.
15impl request::ApiRequest for rds_database::DescribeDatabasesReq {
16    /// Converts the request into a `HashMap` of headers.
17    /// For this specific request, no additional headers are required, so an empty `HashMap` is returned.
18    fn to_hashmap(&self) -> HashMap<String, String> {
19        // request::Request::format_request_to_hashmap(self)
20        HashMap::new()
21    }
22
23    /// Serializes the request into a byte vector to be sent as the request body.
24    /// This method uses `serde_json` to serialize the request into a JSON format, which is then converted into a byte vector.
25    fn to_body(&self) -> Vec<u8> {
26        serde_json::to_vec(self).unwrap()
27    }
28}
29
30/// Implementation of the `ApiResponse` trait for the `DescribeDatabasesResp` structure.
31/// This implementation provides the necessary methods to parse the HTTP response into a structured response object.
32impl response::ApiResponse for rds_database::DescribeDatabasesResp {
33    /// Deserializes the HTTP response into a structured response object.
34    /// This method takes an `http_response` object, parses its JSON body, and updates the current response object with the parsed data.
35    ///
36    /// # Arguments
37    /// * `http_response` - The HTTP response object received from the server.
38    ///
39    /// # Returns
40    /// * `Result<(), error::Error>` - Returns `Ok(())` if the response is successfully parsed, or an error if parsing fails.
41    async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
42        // Get the HTTP status code from the response.
43        let http_status = http_response.status();
44
45        // Convert the response body to a text string.
46        let txt_response = http_response
47            .text()
48            .await
49            .map_err(|e| error::Error::ErrParseResponse(e))?;
50
51        // Sanitize the response text by replacing `null` values with empty arrays.
52        // This is necessary because the response may contain `null` values that need to be handled properly.
53        let sanitized_response =
54            txt_response.replace("\"DatabasePrivileges\":null", "\"DatabasePrivileges\":[]"); // Replace `null` with an empty array
55
56        // Parse the sanitized JSON response body into the expected structure.
57        let parsed_response: volcengine_sdk_protobuf::protobuf::rds_database::DescribeDatabasesResp =
58            serde_json::from_str(&sanitized_response).map_err(|e| error::Error::ErrParseJson(e))?;
59
60        // Update the current response object with the parsed data.
61        *self = parsed_response;
62
63        // Check if the HTTP status code indicates an error.
64        if !http_status.is_success() {
65            // Check if `response_metadata` exists in the response.
66            if let Some(mut response_metadata) = self.response_metadata.take() {
67                // Ensure the `error` field exists in `response_metadata`.
68                let response_metadata_error = response_metadata.error.get_or_insert_with(
69                    volcengine_sdk_protobuf::protobuf::rds_database::ResponseMetadataErr::default,
70                );
71
72                // Set the `code_n` field to the HTTP status code.
73                response_metadata_error.code_n = Some(http_status.as_u16().into());
74
75                // Update `response_metadata` with the error information.
76                self.response_metadata = Some(response_metadata);
77            }
78        }
79
80        // Return successfully if no errors occurred.
81        Ok(())
82    }
83}