volcengine_rust_sdk/service/ecs/
api_describe_images_models.rs

1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-10-29 10:25:09
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-05 14:51:06
6 * @Description: API to describe ECS images model
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::ecs_image;
13
14/// Implement the `ApiRequest` trait for `DescribeImagesReq` struct.
15///
16/// This implementation provides methods to convert the request object to a hashmap
17/// and to generate the body of the request as a byte vector. The `DescribeImagesReq`
18/// struct is used to describe the images in the ECS (Elastic Compute Service) context.
19///
20/// # Methods
21/// - `to_hashmap`: Converts the request struct into a `HashMap<String, String>`.
22/// - `to_body`: Converts the request into a body in byte format (currently returns an empty vector).
23impl request::ApiRequest for ecs_image::DescribeImagesReq {
24    /// Converts the `DescribeImagesReq` struct into a `HashMap<String, String>`.
25    ///
26    /// This method uses the `Request::format_request_to_hashmap` utility to format
27    /// the request struct into a hashmap for API request construction.
28    ///
29    /// # Returns
30    /// - `HashMap<String, String>`: A hashmap representing the request data, which can be used
31    ///   for building the query string for an API request.
32    fn to_hashmap(&self) -> HashMap<String, String> {
33        request::Request::format_request_to_hashmap(self)
34    }
35
36    /// Converts the `DescribeImagesReq` struct into the body of the request as a byte vector.
37    ///
38    /// This method currently returns an empty vector but can be customized to convert the struct
39    /// into a body in the future (e.g., for POST requests).
40    ///
41    /// # Returns
42    /// - `Vec<u8>`: The byte vector representing the body of the request. In this case, it's just an empty vector.
43    fn to_body(&self) -> Vec<u8> {
44        // The body is currently empty, but this method can be expanded to serialize
45        // the request struct into a byte vector (e.g., using `serde_json`).
46        Vec::new()
47    }
48}
49
50/// Implement the `ApiResponse` trait for `DescribeImagesResp` struct.
51///
52/// This implementation provides a method to process the HTTP response from an API call
53/// and convert it into the corresponding `DescribeImagesResp` struct. The response
54/// typically contains a list of ECS images, and this struct will hold that information.
55///
56/// # Methods
57/// - `to_struct`: Parses the HTTP response (JSON) and deserializes it into the `DescribeImagesResp` struct.
58impl response::ApiResponse for ecs_image::DescribeImagesResp {
59    /// Converts the HTTP response into the `DescribeImagesResp` struct.
60    ///
61    /// This method processes the HTTP response (which is expected to be in JSON format),
62    /// deserializes it, and sets the current struct (`self`) with the parsed data.
63    ///
64    /// The method uses `reqwest::Response::json` to parse the JSON body into the
65    /// `DescribeImagesResp` struct and handles any parsing errors by mapping them
66    /// to the `error::Error::ErrParseResponse` variant.
67    ///
68    /// # Arguments
69    /// - `http_response`: The HTTP response object received from the API.
70    ///
71    /// # Returns
72    /// - `Ok(())`: If the parsing is successful, the method sets `self` to the parsed data.
73    /// - `Err(error::Error)`: If there's a parsing error, the method returns an error.
74    async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
75        // Parse the JSON response body into `DescribeImagesResp` struct.
76        let parsed_response: volcengine_sdk_protobuf::protobuf::ecs_image::DescribeImagesResp =
77            http_response
78                .json()
79                .await
80                .map_err(|e| error::Error::ErrParseResponse(e))?;
81
82        // Set the current struct (`self`) with the parsed response data.
83        *self = parsed_response;
84
85        // Return `Ok(())` to indicate successful processing.
86        Ok(())
87    }
88}