1use reqwest::{Client, ClientBuilder};
2use crate::common::utils::md5;
3
4#[derive(Clone)]
16pub enum Endpoint {
17 Auto,
18 Telecom,
19 Cnc,
20 Ctt,
21}
22
23impl Endpoint {
24 pub fn value(&self) -> &'static str {
25 match self {
26 Endpoint::Auto => "https://v0.api.upyun.com",
27 Endpoint::Telecom => "https://v1.api.upyun.com",
28 Endpoint::Cnc => "https://v2.api.upyun.com",
29 Endpoint::Ctt => "https://v3.api.upyun.com"
30 }
31 }
32}
33
34pub struct UpYun {
36 pub bucket: String,
38 pub operator: String,
40 pub password: String,
42 pub timeout: u64,
44 pub endpoint: Endpoint,
46 pub client: Client
48}
49
50pub struct UpyunBuilder {
52 bucket: Option<String>,
53 operator: Option<String>,
54 password: Option<String>,
55 timeout: Option<u64>,
56 endpoint: Option<Endpoint>,
57 danger_accept_invalid_certs: bool
58}
59
60impl UpYun {
61 pub fn builder() -> UpyunBuilder {
63 UpyunBuilder {
64 bucket: None,
65 operator: None,
66 password: None,
67 timeout: None,
68 endpoint: None,
69 danger_accept_invalid_certs: false
70 }
71 }
72}
73
74impl UpyunBuilder {
75 pub fn bucket(mut self, bucket: &str) -> Self {
77 self.bucket = Some(bucket.to_string());
78 self
79 }
80
81 pub fn operator(mut self, operator: &str) -> Self {
83 self.operator = Some(operator.to_string());
84 self
85 }
86
87 pub fn password(mut self, password: &str) -> Self {
89 self.password = Some(password.to_string());
90 self
91 }
92
93 pub fn timeout(mut self, timeout: u64) -> Self {
95 self.timeout = Some(timeout);
96 self
97 }
98
99 pub fn endpoint(mut self, endpoint: Endpoint) -> Self {
101 self.endpoint = Some(endpoint);
102 self
103 }
104
105 pub fn danger_accept_invalid_certs(mut self, danger_accept_invalid_certs: bool) -> Self {
107 self.danger_accept_invalid_certs = danger_accept_invalid_certs;
108 self
109 }
110
111 pub fn build(self) -> UpYun {
113 if self.bucket.is_none() {
114 panic!("Bucket is required.")
115 }
116 if self.operator.is_none() {
117 panic!("Operator is required.")
118 }
119 if self.password.is_none() {
120 panic!("Password is required.")
121 }
122
123 UpYun {
124 bucket: self.bucket.unwrap(),
125 operator: self.operator.unwrap(),
126 password: md5(self.password.unwrap()),
128 timeout: self.timeout.unwrap_or(30 * 1000),
130 endpoint: self.endpoint.unwrap_or(Endpoint::Auto),
132 client: ClientBuilder::new()
133 .danger_accept_invalid_certs(self.danger_accept_invalid_certs)
134 .build()
135 .unwrap()
136 }
137 }
138}