volcengine_rust_sdk/volcengine/request/response.rs
1/*
2 * @Author: Jerry.Yang
3 * @Date: 2024-10-21 18:19:47
4 * @LastEditors: Jerry.Yang
5 * @LastEditTime: 2024-11-08 10:49:53
6 * @Description: response module for handling API responses
7 */
8use crate::volcengine::error::error;
9use serde::{Deserialize, Serialize};
10use std::future::Future;
11
12/// Trait to handle API responses
13///
14/// This trait defines the method to convert an HTTP response into a struct.
15pub trait ApiResponse {
16 /// Converts the given `reqwest::Response` into a struct implementing `ApiResponse`
17 ///
18 /// # Arguments
19 /// - `http_response`: The HTTP response received from an API request
20 ///
21 /// # Returns
22 /// Returns a `Future` that resolves to a `Result` indicating success or failure.
23 fn to_struct(
24 &mut self,
25 http_response: reqwest::Response,
26 ) -> impl Future<Output = Result<(), error::Error>>;
27}
28
29/// Metadata structure for API responses
30///
31/// This struct holds essential metadata details from the API response.
32#[derive(Serialize, Deserialize, Debug, Clone, Default)]
33pub struct ApiResponseMetadata {
34 /// Unique request ID assigned to the request
35 #[serde(rename = "RequestId")]
36 pub request_id: String,
37
38 /// The name of the action performed by the API
39 #[serde(rename = "Action")]
40 pub action: String,
41
42 /// The version of the API used for the request
43 #[serde(rename = "Version")]
44 pub version: String,
45
46 /// The service that processed the request
47 #[serde(rename = "Service")]
48 pub service: String,
49
50 /// The region where the request was processed
51 #[serde(rename = "Region")]
52 pub region: String,
53
54 /// Optional error data if the request failed
55 #[serde(rename = "Error")]
56 pub error: Option<ApiResponseMetadataErrData>,
57}
58
59/// Structure to hold error details in API responses
60///
61/// This struct captures error details such as error code and message.
62#[derive(Serialize, Deserialize, Debug, Clone, Default)]
63pub struct ApiResponseMetadataErrData {
64 /// Numeric error code (if available)
65 #[serde(rename = "CodeN")]
66 pub code_no: Option<i64>,
67
68 /// Error code string
69 #[serde(rename = "Code")]
70 pub code: String,
71
72 /// Error message describing the failure
73 #[serde(rename = "Message")]
74 pub message: String,
75}