upyun_sdk/
upyun.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use reqwest::{Client, ClientBuilder};
use crate::common::utils::md5;

/// Rest Api 接入点
///
/// 可选值:
///
/// `Auto`:根据网络条件自动选择接入点:v0.api.upyun.com(默认)
///
/// `Telecom`:电信接入点:v1.api.upyun.com
///
/// `Cnc`:联通网通接入点:v2.api.upyun.com
///
/// `Ctt`:移动铁通接入点:v3.api.upyun.com
#[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"
        }
    }
}

/// Upyun 实例
pub struct UpYun {
    /// 服务名称
    pub bucket: String,
    /// 操作员
    pub operator: String,
    /// 密码
    pub password: String,
    /// 请求超时时间(默认:30s)
    pub timeout: u64,
    /// 接入点(默认为自动识别接入点)
    pub endpoint: Endpoint,
    /// HTTP 客户端
    pub client: Client
}

/// Upyun 实例构造器
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
    }

    /// 请求超时时间(默认:30s)
    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
    }

    /// 构造 Upyun 实例
    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(),
            // 密码使用 MD5 加密
            password: md5(self.password.unwrap()),
            // 超时时间默认30s
            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()
        }
    }
}