volcengine_rust_sdk/service/rds/api_create_db_endpoint_model.rs
1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-11-05 10:39:54
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-06 10:44:45
6 * @Description: api create db endpoint model
7 */
8use crate::volcengine::error::error;
9use crate::volcengine::request::{request, response};
10use std::collections::HashMap;
11use volcengine_sdk_protobuf::protobuf::rds_endpoint;
12
13/// Implements the `ApiRequest` trait for the `CreateDbEndpointReq` struct.
14/// This implementation enables the `CreateDbEndpointReq` struct to be transformed
15/// into a format appropriate for making API requests. Specifically, it offers
16/// methods to convert the struct into a hashmap for query parameters and a byte
17/// vector for the request body.
18impl request::ApiRequest for rds_endpoint::CreateDbEndpointReq {
19 /// Converts the `CreateDbEndpointReq` instance into a hashmap of string key - value pairs.
20 /// This hashmap can be utilized to construct the query parameters for an API request.
21 /// Currently, the implementation returns an empty hashmap since the actual
22 /// formatting logic is commented out.
23 ///
24 /// # Returns
25 /// - A `HashMap<String, String>` representing the formatted request data (empty in this case).
26 fn to_hashmap(&self) -> HashMap<String, String> {
27 // Uncomment this line to format the request into a hashmap.
28 // request::Request::format_request_to_hashmap(self)
29 return HashMap::new();
30 }
31
32 /// Converts the `CreateDbEndpointReq` instance into a byte vector representing the request body.
33 /// It serializes the struct into a JSON byte vector. Note that if the serialization fails,
34 /// the program will panic due to the use of `unwrap`.
35 ///
36 /// # Returns
37 /// - A `Vec<u8>` representing the serialized JSON data of the request body.
38 fn to_body(&self) -> Vec<u8> {
39 return serde_json::to_vec(self).unwrap();
40 }
41}
42
43/// Implements the `ApiResponse` trait for the `CreateDbEndpointResp` struct.
44/// This implementation is in charge of converting an HTTP response into the
45/// `CreateDbEndpointResp` struct. It parses the JSON response body and updates
46/// the current instance with the parsed data.
47impl response::ApiResponse for rds_endpoint::CreateDbEndpointResp {
48 /// Converts an HTTP response into the `CreateDbEndpointResp` struct.
49 /// First, it parses the JSON response body. Then, it updates the current
50 /// `CreateDbEndpointResp` instance with the parsed data. If there is an error
51 /// during parsing, it returns an `Error` struct.
52 ///
53 /// # Arguments
54 /// - `&mut self`: A mutable reference to the `CreateDbEndpointResp` instance.
55 /// - `http_response`: A `reqwest::Response` containing the HTTP response data.
56 ///
57 /// # Returns
58 /// - On success, returns `Ok(())`.
59 /// - On error, returns an `Error` struct indicating the reason for the failure,
60 /// such as a parsing error.
61 async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
62 // Parse the JSON response body into a `CreateDbEndpointResp` struct.
63 // If the parsing fails, map the error to an `ErrParseResponse` error type.
64 let parsed_response: volcengine_sdk_protobuf::protobuf::rds_endpoint::CreateDbEndpointResp =
65 http_response
66 .json()
67 .await
68 .map_err(|e| error::Error::ErrParseResponse(e))?;
69
70 // Update the current `CreateDbEndpointResp` instance with the parsed response data.
71 *self = parsed_response;
72
73 // Return `Ok(())` to indicate successful conversion of the HTTP response to the struct.
74 return Ok(());
75 }
76}