upyun_sdk/
upyun.rs

1use reqwest::{Client, ClientBuilder};
2use crate::common::utils::md5;
3
4/// Rest Api 接入点
5///
6/// 可选值:
7///
8/// `Auto`:根据网络条件自动选择接入点:v0.api.upyun.com(默认)
9///
10/// `Telecom`:电信接入点:v1.api.upyun.com
11///
12/// `Cnc`:联通网通接入点:v2.api.upyun.com
13///
14/// `Ctt`:移动铁通接入点:v3.api.upyun.com
15#[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
34/// Upyun 实例
35pub struct UpYun {
36    /// 服务名称
37    pub bucket: String,
38    /// 操作员
39    pub operator: String,
40    /// 密码
41    pub password: String,
42    /// 请求超时时间(默认:30s)
43    pub timeout: u64,
44    /// 接入点(默认为自动识别接入点)
45    pub endpoint: Endpoint,
46    /// HTTP 客户端
47    pub client: Client
48}
49
50/// Upyun 实例构造器
51pub 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    /// 构造器
62    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    /// 服务名称
76    pub fn bucket(mut self, bucket: &str) -> Self {
77        self.bucket = Some(bucket.to_string());
78        self
79    }
80
81    /// 操作员
82    pub fn operator(mut self, operator: &str) -> Self {
83        self.operator = Some(operator.to_string());
84        self
85    }
86
87    /// 密码
88    pub fn password(mut self, password: &str) -> Self {
89        self.password = Some(password.to_string());
90        self
91    }
92
93    /// 请求超时时间(默认:30s)
94    pub fn timeout(mut self, timeout: u64) -> Self {
95        self.timeout = Some(timeout);
96        self
97    }
98
99    /// 接入点(默认为自动识别接入点)
100    pub fn endpoint(mut self, endpoint: Endpoint) -> Self {
101        self.endpoint = Some(endpoint);
102        self
103    }
104
105    /// 忽略证书验证
106    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    /// 构造 Upyun 实例
112    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            // 密码使用 MD5 加密
127            password: md5(self.password.unwrap()),
128            // 超时时间默认30s
129            timeout: self.timeout.unwrap_or(30 * 1000),
130            // 默认为自动识别接入点
131            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}