volcengine_rust_sdk/service/iam/
api_get_user_model.rs

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