xt_oss/oss/api/bucket/
policy.rs1use crate::oss;
2
3use self::builders::{DeleteBucketPolicyBuilder, GetBucketPolicyBuilder, PutBucketPolicyBuilder};
4
5pub mod builders {
6 use crate::oss::{
7 self,
8 api::{self, ApiResponseFrom},
9 http,
10 };
11
12 pub struct PutBucketPolicyBuilder<'a> {
13 client: &'a oss::Client<'a>,
14 policy: &'a str,
15 }
16
17 impl<'a> PutBucketPolicyBuilder<'a> {
18 pub(crate) fn new(client: &'a oss::Client) -> Self {
19 Self {
20 client,
21 policy: Default::default(),
22 }
23 }
24
25 pub fn with_policy(mut self, value: &'a str) -> Self {
26 self.policy = value;
27 self
28 }
29
30 pub async fn execute(&self) -> api::ApiResult<()> {
31 let res = format!("/{}/?{}", self.client.bucket(), "policy");
32 let url = format!("{}/?{}", self.client.base_url(), "policy");
33
34 let data = oss::Bytes::from(self.policy.to_string());
35
36 let resp = self
37 .client
38 .request
39 .task()
40 .with_url(&url)
41 .with_resource(&res)
42 .with_method(http::Method::PUT)
43 .with_body(data)
44 .execute()
45 .await?;
46
47 Ok(ApiResponseFrom(resp).to_empty().await)
48 }
49 }
50
51 pub struct GetBucketPolicyBuilder<'a> {
52 client: &'a oss::Client<'a>,
53 }
54
55 impl<'a> GetBucketPolicyBuilder<'a> {
56 pub(crate) fn new(client: &'a oss::Client) -> Self {
57 Self { client }
58 }
59
60 pub async fn execute(&self) -> api::ApiResult<String> {
61 let res = format!("/{}/?{}", self.client.options.bucket, "policy");
62 let url = format!("{}/?{}", self.client.options.base_url(), "policy");
63
64 let resp = self
65 .client
66 .request
67 .task()
68 .with_url(&url)
69 .with_resource(&res)
70 .execute()
71 .await?;
72
73 Ok(ApiResponseFrom(resp).to_text().await)
74 }
75 }
76
77 pub struct DeleteBucketPolicyBuilder<'a> {
78 client: &'a oss::Client<'a>,
79 }
80
81 impl<'a> DeleteBucketPolicyBuilder<'a> {
82 pub(crate) fn new(client: &'a oss::Client) -> Self {
83 Self { client }
84 }
85
86 pub async fn execute(&self) -> api::ApiResult {
87 let res = format!("/{}/?{}", self.client.options.bucket, "policy");
88 let url = format!("{}/?{}", self.client.options.base_url(), "policy");
89
90 let resp = self
91 .client
92 .request
93 .task()
94 .with_url(&url)
95 .with_resource(&res)
96 .with_method(http::Method::DELETE)
97 .execute()
98 .await?;
99
100 Ok(ApiResponseFrom(resp).to_empty().await)
101 }
102 }
103}
104
105#[allow(non_snake_case)]
107impl<'a> oss::Client<'a> {
108 pub fn PutBucketPolicy(&self) -> PutBucketPolicyBuilder {
113 PutBucketPolicyBuilder::new(self)
114 }
115
116 pub fn GetBucketPolicy(&self) -> GetBucketPolicyBuilder {
121 GetBucketPolicyBuilder::new(self)
122 }
123
124 pub fn DeleteBucketPolicy(&self) -> DeleteBucketPolicyBuilder {
129 DeleteBucketPolicyBuilder::new(self)
130 }
131}