use crate::volcengine::client::client_info;
use crate::volcengine::client::config;
use crate::volcengine::error::error;
use crate::volcengine::request::handles;
use crate::volcengine::request::operation;
use crate::volcengine::request::request;
use crate::volcengine::request::send;
use crate::volcengine::request::send::SendRequest;
use std::collections::HashMap;
use std::future::Future;
pub trait ApiRequest: Send {
fn to_hashmap(&self) -> HashMap<String, String>;
fn to_body(&self) -> Vec<u8>;
}
pub trait RequestVolcengine {
fn format_request_to_hashmap<T: serde::Serialize>(request: &T) -> HashMap<String, String>;
fn send<T: request::ApiRequest>(
&self,
request: T,
) -> impl Future<Output = Result<reqwest::Response, error::Error>>;
}
#[derive(Debug, Clone)]
#[allow(dead_code)] pub struct Request {
pub config: config::Config,
pub client_info: client_info::ClientInfo,
pub handles: handles::Handles,
pub operation: operation::Operation,
}
impl Request {
pub fn builder() -> RequestBuilder {
RequestBuilder {
config: None,
client_info: None,
handles: None,
operation: None,
}
}
}
impl RequestVolcengine for Request {
fn format_request_to_hashmap<T: serde::Serialize>(request: &T) -> HashMap<String, String> {
let value = serde_json::to_value(request).unwrap();
let map = value.as_object().unwrap();
let mut request_hashmap: HashMap<String, String> = HashMap::new();
for (k, v) in map.iter() {
if v.is_null() || v == "" || v == "null" {
continue;
}
if v.is_array() {
if let Some(arr) = v.as_array() {
if !arr.is_empty() {
for (i, x) in arr.iter().enumerate() {
if x.is_object() {
if let Some(mapx) = x.as_object() {
for (mapx_key, mapx_value) in mapx.iter() {
if mapx_value.is_null()
|| mapx_value == ""
|| mapx_value == "null"
{
continue;
}
if mapx_value.is_array() {
if let Some(mapx_value_arr) = mapx_value.as_array() {
if !mapx_value_arr.is_empty() {
for (
mapx_value_arr_key,
mapx_value_arr_value,
) in mapx_value_arr.iter().enumerate()
{
if mapx_value_arr_value.is_null()
|| mapx_value_arr_value == ""
|| mapx_value_arr_value == "null"
{
continue;
}
request_hashmap.insert(
format!(
"{}.{}.{}.{}",
k.clone(),
i + 1,
mapx_key,
mapx_value_arr_key + 1
),
mapx_value_arr_value
.to_string()
.replace("\"", ""),
);
}
}
}
continue;
}
request_hashmap.insert(
format!("{}.{}.{}", k.clone(), i + 1, mapx_key),
mapx_value.to_string().replace("\"", ""),
);
}
}
continue;
}
if !x.is_null() {
request_hashmap.insert(
format!("{}.{}", k, i + 1),
x.to_string().replace("\"", ""),
);
}
}
}
}
} else {
if k == "PolicyDocument" {
let mut policy_document = v.to_string().replace("\\", "");
policy_document = policy_document[1..policy_document.len() - 1].to_string();
request_hashmap.insert(k.clone(), policy_document);
continue;
}
if k == "NewPolicyDocument" {
let mut policy_document = v.to_string().replace("\\", "");
policy_document = policy_document[1..policy_document.len() - 1].to_string();
request_hashmap.insert(k.clone(), policy_document);
continue;
}
request_hashmap.insert(k.clone(), v.to_string().replace("\"", ""));
}
}
request_hashmap
}
async fn send<T: request::ApiRequest>(
&self,
request: T,
) -> Result<reqwest::Response, error::Error> {
send::Send::set_request(self).send(&request).await
}
}
pub struct RequestBuilder {
pub config: Option<config::Config>, pub client_info: Option<client_info::ClientInfo>, pub handles: Option<handles::Handles>, pub operation: Option<operation::Operation>, }
impl RequestBuilder {
pub fn with_config(mut self, config: &config::Config) -> Self {
self.config = Some(config.clone());
self
}
pub fn with_client_info(mut self, client_info: &client_info::ClientInfo) -> Self {
self.client_info = Some(client_info.clone());
self
}
pub fn with_handles(mut self, handles: &handles::Handles) -> Self {
self.handles = Some(handles.clone());
self
}
pub fn with_operation(mut self, operation: &operation::Operation) -> Self {
self.operation = Some(operation.clone());
self
}
pub fn build(self) -> Result<Request, error::Error> {
if self.config.is_none() {
return Err(error::Error::ErrUtilRequestBuildRequestNo(
"config".to_string(),
));
}
if self.client_info.is_none() {
return Err(error::Error::ErrUtilRequestBuildRequestNo(
"client_info".to_string(),
));
}
if self.handles.is_none() {
return Err(error::Error::ErrUtilRequestBuildRequestNo(
"handles".to_string(),
));
}
if self.operation.is_none() {
return Err(error::Error::ErrUtilRequestBuildRequestNo(
"operation".to_string(),
));
}
Ok(Request {
config: self.config.unwrap(),
client_info: self.client_info.unwrap(),
handles: self.handles.unwrap(),
operation: self.operation.unwrap(),
})
}
}