xt_oss/oss/api/
service.rs

1use crate::oss::Client;
2
3use builders::ListBucketsBuilder;
4
5pub mod builders {
6    use serde::{Deserialize, Serialize};
7    use std::fmt;
8
9    use crate::oss::{
10        self,
11        api::{self, insert_custom_header, ApiResponseFrom},
12        entities::bucket::ListAllMyBucketsResult,
13        http,
14    };
15
16    #[derive(Debug, Serialize, Deserialize, Default)]
17    struct ListBucketsQuery<'a> {
18        #[serde(skip_serializing_if = "Option::is_none")]
19        marker: Option<&'a str>,
20        #[serde(
21            rename(serialize = "max-keys"),
22            skip_serializing_if = "Option::is_none"
23        )]
24        max_keys: Option<i32>,
25        #[serde(skip_serializing_if = "Option::is_none")]
26        prefix: Option<&'a str>,
27    }
28
29    impl<'a> fmt::Display for ListBucketsQuery<'a> {
30        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31            write!(f, "{}", serde_qs::to_string(&self).unwrap())
32        }
33    }
34
35    #[derive(Debug)]
36    pub struct ListBucketsBuilder<'a> {
37        client: &'a oss::Client<'a>,
38        resource_group_id: Option<&'a str>,
39        query: ListBucketsQuery<'a>,
40    }
41
42    impl<'a> ListBucketsBuilder<'a> {
43        pub(crate) fn new(client: &'a oss::Client) -> Self {
44            Self {
45                client,
46                resource_group_id: None,
47                query: ListBucketsQuery::default(),
48            }
49        }
50
51        pub fn with_prefix(mut self, value: &'a str) -> Self {
52            self.query.prefix = Some(value);
53            self
54        }
55
56        pub fn with_marker(mut self, value: &'a str) -> Self {
57            self.query.marker = Some(value);
58            self
59        }
60
61        pub fn with_max_keys(mut self, value: i32) -> Self {
62            self.query.max_keys = Some(value);
63            self
64        }
65
66        pub fn with_resource_group_id(mut self, value: &'a str) -> Self {
67            self.resource_group_id = Some(value);
68            self
69        }
70
71        fn query(&self) -> String {
72            serde_qs::to_string(&self.query).unwrap()
73        }
74
75        fn headers(&self) -> http::HeaderMap {
76            let mut headers = http::HeaderMap::new();
77            if let Some(group_id) = self.resource_group_id {
78                insert_custom_header(&mut headers, "x-oss-resource-group-id", group_id);
79            }
80            headers
81        }
82
83        pub async fn execute(&self) -> api::ApiResult<ListAllMyBucketsResult> {
84            let query = self.query();
85            let headers = self.headers();
86
87            let mut url = self.client.root_url();
88
89            if !query.is_empty() {
90                url = format!("{}/?{}", url, query)
91            }
92
93            let resp = self
94                .client
95                .request
96                .task()
97                .with_method(http::Method::GET)
98                .with_headers(headers)
99                .with_url(&url)
100                .execute_timeout(self.client.timeout())
101                .await?;
102
103            Ok(ApiResponseFrom(resp).to_type().await)
104        }
105    }
106}
107
108#[allow(non_snake_case)]
109/// 关于Region操作
110impl<'a> Client<'a> {
111    /// 调用ListBuckets(GetService)接口列举请求者拥有的所有存储空间(Bucket)。
112    /// 您还可以通过设置prefix、marker或者max-keys参数列举满足指定条件的存储空间。
113    ///
114    /// - [official docs](https://help.aliyun.com/zh/oss/developer-reference/listbuckets)
115    /// - [xtoss example](https://github.com/isme-sun/xt_oss/blob/main/examples/api_service_list_buckets.rs)
116    pub fn ListBuckets(&self) -> ListBucketsBuilder {
117        ListBucketsBuilder::new(self)
118    }
119}