volcengine_rust_sdk/service/ecs/api_run_instances_model.rs
1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-10-28 17:13:35
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-05 16:43:08
6 * @Description: API model for running instances in ECS (Elastic Compute Service)
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_instance;
13
14// Implementation of the ApiRequest trait for the RunInstancesReq struct.
15// This allows the struct to be used as a request object in API calls to create new ECS instances.
16impl request::ApiRequest for ecs_instance::RunInstancesReq {
17 /// Converts the request into a HashMap of key-value pairs for use in HTTP requests.
18 /// This method is typically used when sending the request to the API server to start new instances.
19 fn to_hashmap(&self) -> HashMap<String, String> {
20 request::Request::format_request_to_hashmap(self)
21 }
22
23 /// Prepares the request body as a byte vector.
24 /// For this request, the body is an empty vector when using GET method.
25 /// However, in practice, this request may use POST method with a body containing instance configurations.
26 fn to_body(&self) -> Vec<u8> {
27 Vec::new()
28 // request::Request::format_request_to_body(self)
29 }
30}
31
32// Implementation of the ApiResponse trait for the RunInstancesResp struct.
33// This allows the struct to parse HTTP responses and convert them into a structured object containing information about the newly created instances.
34impl response::ApiResponse for ecs_instance::RunInstancesResp {
35 /// Converts the HTTP response into a structured response object.
36 /// This method handles parsing the JSON response from the API server and updating the current object with the parsed data.
37 async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
38 // Parse the JSON response from the HTTP response.
39 // The `json()` method deserializes the response body into the target type.
40 let parsed_response: volcengine_sdk_protobuf::protobuf::ecs_instance::RunInstancesResp =
41 http_response
42 .json()
43 .await
44 .map_err(|e| error::Error::ErrParseResponse(e))?; // Handle JSON parsing errors
45
46 // Replace the current `self` with the parsed response.
47 *self = parsed_response;
48
49 // Return Ok(()) to indicate successful parsing.
50 Ok(())
51 }
52}