xt_oss/oss/api/bucket/
style.rs

1use crate::oss;
2
3use self::builders::{DeleteStyleBuilder, GetStyleBuilder, ListStyleBuilder, PutStyleBuilder};
4
5pub mod builders {
6    use crate::oss::{
7        self,
8        api::{self, ApiResponseFrom},
9        entities::style::{Style, StyleList},
10        http,
11    };
12
13    pub struct PutStyleBuilder<'a> {
14        client: &'a oss::Client<'a>,
15        style: Style,
16    }
17
18    impl<'a> PutStyleBuilder<'a> {
19        pub fn new(client: &'a oss::Client<'a>) -> Self {
20            Self {
21                client,
22                style: Style::default(),
23            }
24        }
25
26        pub fn with_name(mut self, value: &'a str) -> Self {
27            self.style.name = value.to_string();
28            self
29        }
30
31        pub fn with_content(mut self, value: &'a str) -> Self {
32            self.style.content = value.to_string();
33            self
34        }
35
36        // pub fn with_category(mut self, value: &'a str) -> Self {
37        //     self.style.category = Some(value.to_string());
38        //     self
39        // }
40
41        fn style(&self) -> String {
42            quick_xml::se::to_string(&self.style).unwrap()
43        }
44
45        pub async fn execute(&self) -> api::ApiResult {
46            let res = format!(
47                "/{}/?{}&styleName={}",
48                self.client.bucket(),
49                "style",
50                self.style.name
51            );
52            let url = format!(
53                "{}?{}&styleName={}",
54                self.client.base_url(),
55                "style",
56                self.style.name
57            );
58
59            let data = oss::Bytes::from(self.style());
60            // dbg!(&data);
61
62            let resp = self
63                .client
64                .request
65                .task()
66                .with_url(&url)
67                .with_method(http::Method::PUT)
68                .with_resource(&res)
69                .with_body(data)
70                .execute()
71                .await?;
72
73            Ok(ApiResponseFrom(resp).to_empty().await)
74        }
75    }
76
77    pub struct ListStyleBuilder<'a> {
78        client: &'a oss::Client<'a>,
79    }
80
81    impl<'a> ListStyleBuilder<'a> {
82        pub fn new(client: &'a oss::Client<'a>) -> Self {
83            Self { client }
84        }
85
86        pub async fn execute(&self) -> api::ApiResult<StyleList> {
87            let res = format!("/{}/?{}", self.client.bucket(), "style");
88            let url = format!("{}/?{}", self.client.base_url(), "style");
89            let resp = self
90                .client
91                .request
92                .task()
93                .with_url(&url)
94                .with_resource(&res)
95                .execute()
96                .await?;
97            Ok(ApiResponseFrom(resp).to_type().await)
98        }
99    }
100
101    pub struct GetStyleBuilder<'a> {
102        client: &'a oss::Client<'a>,
103        name: &'a str,
104    }
105
106    impl<'a> GetStyleBuilder<'a> {
107        pub fn new(client: &'a oss::Client<'a>, name: &'a str) -> Self {
108            Self { client, name }
109        }
110
111        pub async fn execute(&self) -> api::ApiResult<Style> {
112            let res = format!(
113                "/{}/?{}&styleName={}",
114                self.client.bucket(),
115                "style",
116                self.name
117            );
118            let url = format!(
119                "{}/?{}&styleName={}",
120                self.client.options.base_url(),
121                "style",
122                self.name
123            );
124
125            let resp = self
126                .client
127                .request
128                .task()
129                .with_url(&url)
130                .with_method(http::Method::GET)
131                .with_resource(&res)
132                .execute()
133                .await?;
134            Ok(ApiResponseFrom(resp).to_type().await)
135        }
136    }
137
138    pub struct DeleteStyleBuilder<'a> {
139        client: &'a oss::Client<'a>,
140        name: &'a str,
141    }
142
143    impl<'a> DeleteStyleBuilder<'a> {
144        pub fn new(client: &'a oss::Client<'a>, name: &'a str) -> Self {
145            Self { client, name }
146        }
147
148        pub async fn execute(&self) -> api::ApiResult {
149            let res = format!(
150                "/{}/?{}&styleName={}",
151                self.client.bucket(),
152                "style",
153                self.name
154            );
155            let url = format!(
156                "{}/?{}&styleName={}",
157                self.client.base_url(),
158                "style",
159                self.name
160            );
161
162            let resp = self
163                .client
164                .request
165                .task()
166                .with_url(&url)
167                .with_method(http::Method::DELETE)
168                .with_resource(&res)
169                .execute()
170                .await?;
171            Ok(ApiResponseFrom(resp).to_empty().await)
172        }
173    }
174}
175
176/// # 图片样式`Style`
177#[allow(non_snake_case)]
178impl<'a> oss::Client<'a> {
179    /// 调用PutStyle接口新增图片样式。一个图片样式中可以包含单个或多个图片处理参数
180    ///
181    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/putstyle)
182    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_style_put.rs)
183    pub fn PutStyle(&self) -> PutStyleBuilder {
184        PutStyleBuilder::new(self)
185    }
186
187    /// 调用ListStyle接口查询某个Bucket下已创建的所有样式
188    ///
189    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/deletestyle)
190    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_style_get.rs)
191    pub fn GetStyle(&self, name: &'a str) -> GetStyleBuilder {
192        GetStyleBuilder::new(self, name)
193    }
194
195    /// 调用GetStyle接口查询某个Bucket下指定的样式信息
196    ///
197    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/getstyle)
198    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_style_list.rs)
199    pub fn ListStyle(&self) -> ListStyleBuilder {
200        ListStyleBuilder::new(self)
201    }
202
203    /// 调用DeleteStyle删除某个Bucket下指定的图片样式
204    ///
205    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/deletestyle)
206    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_style_del.rs)
207    pub fn DeleteStyle(&self, name: &'a str) -> DeleteStyleBuilder {
208        DeleteStyleBuilder::new(self, name)
209    }
210}