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