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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use crate::oss;

use self::builders::{DeleteStyleBuilder, GetStyleBuilder, ListStyleBuilder, PutStyleBuilder};

pub mod builders {
    use crate::oss::{
        self,
        api::{self, ApiResponseFrom},
        entities::style::{Style, StyleList},
        http,
    };

    pub struct PutStyleBuilder<'a> {
        client: &'a oss::Client<'a>,
        style: Style,
    }

    impl<'a> PutStyleBuilder<'a> {
        pub fn new(client: &'a oss::Client<'a>) -> Self {
            Self {
                client,
                style: Style::default(),
            }
        }

        pub fn with_name(mut self, value: &'a str) -> Self {
            self.style.name = value.to_string();
            self
        }

        pub fn with_content(mut self, value: &'a str) -> Self {
            self.style.content = value.to_string();
            self
        }

        // pub fn with_category(mut self, value: &'a str) -> Self {
        //     self.style.category = Some(value.to_string());
        //     self
        // }

        fn style(&self) -> String {
            quick_xml::se::to_string(&self.style).unwrap()
        }

        pub async fn execute(&self) -> api::ApiResult {
            let res = format!(
                "/{}/?{}&styleName={}",
                self.client.bucket(),
                "style",
                self.style.name
            );
            let url = format!(
                "{}?{}&styleName={}",
                self.client.base_url(),
                "style",
                self.style.name
            );

            let data = oss::Bytes::from(self.style());
            // dbg!(&data);

            let resp = self
                .client
                .request
                .task()
                .with_url(&url)
                .with_method(http::Method::PUT)
                .with_resource(&res)
                .with_body(data)
                .execute()
                .await?;

            Ok(ApiResponseFrom(resp).to_empty().await)
        }
    }

    pub struct ListStyleBuilder<'a> {
        client: &'a oss::Client<'a>,
    }

    impl<'a> ListStyleBuilder<'a> {
        pub fn new(client: &'a oss::Client<'a>) -> Self {
            Self { client }
        }

        pub async fn execute(&self) -> api::ApiResult<StyleList> {
            let res = format!("/{}/?{}", self.client.bucket(), "style");
            let url = format!("{}/?{}", self.client.base_url(), "style");
            let resp = self
                .client
                .request
                .task()
                .with_url(&url)
                .with_resource(&res)
                .execute()
                .await?;
            Ok(ApiResponseFrom(resp).to_type().await)
        }
    }

    pub struct GetStyleBuilder<'a> {
        client: &'a oss::Client<'a>,
        name: &'a str,
    }

    impl<'a> GetStyleBuilder<'a> {
        pub fn new(client: &'a oss::Client<'a>, name: &'a str) -> Self {
            Self { client, name }
        }

        pub async fn execute(&self) -> api::ApiResult<Style> {
            let res = format!(
                "/{}/?{}&styleName={}",
                self.client.bucket(),
                "style",
                self.name
            );
            let url = format!(
                "{}/?{}&styleName={}",
                self.client.options.base_url(),
                "style",
                self.name
            );

            let resp = self
                .client
                .request
                .task()
                .with_url(&url)
                .with_method(http::Method::GET)
                .with_resource(&res)
                .execute()
                .await?;
            Ok(ApiResponseFrom(resp).to_type().await)
        }
    }

    pub struct DeleteStyleBuilder<'a> {
        client: &'a oss::Client<'a>,
        name: &'a str,
    }

    impl<'a> DeleteStyleBuilder<'a> {
        pub fn new(client: &'a oss::Client<'a>, name: &'a str) -> Self {
            Self { client, name }
        }

        pub async fn execute(&self) -> api::ApiResult {
            let res = format!(
                "/{}/?{}&styleName={}",
                self.client.bucket(),
                "style",
                self.name
            );
            let url = format!(
                "{}/?{}&styleName={}",
                self.client.base_url(),
                "style",
                self.name
            );

            let resp = self
                .client
                .request
                .task()
                .with_url(&url)
                .with_method(http::Method::DELETE)
                .with_resource(&res)
                .execute()
                .await?;
            Ok(ApiResponseFrom(resp).to_empty().await)
        }
    }
}

/// # 图片样式`Style`
#[allow(non_snake_case)]
impl<'a> oss::Client<'a> {
    /// 调用PutStyle接口新增图片样式。一个图片样式中可以包含单个或多个图片处理参数
    ///
    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/putstyle)
    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_style_put.rs)
    pub fn PutStyle(&self) -> PutStyleBuilder {
        PutStyleBuilder::new(self)
    }

    /// 调用ListStyle接口查询某个Bucket下已创建的所有样式
    ///
    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/deletestyle)
    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_style_get.rs)
    pub fn GetStyle(&self, name: &'a str) -> GetStyleBuilder {
        GetStyleBuilder::new(self, name)
    }

    /// 调用GetStyle接口查询某个Bucket下指定的样式信息
    ///
    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/getstyle)
    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_style_list.rs)
    pub fn ListStyle(&self) -> ListStyleBuilder {
        ListStyleBuilder::new(self)
    }

    /// 调用DeleteStyle删除某个Bucket下指定的图片样式
    ///
    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/deletestyle)
    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_bucket_style_del.rs)
    pub fn DeleteStyle(&self, name: &'a str) -> DeleteStyleBuilder {
        DeleteStyleBuilder::new(self, name)
    }
}