use serde::{de::DeserializeOwned, Serialize};
use serde_json::json;
use crate::error::Result;
use crate::utils::http::HttpClient;
use crate::utils::signature::SignatureBuilder;
pub struct TencentCloudClient {
secret_id: String,
secret_key: String,
http_client: HttpClient,
}
impl TencentCloudClient {
pub fn new(secret_id: String, secret_key: String) -> Self {
Self {
secret_id,
secret_key,
http_client: HttpClient::new(),
}
}
pub async fn request<T, R>(&self, action: &str, params: &T, service: &str, version: &str, region: Option<&str>) -> Result<R>
where
T: Serialize,
R: DeserializeOwned,
{
let host = format!("{}.tencentcloudapi.com", service);
let mut json_params = serde_json::to_value(params).unwrap_or(json!({}));
if !json_params.is_object() {
json_params = json!({});
}
let payload = json_params.to_string();
let mut builder = SignatureBuilder::new(
self.secret_id.clone(),
self.secret_key.clone(),
service.to_string(),
host,
)
.action(action.to_string())
.version(version.to_string())
.payload(payload);
if let Some(region_value) = region {
builder = builder.region(region_value.to_string());
}
let signature_info = builder.build();
self.http_client.send_request(signature_info).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_client() {
let client = TencentCloudClient::new("test_id".to_string(), "test_key".to_string());
assert_eq!(client.secret_id, "test_id");
assert_eq!(client.secret_key, "test_key");
}
}