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