volcengine_rust_sdk/service/vpc/api_describe_vpcs_model.rs
1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-10-29 17:33:27
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-06 14:35:15
6 * @Description: API for describing VPCs.
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::vpc_vpc;
13
14/// Implementation of the `ApiRequest` trait for the `DescribeVpcsReq` structure.
15/// This implementation provides the necessary methods to convert the request into a format suitable for sending over HTTP.
16impl request::ApiRequest for vpc_vpc::DescribeVpcsReq {
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 `DescribeVpcsResp` structure.
32/// This implementation provides the necessary methods to parse the HTTP response into a structured response object.
33impl response::ApiResponse for vpc_vpc::DescribeVpcsResp {
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 // Convert the response body to a text string.
44 let txt_response = http_response
45 .text()
46 .await
47 .map_err(|e| error::Error::ErrParseResponse(e))?;
48
49 // Sanitize the response text by replacing `null` values with empty arrays.
50 // This is necessary because the response may contain `null` values that need to be handled properly.
51 let sanitized_response = txt_response
52 .replace("\"SecondaryCidrBlocks\":null", "\"SecondaryCidrBlocks\":[]") // Replace `null` with an empty array
53 .replace("\"DnsServers\":null", "\"DnsServers\":[]");
54
55 // Parse the sanitized JSON response body into the expected structure.
56 let parsed_response: volcengine_sdk_protobuf::protobuf::vpc_vpc::DescribeVpcsResp =
57 serde_json::from_str(&sanitized_response).map_err(|e| error::Error::ErrParseJson(e))?;
58
59 // Update the current response object with the parsed data.
60 *self = parsed_response;
61
62 // Return successfully if no errors occurred.
63 Ok(())
64 }
65}