aliyun_oss_rs/client/
describe_regions.rs

1use crate::common::body_to_bytes;
2use crate::{
3    Error,
4    error::normal_error,
5    request::{Oss, OssRequest},
6};
7use http::Method;
8use serde_derive::Deserialize;
9
10// Returned content
11/// Basic region information
12#[derive(Debug, Deserialize)]
13#[serde(rename_all = "PascalCase")]
14pub struct RegionInfo {
15    /// Region ID
16    pub region: String,
17    /// Acceleration endpoint for the region
18    pub accelerate_endpoint: String,
19    /// Internal endpoint for the region
20    pub internal_endpoint: String,
21    /// Public endpoint for the region
22    pub internet_endpoint: String,
23}
24
25#[doc(hidden)]
26#[derive(Debug, Deserialize)]
27#[serde(rename_all = "PascalCase")]
28pub(crate) struct RegionInfoList {
29    pub region_info: Vec<RegionInfo>,
30}
31
32/// Query the endpoint information of regions
33///
34/// You can call `set_regions` to query specific regions; by default all are queried. See the [Alibaba Cloud documentation](https://help.aliyun.com/document_detail/345596.html) for details
35///
36/// ```ignore
37/// let client = OssClient::new("AccessKey ID","AccessKey Secret","oss-cn-beijing.aliyuncs.com");
38/// let regions = client.describe_regions()
39///                     .set_regions("oss-cn-hangzhou")
40///                     .send().await;
41/// println!("{:#?}", regions);
42/// ```
43///
44pub struct DescribeRegions {
45    req: OssRequest,
46}
47
48impl DescribeRegions {
49    pub(super) fn new(oss: Oss) -> Self {
50        let mut req = OssRequest::new(oss, Method::GET);
51        req.insert_query("regions", "");
52        DescribeRegions { req }
53    }
54
55    /// Specify a single region to query; this requires a Region ID such as oss-cn-hangzhou
56    pub fn set_regions(mut self, regions: impl ToString) -> Self {
57        self.req.insert_query("regions", regions);
58        self
59    }
60
61    /// Specify which endpoint to use for the request
62    ///
63    /// Defaults to oss.aliyuncs.com. If your network cannot access it, set an accessible endpoint
64    pub fn set_endpoint(mut self, endpoint: impl ToString) -> Self {
65        self.req.set_endpoint(endpoint);
66        self
67    }
68
69    /// Send the request
70    pub async fn send(self) -> Result<Vec<RegionInfo>, Error> {
71        // Build the HTTP request
72        let response = self.req.send_to_oss()?.await?;
73        // Parse the response
74        let status_code = response.status();
75        match status_code {
76            code if code.is_success() => {
77                let response_bytes = body_to_bytes(response.into_body())
78                    .await
79                    .map_err(|_| Error::OssInvalidResponse(None))?;
80                let regions: RegionInfoList = serde_xml_rs::from_reader(&*response_bytes)
81                    .map_err(|_| Error::OssInvalidResponse(Some(response_bytes)))?;
82                Ok(regions.region_info)
83            }
84            _ => Err(normal_error(response).await),
85        }
86    }
87}