volcengine_rust_sdk/service/iam/
api_get_project_model.rs

1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-10-25 15:10:33
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2025-02-05 18:28:48
6 * @Description: create login profile 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::iam_project;
13
14/// Implementation of the `ApiRequest` trait for the `GetProjectReq` struct.
15/// This implementation enables the `GetProjectReq` struct to be transformed into a format suitable for API requests.
16impl request::ApiRequest for iam_project::GetProjectReq {
17    /// Converts the `GetProjectReq` instance into a hashmap of string key - value pairs.
18    /// This hashmap can be used to construct query parameters or form data for an API request.
19    ///
20    /// # Returns
21    /// - A `HashMap<String, String>` representing the formatted request data.
22    fn to_hashmap(&self) -> HashMap<String, String> {
23        request::Request::format_request_to_hashmap(self)
24    }
25
26    /// Converts the `GetProjectReq` instance into a byte vector representing the request body.
27    /// In this case, an empty vector is returned, indicating that there is no specific body data for this request.
28    ///
29    /// # Returns
30    /// - A `Vec<u8>` representing the request body.
31    fn to_body(&self) -> Vec<u8> {
32        let result = Vec::new();
33        result
34    }
35}
36
37/// Implementation of the `ApiResponse` trait for the `GetProjectResp` struct.
38/// This implementation is responsible for converting an HTTP response into the `GetProjectResp` struct
39/// and handling error cases by updating the response metadata accordingly.
40impl response::ApiResponse for iam_project::GetProjectResp {
41    /// Converts an HTTP response into the `GetProjectResp` struct.
42    /// It first retrieves the HTTP status code, parses the JSON response body,
43    /// updates the current `GetProjectResp` instance with the parsed data,
44    /// and if the request was not successful, it updates the response metadata with the error information.
45    ///
46    /// # Arguments
47    /// - `&mut self`: A mutable reference to the `GetProjectResp` instance.
48    /// - `http_response`: A `reqwest::Response` containing the HTTP response data.
49    ///
50    /// # Returns
51    /// - On success, returns `Ok(())`.
52    /// - On error, returns an `Error` struct indicating the reason for the failure, such as a parsing error.
53    async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
54        // Get the HTTP status code of the response
55        let http_status = http_response.status();
56
57        // Parse the JSON response body into a `GetProjectResp` struct.
58        // If the parsing fails, map the error to an `ErrParseResponse` error type.
59        let parsed_response: volcengine_sdk_protobuf::protobuf::iam_project::GetProjectResp =
60            http_response
61                .json()
62                .await
63                .map_err(error::Error::ErrParseResponse)?;
64
65        // Update the current `GetProjectResp` instance with the parsed response data.
66        *self = parsed_response;
67
68        // Check if the HTTP request was not successful (status code is not in the 200 - 299 range).
69        if !http_status.is_success() {
70            // Check if the `response_metadata` field exists in the current `GetProjectResp` instance.
71            if let Some(mut response_metadata) = self.response_metadata.take() {
72                // Ensure that the `error` field in the `response_metadata` exists.
73                // If it doesn't, insert a default `ResponseMetadataErr` struct.
74                let response_metadata_error = response_metadata.error.get_or_insert_with(
75                    volcengine_sdk_protobuf::protobuf::iam_project::ResponseMetadataErr::default,
76                );
77
78                // Set the `code_n` field in the `response_metadata_error` struct
79                // to the HTTP status code.
80                response_metadata_error.code_n = Some(http_status.as_u16().into());
81
82                // Update the `response_metadata` field in the current `GetProjectResp` instance.
83                self.response_metadata = Some(response_metadata);
84            }
85        }
86
87        // Return `Ok(())` to indicate successful conversion of the HTTP response to the struct.
88        Ok(())
89    }
90}