volcengine_rust_sdk/service/redis/
api_enable_sharded_cluster_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:40
6 * @Description: API for decreasing the node number of a Redis database instance.
7 * This module provides the necessary implementations to handle requests and responses for decreasing the node number
8 * of a Redis database instance in the Volcengine environment. It includes traits implementations for converting requests
9 * to HTTP-compatible formats and parsing HTTP responses into structured data.
10 */
11use crate::volcengine::error::error;
12use crate::volcengine::request::{request, response};
13use std::collections::HashMap;
14use volcengine_sdk_protobuf::protobuf::redis_instance;
15
16/// Implementation of the `ApiRequest` trait for the `DecreaseDbInstanceNodeNumberReq` structure.
17/// This implementation allows the `DecreaseDbInstanceNodeNumberReq` to be converted into a format
18/// that can be sent as an HTTP request. It provides methods to generate headers and serialize the request body.
19impl request::ApiRequest for redis_instance::RedisEnableShardedClusterReq {
20    /// Converts the request into a `HashMap` of headers and query parameters.
21    /// This method is responsible for formatting the request's data into a `HashMap`
22    /// where keys are parameter names and values are their corresponding values.
23    /// These parameters will be used as query parameters in the HTTP request.
24    /// In this implementation, an empty `HashMap` is returned, which might need to be adjusted
25    /// based on the actual requirements of the API.
26    ///
27    /// # Returns
28    /// * `HashMap<String, String>` - A map containing the request parameters as query parameters.
29    fn to_hashmap(&self) -> HashMap<String, String> {
30        // Currently, this implementation returns an empty HashMap.
31        // This might need to be adjusted based on the actual API requirements.
32        HashMap::new()
33    }
34
35    /// Serializes the request into a byte vector to be sent as the request body.
36    /// This method takes the current request object and serializes it into a JSON byte vector.
37    /// The serialized data will be used as the body of the HTTP request.
38    /// If the serialization process fails, it will panic because the `unwrap` method is used.
39    ///
40    /// # Returns
41    /// * `Vec<u8>` - A byte vector representing the serialized request body.
42    fn to_body(&self) -> Vec<u8> {
43        // Serialize the request object into a JSON byte vector.
44        // The `unwrap` method is used here to panic if serialization fails.
45        // In a production environment, it is recommended to handle errors more gracefully.
46        serde_json::to_vec(self).unwrap()
47    }
48}
49
50/// Implementation of the `ApiResponse` trait for the `DecreaseDbInstanceNodeNumberResp` structure.
51/// This implementation enables the `DecreaseDbInstanceNodeNumberResp` to parse an HTTP response
52/// into a structured response object. It provides a method to deserialize the JSON body of the HTTP response
53/// and update the current response object with the parsed data.
54impl response::ApiResponse for redis_instance::RedisEnableShardedClusterResp {
55    /// Deserializes the HTTP response into a structured response object.
56    /// This asynchronous method takes an `http_response` object, which represents the HTTP response received from the server.
57    /// It attempts to parse the JSON body of the response into a `DecreaseDbInstanceNodeNumberResp` object.
58    /// If the parsing is successful, it updates the current response object with the parsed data.
59    ///
60    /// # Arguments
61    /// * `http_response` - The HTTP response object received from the server.
62    ///
63    /// # Returns
64    /// * `Result<(), error::Error>` - Returns `Ok(())` if the response is successfully parsed,
65    /// or an `error::Error` if parsing fails. The error type `ErrParseResponse` is used to indicate
66    /// a failure during the JSON parsing process.
67    async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
68        // Get the HTTP status code from the response.
69        let http_status = http_response.status();
70
71        // Parse the JSON response body into the expected structure.
72        let parsed_response: volcengine_sdk_protobuf::protobuf::redis_instance::RedisEnableShardedClusterResp =
73            http_response
74                .json()
75                .await
76                .map_err(|e| error::Error::ErrParseResponse(e))?;
77
78        // Update the current response object with the parsed data.
79        *self = parsed_response;
80
81        // Check if the HTTP status code indicates an error.
82        if !http_status.is_success() {
83            // Check if `response_metadata` exists in the response.
84            if let Some(mut response_metadata) = self.response_metadata.take() {
85                // Ensure the `error` field exists in `response_metadata`.
86                let response_metadata_error = response_metadata.error.get_or_insert_with(
87                    volcengine_sdk_protobuf::protobuf::redis_instance::ResponseMetadataErr::default,
88                );
89
90                // Set the `code_n` field to the HTTP status code.
91                response_metadata_error.code_n = Some(http_status.as_u16().into());
92
93                // Update `response_metadata` with the error information.
94                self.response_metadata = Some(response_metadata);
95            }
96        }
97
98        // Return successfully if no errors occurred.
99        Ok(())
100    }
101}