volcengine_rust_sdk/service/iam/
api_delete_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 17:28:49
6 * @Description: Model for deleting a user in IAM (Identity and Access Management)
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 DeleteUserReq struct
15// This allows the struct to be used as a request object in API calls to delete a user
16impl request::ApiRequest for volcengine_sdk_protobuf::protobuf::iam_user::DeleteUserReq {
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 delete a user
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 since it's a GET request
25    fn to_body(&self) -> Vec<u8> {
26        let result = Vec::new();
27        result
28    }
29}
30
31// Implementation of the ApiResponse trait for the DeleteUserResp struct
32// This allows the struct to parse HTTP responses and convert them into a structured object containing information about the deleted user
33impl response::ApiResponse for volcengine_sdk_protobuf::protobuf::iam_user::DeleteUserResp {
34    /// Converts the HTTP response into a structured response object
35    /// This method handles parsing the JSON response from the API server and updating the current object with the parsed data
36    /// It also checks the HTTP status code to determine if the request was successful
37    async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
38        // Get the HTTP status code
39        let http_status = http_response.status();
40
41        // Parse the JSON response body
42        let parsed_response: volcengine_sdk_protobuf::protobuf::iam_user::DeleteUserResp =
43            http_response
44                .json()
45                .await
46                .map_err(|e| error::Error::ErrParseResponse(e))?; // Handle JSON parsing errors
47
48        // Update the current object with the parsed response
49        *self = parsed_response;
50
51        // Check if the HTTP status is not successful
52        if !http_status.is_success() {
53            // Check if the `response_metadata` exists
54            if let Some(mut response_metadata) = self.response_metadata.take() {
55                // Ensure the `error` field exists
56                let response_metadata_error = response_metadata.error.get_or_insert_with(
57                    volcengine_sdk_protobuf::protobuf::iam_user::ResponseMetadataErr::default,
58                );
59
60                // Set the `code_n` field
61                response_metadata_error.code_n = Some(http_status.as_u16().into());
62
63                // Update the `response_metadata`
64                self.response_metadata = Some(response_metadata);
65            }
66        }
67
68        // Return Ok(()) to indicate successful parsing
69        Ok(())
70    }
71}