volcengine_rust_sdk/service/rds/api_create_db_database_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:32:09
6 * @Description:
7 */
8use crate::volcengine::error::error;
9use crate::volcengine::request::{request, response};
10use std::collections::HashMap;
11use volcengine_sdk_protobuf::protobuf::rds_database;
12
13/// Implements the `ApiRequest` trait for the `CreateDatabaseReq` struct.
14/// This allows the `CreateDatabaseReq` struct to be formatted into a format
15/// suitable for making API requests, such as converting it to a hashmap for
16/// query parameters and a byte vector for the request body.
17impl request::ApiRequest for rds_database::CreateDatabaseReq {
18 /// Converts the `CreateDatabaseReq` instance into a hashmap of string key - value pairs.
19 /// This hashmap can be used to construct query parameters for an API request.
20 /// Currently, the implementation returns an empty hashmap as the actual formatting
21 /// logic is commented out.
22 ///
23 /// # Returns
24 /// - A `HashMap<String, String>` representing the request data in key - value pairs (empty here).
25 fn to_hashmap(&self) -> HashMap<String, String> {
26 // This line would format the request into a hashmap if uncommented.
27 // request::Request::format_request_to_hashmap(self)
28 HashMap::new()
29 }
30
31 /// Converts the `CreateDatabaseReq` instance into a byte vector representing the request body.
32 /// It serializes the struct into a JSON byte vector. If serialization fails,
33 /// the program will panic because `unwrap` is used.
34 ///
35 /// # Returns
36 /// - A `Vec<u8>` representing the serialized JSON data of the request body.
37 fn to_body(&self) -> Vec<u8> {
38 serde_json::to_vec(self).unwrap()
39 }
40}
41
42/// Implements the `ApiResponse` trait for the `CreateDatabaseResp` struct.
43/// This is responsible for converting an HTTP response into the `CreateDatabaseResp` struct
44/// and handling error cases by updating the response metadata if the request was not successful.
45impl response::ApiResponse for rds_database::CreateDatabaseResp {
46 /// Converts an HTTP response into the `CreateDatabaseResp` struct.
47 /// It first retrieves the HTTP status code, parses the JSON response body,
48 /// updates the current `CreateDatabaseResp` instance with the parsed data,
49 /// and if the request was not successful, it updates the response metadata with the error information.
50 ///
51 /// # Arguments
52 /// - `&mut self`: A mutable reference to the `CreateDatabaseResp` instance.
53 /// - `http_response`: A `reqwest::Response` containing the HTTP response data.
54 ///
55 /// # Returns
56 /// - On success, returns `Ok(())`.
57 /// - On error, returns an `Error` struct indicating the reason for the failure,
58 /// such as a parsing error.
59 async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
60 // Get the HTTP status code of the response.
61 let http_status = http_response.status();
62
63 // Parse the JSON response body into a `CreateDatabaseResp` struct.
64 // If the parsing fails, map the error to an `ErrParseResponse` error type.
65 let parsed_response: volcengine_sdk_protobuf::protobuf::rds_database::CreateDatabaseResp =
66 http_response
67 .json()
68 .await
69 .map_err(error::Error::ErrParseResponse)?;
70
71 // Update the current `CreateDatabaseResp` instance with the parsed response data.
72 *self = parsed_response;
73
74 // Check if the HTTP request was not successful (status code is not in the 200 - 299 range).
75 if !http_status.is_success() {
76 // Check if the `response_metadata` field exists in the current `CreateDatabaseResp` instance.
77 if let Some(mut response_metadata) = self.response_metadata.take() {
78 // Ensure that the `error` field in the `response_metadata` exists.
79 // If it doesn't, insert a default `ResponseMetadataErr` struct.
80 let response_metadata_error = response_metadata.error.get_or_insert_with(
81 volcengine_sdk_protobuf::protobuf::rds_database::ResponseMetadataErr::default,
82 );
83
84 // Set the `code_n` field in the `response_metadata_error` struct
85 // to the HTTP status code.
86 response_metadata_error.code_n = Some(http_status.as_u16().into());
87
88 // Update the `response_metadata` field in the current `CreateDatabaseResp` instance.
89 self.response_metadata = Some(response_metadata);
90 }
91 }
92
93 // Return `Ok(())` to indicate successful conversion of the HTTP response to the struct.
94 return Ok(());
95 }
96}