volcengine_rust_sdk/service/rds/
api_create_db_instance_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:14:44
6 * @Description: Implementation of request and response handling for creating an RDS instance.
7 */
8
9use crate::volcengine::error::error;
10use crate::volcengine::request::{request, response};
11use std::collections::HashMap;
12use volcengine_sdk_protobuf::protobuf::rds_instance;
13
14/// Implementation of the `ApiRequest` trait for the `CreateDbInstanceReq` structure.
15/// This implementation provides the necessary methods to convert the request into a format suitable for sending over HTTP.
16impl request::ApiRequest for rds_instance::CreateDbInstanceReq {
17    /// Converts the request into a `HashMap` of headers.
18    /// For this specific request, no additional headers are required, so an empty `HashMap` is returned.
19    fn to_hashmap(&self) -> HashMap<String, String> {
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    /// If serialization fails, it panics. In a production environment, this should be handled more gracefully.
26    fn to_body(&self) -> Vec<u8> {
27        serde_json::to_vec(self).unwrap()
28        // Alternatively, you could use the following method to handle errors:
29        // request::Request::format_request_to_body(self)
30    }
31}
32
33/// Implementation of the `ApiResponse` trait for the `CreateDbInstanceResp` structure.
34/// This implementation provides the necessary methods to parse the HTTP response into a structured response object.
35impl response::ApiResponse for rds_instance::CreateDbInstanceResp {
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        // Parse the JSON response body into the expected structure.
46        let parsed_response: volcengine_sdk_protobuf::protobuf::rds_instance::CreateDbInstanceResp =
47            http_response
48                .json()
49                .await
50                .map_err(|e| error::Error::ErrParseResponse(e))?;
51
52        // Update the current response object with the parsed data.
53        *self = parsed_response;
54
55        // Return successfully if no errors occurred.
56        Ok(())
57    }
58}