volcengine_rust_sdk/service/iam/api_get_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:25:26
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/// Implementation of the `ApiRequest` trait for the `GetPolicyReq` struct.
15/// This implementation allows the `GetPolicyReq` struct to be formatted into a hashmap
16/// and a request body for making API requests.
17impl request::ApiRequest for iam_policy::GetPolicyReq {
18 /// Converts the `GetPolicyReq` 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 `GetPolicyReq` 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 Vec::new()
35 }
36}
37
38/// Implementation of the `ApiResponse` trait for the `GetPolicyResp` struct.
39/// This implementation is responsible for converting an HTTP response into the `GetPolicyResp` struct
40/// and handling error cases by updating the response metadata if necessary.
41impl response::ApiResponse for iam_policy::GetPolicyResp {
42 /// Converts an HTTP response into the `GetPolicyResp` struct.
43 /// It first checks the HTTP status code, parses the JSON response body,
44 /// updates the current `GetPolicyResp` instance with the parsed data,
45 /// and if the request was not successful, it updates the response metadata with the error code.
46 ///
47 /// # Arguments
48 /// - `&mut self`: A mutable reference to the `GetPolicyResp` instance.
49 /// - `http_response`: A `reqwest::Response` containing the HTTP response data.
50 ///
51 /// # Returns
52 /// - On success, returns `Ok(())`.
53 /// - On error, returns an `Error` struct indicating the reason for the failure,
54 /// such as a parsing error.
55 async fn to_struct(&mut self, http_response: reqwest::Response) -> Result<(), error::Error> {
56 // Get the HTTP status code of the response
57 let http_status = http_response.status();
58
59 // Parse the JSON response body into a `GetPolicyResp` struct.
60 // If the parsing fails, map the error to an `ErrParseResponse` error type.
61 let parsed_response: volcengine_sdk_protobuf::protobuf::iam_policy::GetPolicyResp =
62 http_response
63 .json()
64 .await
65 .map_err(error::Error::ErrParseResponse)?;
66
67 // Update the current `GetPolicyResp` instance with the parsed response data.
68 *self = parsed_response;
69
70 // Check if the HTTP request was not successful (status code is not in the 200 - 299 range).
71 if !http_status.is_success() {
72 // Check if the `response_metadata` field exists in the current `GetPolicyResp` instance.
73 if let Some(mut response_metadata) = self.response_metadata.take() {
74 // Ensure that the `error` field in the `response_metadata` exists.
75 // If it doesn't, insert a default `ResponseMetadataErr` struct.
76 let response_metadata_error = response_metadata.error.get_or_insert_with(
77 volcengine_sdk_protobuf::protobuf::iam_policy::ResponseMetadataErr::default,
78 );
79
80 // Set the `code_n` field in the `response_metadata_error` struct
81 // to the HTTP status code.
82 response_metadata_error.code_n = Some(http_status.as_u16().into());
83
84 // Update the `response_metadata` field in the current `GetPolicyResp` instance.
85 self.response_metadata = Some(response_metadata);
86 }
87 }
88
89 // Return `Ok(())` to indicate successful conversion of the HTTP response to the struct.
90 Ok(())
91 }
92}