volcengine_rust_sdk/service/rds/
api_describe_db_instances_models.rs

1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-11-05 10:39:54
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-06 11:26:18
6 * @Description: Implementation of request and response handling for describing RDS instances.
7 */
8use crate::volcengine::error::error;
9use crate::volcengine::request::{request, response};
10use std::collections::HashMap;
11use volcengine_sdk_protobuf::protobuf::rds_instance;
12
13/// Implementation of the `ApiRequest` trait for the `DescribeDbInstancesReq` 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_instance::DescribeDbInstancesReq {
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    /// The `Request::format_request_to_hashmap` method is commented out as it is not used in this implementation.
19    fn to_hashmap(&self) -> HashMap<String, String> {
20        // request::Request::format_request_to_hashmap(self)
21        HashMap::new()
22    }
23
24    /// Serializes the request into a byte vector to be sent as the request body.
25    /// This method uses `serde_json` to serialize the request into a JSON format, which is then converted into a byte vector.
26    /// If serialization fails, it panics. In a production environment, this should be handled more gracefully.
27    fn to_body(&self) -> Vec<u8> {
28        serde_json::to_vec(self).unwrap()
29    }
30}
31
32/// Implementation of the `ApiResponse` trait for the `DescribeDbInstancesResp` structure.
33/// This implementation provides the necessary methods to parse the HTTP response into a structured response object.
34impl response::ApiResponse for rds_instance::DescribeDbInstancesResp {
35    /// Deserializes the HTTP response into a structured response object.
36    /// This method takes an `http_response` object, parses its JSON body, and updates the current response object with the parsed data.
37    ///
38    /// # Arguments
39    /// * `http_response` - The HTTP response object received from the server.
40    ///
41    /// # Returns
42    /// * `Result<(), error::Error>` - Returns `Ok(())` if the response is successfully parsed, or an error if parsing fails.
43    async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
44        // Get the HTTP status code from the response.
45        let http_status = http_response.status();
46
47        // Parse the JSON response body into the expected structure.
48        let parsed_response: volcengine_sdk_protobuf::protobuf::rds_instance::DescribeDbInstancesResp =
49            http_response
50                .json()
51                .await
52                .map_err(error::Error::ErrParseResponse)?;
53
54        // Update the current response object with the parsed data.
55        *self = parsed_response;
56
57        // Check if the HTTP status code indicates an error.
58        if !http_status.is_success() {
59            // Check if `response_metadata` exists in the response.
60            if let Some(mut response_metadata) = self.response_metadata.take() {
61                // Ensure the `error` field exists in `response_metadata`.
62                let response_metadata_error = response_metadata.error.get_or_insert_with(
63                    volcengine_sdk_protobuf::protobuf::rds_instance::ResponseMetadataErr::default,
64                );
65
66                // Set the `code_n` field to the HTTP status code.
67                response_metadata_error.code_n = Some(http_status.as_u16().into());
68
69                // Update `response_metadata` with the error information.
70                self.response_metadata = Some(response_metadata);
71            }
72        }
73
74        // Return successfully if no errors occurred.
75        Ok(())
76    }
77}