use std::time::Duration;
use hyper::Request;
use hyper::{client::HttpConnector, Body, Client, Method, Response};
use hyper_rustls::HttpsConnector;
use crate::api::TranslateMethods;
pub struct TencentClient<S> {
pub client: Client<S>,
pub credential: Credential,
pub user_agent: String,
}
pub struct Credential {
pub id: String,
pub key: String,
}
impl<'a, S> TencentClient<S> {
pub fn new(client: Client<S>, credential: Credential) -> Self {
Self {
client,
credential,
user_agent: r#"Mozilla/5.0 Safari/537.36"#.to_string(),
}
}
pub fn translate(&'a self) -> TranslateMethods<'a, S> {
TranslateMethods { client: self }
}
}
impl TencentClient<HttpsConnector<HttpConnector>> {
pub fn native(credential: Credential) -> Self {
let tls_connector = hyper_rustls::HttpsConnectorBuilder::new()
.with_native_roots()
.https_only()
.enable_http1()
.enable_http2()
.build();
let client = Client::builder().build(tls_connector);
Self::new(client, credential)
}
}
pub trait Delegate: Send {
fn begin(&mut self, _info: MethodInfo) {}
fn http_failure(&mut self, _: &Response<Body>) -> Retry {
Retry::Abort
}
fn http_error(&mut self, _err: &hyper::Error) -> Retry {
Retry::Abort
}
fn pre_request(&mut self, _request: &Request<Body>) {}
fn retry_times(&self) -> u8 {
3
}
fn finished(&mut self, is_success: bool) {
let _ = is_success;
}
}
pub struct MethodInfo {
pub id: &'static str,
pub http_method: Method,
}
#[derive(Default)]
pub struct DefaultDelegate;
impl Delegate for DefaultDelegate {}
pub enum Retry {
Abort,
After(Duration),
}