aliyun_oss_rust_sdk/
request.rs

1use std::collections::HashMap;
2use std::fmt::{Display, Formatter};
3
4pub type Seconds = i64;
5
6#[derive(PartialEq, Eq, Clone, Debug)]
7pub enum RequestType {
8    Get,
9    Put,
10    Post,
11    Delete,
12    Head,
13}
14
15impl Display for RequestType{
16    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
17        match self {
18            RequestType::Get => write!(f, "GET"),
19            RequestType::Put => write!(f, "PUT"),
20            RequestType::Post => write!(f, "POST"),
21            RequestType::Delete => write!(f, "DELETE"),
22            RequestType::Head => write!(f, "HEAD"),
23        }
24    }
25}
26
27unsafe impl Send for RequestType {}
28unsafe impl Sync for RequestType {}
29
30#[derive(Clone, Debug)]
31pub struct RequestBuilder {
32    pub cdn: Option<String>,
33    pub https: bool,
34    pub method: RequestType,
35    pub expire: Seconds,
36    pub headers: HashMap<String, String>,
37    pub parameters: HashMap<String, String>,
38    pub content_type: Option<String>,
39    pub content_md5: Option<String>,
40    pub oss_headers: HashMap<String, String>,
41}
42
43impl Default for RequestBuilder {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49unsafe impl Send for RequestBuilder {}
50
51unsafe impl Sync for RequestBuilder {}
52
53impl RequestBuilder {
54    pub fn new() -> Self {
55        Self {
56            cdn: None,
57            https: true,
58            method: RequestType::Get,
59            expire: 60,
60            headers: HashMap::new(),
61            parameters: HashMap::new(),
62            content_type: None,
63            content_md5: None,
64            oss_headers: HashMap::new(),
65        }
66    }
67    pub fn with_http(mut self) -> Self {
68        self.https = false;
69        self
70    }
71    pub fn with_cdn<S: AsRef<str>>(mut self, cdn: S) -> Self {
72        self.cdn = Some(cdn.as_ref().to_string());
73        self
74    }
75    pub fn with_content_type<S: AsRef<str>>(mut self, content_type: S) -> Self {
76        self.content_type = Some(content_type.as_ref().to_string());
77        self
78    }
79    pub fn with_expire(mut self, expire: Seconds) -> Self {
80        self.expire = expire;
81        self
82    }
83    pub fn response_content_disposition<S: AsRef<str>>(mut self, file_name: S) -> Self {
84        self.parameters.insert("response-content-disposition".to_string(), format!("attachment;filename={}", file_name.as_ref()));
85        self
86    }
87    pub fn oss_signature_version2(mut self) -> Self {
88        self.parameters.insert("x-oss-signature-version".to_string(), "OSS2".to_string());
89        self
90    }
91    pub fn response_content_encoding<S: AsRef<String>>(mut self, encoding: S) -> Self {
92        self.parameters.insert("response-content-encoding".to_string(), encoding.as_ref().to_string());
93        self
94    }
95    pub fn oss_download_speed_limit<S: Into<i32>>(mut self, speed: S) -> Self {
96        let speed = speed.into();
97        assert!(speed >= 30, "speed must be greater than 30kb");
98        self.parameters.insert("x-oss-traffic-limit".to_string(), (speed * 1024 * 8).to_string());
99        self
100    }
101    pub fn oss_download_allow_ip<IP, S>(mut self, ip: IP, mask: S) -> Self
102        where IP: AsRef<str>, S: Into<u8>
103    {
104        self.parameters.insert("x-oss-ac-source-ip".to_string(), ip.as_ref().to_string());
105        self.parameters.insert("x-oss-ac-subnet-mask".to_string(), mask.into().to_string());
106        self
107    }
108    pub fn oss_ac_forward_allow(mut self) -> Self {
109        self.parameters.insert("x-oss-ac-forwarded-for".to_string(), "true".to_string());
110        self
111    }
112    pub fn oss_header_put<S: AsRef<str>>(mut self, key: S, value: S) -> Self {
113        self.oss_headers.insert(key.as_ref().to_string(), value.as_ref().to_string());
114        self
115    }
116    pub fn parameters_put<S: AsRef<str>>(mut self, key: S, value: S) -> Self {
117        self.parameters.insert(key.as_ref().to_string(), value.as_ref().to_string());
118        self
119    }
120}