use reqwest::{Client, ClientBuilder};
use crate::common::utils::md5;
#[derive(Clone)]
pub enum Endpoint {
Auto,
Telecom,
Cnc,
Ctt,
}
impl Endpoint {
pub fn value(&self) -> &'static str {
match self {
Endpoint::Auto => "https://v0.api.upyun.com",
Endpoint::Telecom => "https://v1.api.upyun.com",
Endpoint::Cnc => "https://v2.api.upyun.com",
Endpoint::Ctt => "https://v3.api.upyun.com"
}
}
}
pub struct UpYun {
pub bucket: String,
pub operator: String,
pub password: String,
pub timeout: u64,
pub endpoint: Endpoint,
pub client: Client
}
pub struct UpyunBuilder {
bucket: Option<String>,
operator: Option<String>,
password: Option<String>,
timeout: Option<u64>,
endpoint: Option<Endpoint>,
danger_accept_invalid_certs: bool
}
impl UpYun {
pub fn builder() -> UpyunBuilder {
UpyunBuilder {
bucket: None,
operator: None,
password: None,
timeout: None,
endpoint: None,
danger_accept_invalid_certs: false
}
}
}
impl UpyunBuilder {
pub fn bucket(mut self, bucket: &str) -> Self {
self.bucket = Some(bucket.to_string());
self
}
pub fn operator(mut self, operator: &str) -> Self {
self.operator = Some(operator.to_string());
self
}
pub fn password(mut self, password: &str) -> Self {
self.password = Some(password.to_string());
self
}
pub fn timeout(mut self, timeout: u64) -> Self {
self.timeout = Some(timeout);
self
}
pub fn endpoint(mut self, endpoint: Endpoint) -> Self {
self.endpoint = Some(endpoint);
self
}
pub fn danger_accept_invalid_certs(mut self, danger_accept_invalid_certs: bool) -> Self {
self.danger_accept_invalid_certs = danger_accept_invalid_certs;
self
}
pub fn build(self) -> UpYun {
if self.bucket.is_none() {
panic!("Bucket is required.")
}
if self.operator.is_none() {
panic!("Operator is required.")
}
if self.password.is_none() {
panic!("Password is required.")
}
UpYun {
bucket: self.bucket.unwrap(),
operator: self.operator.unwrap(),
password: md5(self.password.unwrap()),
timeout: self.timeout.unwrap_or(30 * 1000),
endpoint: self.endpoint.unwrap_or(Endpoint::Auto),
client: ClientBuilder::new()
.danger_accept_invalid_certs(self.danger_accept_invalid_certs)
.build()
.unwrap()
}
}
}