volcengine_rust_sdk/service/iam/api_update_policy_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:49:13
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_policy;
13
14/// Implements the `ApiRequest` trait for the `UpdatePolicyReq` struct.
15/// This implementation allows the `UpdatePolicyReq` struct to be formatted
16/// into a format suitable for API requests, including a hashmap for query parameters
17/// and an empty request body in this case.
18impl request::ApiRequest for iam_policy::UpdatePolicyReq {
19 /// Converts the `UpdatePolicyReq` instance into a hashmap of string key - value pairs.
20 /// This hashmap can be used to construct query parameters for an API request.
21 ///
22 /// # Returns
23 /// - A `HashMap<String, String>` containing the formatted request data.
24 fn to_hashmap(&self) -> HashMap<String, String> {
25 // Use the utility function to format the request into a hashmap
26 request::Request::format_request_to_hashmap(self)
27 }
28
29 /// Converts the `UpdatePolicyReq` instance into a byte vector representing the request body.
30 /// In this implementation, an empty vector is returned, indicating that there is no
31 /// specific data to be sent in the request body for this type of request.
32 ///
33 /// # Returns
34 /// - A `Vec<u8>` representing the request body (empty in this case).
35 fn to_body(&self) -> Vec<u8> {
36 Vec::new()
37 }
38}
39
40/// Implements the `ApiResponse` trait for the `UpdatePolicyResp` struct.
41/// This implementation is responsible for converting an HTTP response into the `UpdatePolicyResp` struct
42/// and handling error cases by updating the response metadata when necessary.
43impl response::ApiResponse for iam_policy::UpdatePolicyResp {
44 /// Converts an HTTP response into the `UpdatePolicyResp` struct.
45 /// It first checks the HTTP status code, parses the JSON response body,
46 /// updates the current `UpdatePolicyResp` instance with the parsed data,
47 /// and if the request was not successful, it updates the response metadata with the error code.
48 ///
49 /// # Arguments
50 /// - `&mut self`: A mutable reference to the `UpdatePolicyResp` instance.
51 /// - `http_response`: A `reqwest::Response` containing the HTTP response data.
52 ///
53 /// # Returns
54 /// - On success, returns `Ok(())`.
55 /// - On error, returns an `Error` struct indicating the reason for the failure,
56 /// such as a parsing error.
57 async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
58 // Retrieve the HTTP status code from the response
59 let http_status = http_response.status();
60
61 // Parse the JSON response body into an `UpdatePolicyResp` struct.
62 // If the parsing fails, map the error to an `ErrParseResponse` error type.
63 let parsed_response: volcengine_sdk_protobuf::protobuf::iam_policy::UpdatePolicyResp =
64 http_response
65 .json()
66 .await
67 .map_err(|e| error::Error::ErrParseResponse(e))?;
68
69 // Update the current `UpdatePolicyResp` instance with the parsed response data.
70 *self = parsed_response;
71
72 // Check if the HTTP request was not successful (status code is not in the 200 - 299 range).
73 if !http_status.is_success() {
74 // Check if the `response_metadata` field exists in the current `UpdatePolicyResp` instance.
75 if let Some(mut response_metadata) = self.response_metadata.take() {
76 // Ensure that the `error` field in the `response_metadata` exists.
77 // If it doesn't, insert a default `ResponseMetadataErr` struct.
78 let response_metadata_error = response_metadata.error.get_or_insert_with(
79 volcengine_sdk_protobuf::protobuf::iam_policy::ResponseMetadataErr::default,
80 );
81
82 // Set the `code_n
83 response_metadata_error.code_n = Some(http_status.as_u16().into());
84
85 // Update `response_metadata`
86 self.response_metadata = Some(response_metadata);
87 }
88 }
89
90 // return
91 Ok(())
92 }
93}