oss_rust_sdk/
async_service.rs

1use async_trait::async_trait;
2use reqwest::header::{HeaderMap, DATE};
3use std::collections::HashMap;
4
5use crate::prelude::ListBuckets;
6
7use super::auth::*;
8use super::errors::Error;
9use super::oss::OSS;
10
11#[derive(Clone, Debug)]
12pub struct Bucket {
13    name: String,
14    creation_date: String,
15    location: String,
16    extranet_endpoint: String,
17    intranet_endpoint: String,
18    storage_class: String,
19}
20
21impl Bucket {
22    pub fn new(
23        name: String,
24        creation_date: String,
25        location: String,
26        extranet_endpoint: String,
27        intranet_endpoint: String,
28        storage_class: String,
29    ) -> Self {
30        Bucket {
31            name,
32            creation_date,
33            location,
34            extranet_endpoint,
35            intranet_endpoint,
36            storage_class,
37        }
38    }
39
40    pub fn name(&self) -> &str {
41        &self.name
42    }
43
44    pub fn creation_date(&self) -> &str {
45        &self.creation_date
46    }
47
48    pub fn location(&self) -> &str {
49        &self.location
50    }
51
52    pub fn extranet_endpoint(&self) -> &str {
53        &self.extranet_endpoint
54    }
55
56    pub fn intranet_endpoint(&self) -> &str {
57        &self.intranet_endpoint
58    }
59
60    pub fn storage_class(&self) -> &str {
61        &self.storage_class
62    }
63}
64
65#[async_trait]
66pub trait ServiceAPI {
67    async fn list_bucket<S, R>(&self, resources: R) -> Result<ListBuckets, Error>
68    where
69        S: AsRef<str> + Send,
70        R: Into<Option<HashMap<S, Option<S>>>> + Send;
71}
72
73#[async_trait]
74impl<'a> ServiceAPI for OSS<'a> {
75    async fn list_bucket<S, R>(&self, resources: R) -> Result<ListBuckets, Error>
76    where
77        S: AsRef<str> + Send,
78        R: Into<Option<HashMap<S, Option<S>>>> + Send,
79    {
80        let resources_str = if let Some(r) = resources.into() {
81            self.get_resources_str(&r)
82        } else {
83            String::new()
84        };
85        let host = self.endpoint();
86        let date = self.date();
87
88        let mut headers = HeaderMap::new();
89        headers.insert(DATE, date.parse()?);
90        let authorization = self.oss_sign(
91            "GET",
92            self.key_id(),
93            self.key_secret(),
94            "",
95            "",
96            &resources_str,
97            &headers,
98        );
99        headers.insert("Authorization", authorization.parse()?);
100
101        let resp = self.http_client.get(host).headers(headers).send().await?;
102
103        let body = resp.text().await?;
104        let list_buckets = quick_xml::de::from_str::<ListBuckets>(&body)?;
105
106        Ok(list_buckets)
107    }
108}