supabase_storage/build/
bucket.rs

1use reqwest::{header::HeaderValue, Method};
2
3use crate::{
4    build::{builder::Builder, executor::Executor},
5    model::bucket::{BucketUpdate, NewBucket},
6};
7
8use super::builder::BodyType;
9
10impl Builder {
11    /// retrieve all buckets
12    ///
13    /// # Returns
14    ///
15    /// * `Executor` - The constructed `Executor` instance for executing the request.
16    ///
17    /// # Example
18    /// ```
19    /// use supabase_storage::{
20    ///     Storage,
21    ///     config::SupabaseConfig,
22    /// };
23    /// use dotenv::dotenv;
24    ///
25    /// dotenv().ok();
26    /// let config = SupabaseConfig::default();
27    /// let storage = Storage::new_with_config(config)
28    ///     .from()
29    ///     .get_buckets()
30    ///     .execute();
31    /// ```
32    pub fn get_buckets(mut self) -> Executor {
33        self.url.path_segments_mut().unwrap().push("bucket");
34        self.create_executor()
35    }
36
37    /// create a new bucket
38    ///
39    /// # Arguments
40    ///
41    /// * `body` - The request body as a string.
42    ///
43    /// # Returns
44    ///
45    /// * `Executor` - The constructed `Executor` instance for executing the request.
46    ///
47    /// # Example
48    ///
49    /// ```
50    /// use supabase_storage::{
51    ///     Storage,
52    ///     config::SupabaseConfig,
53    /// };
54    /// use dotenv::dotenv;
55    ///
56    /// dotenv().ok();
57    /// let config = SupabaseConfig::default();
58    /// let storage = Storage::new_with_config(config)
59    ///     .from()
60    ///     .create_bucket("thefux")
61    ///     .execute();
62    /// ```
63    pub fn create_bucket(mut self, body: &str) -> Executor {
64        self.method = Method::POST;
65        self.url.path_segments_mut().unwrap().push("bucket");
66        self.body = Some(BodyType::StringBody(body.into()));
67        self.create_executor()
68    }
69
70    /// create a new bucket using a struct
71    ///
72    /// # Arguments
73    ///
74    /// * `body` - The `NewBucket` struct containing the request body.
75    ///
76    /// # Returns
77    ///
78    /// * `Executor` - The constructed `Executor` instance for executing the request.
79    ///
80    /// # Example
81    /// ```
82    /// use supabase_storage::{
83    ///     Storage,
84    ///     config::SupabaseConfig,
85    ///     model::bucket::NewBucket,
86    /// };
87    /// use dotenv::dotenv;
88    ///
89    /// dotenv().ok();
90    /// let config = SupabaseConfig::default();
91    /// let storage = Storage::new_with_config(config)
92    ///     .from()
93    ///     .create_bucket_from(NewBucket::new("thefux".to_string()))
94    ///     .execute();
95    /// ```
96    pub fn create_bucket_from(mut self, body: NewBucket) -> Executor {
97        self.method = Method::POST;
98        self.url.path_segments_mut().unwrap().push("bucket");
99        self.body = Some(BodyType::StringBody(
100            serde_json::to_string(&body).unwrap_or_default(),
101        ));
102        self.create_executor()
103    }
104
105    /// empty the bucket
106    ///
107    /// # Arguments
108    ///
109    /// * `bucket_id` - The identifier of the bucket to empty.
110    ///
111    /// # Returns
112    ///
113    /// * `Executor` - The constructed `Executor` instance for executing the request.
114    ///
115    /// # Example
116    /// ```
117    /// use supabase_storage::{
118    ///     Storage,
119    ///     config::SupabaseConfig,
120    /// };
121    /// use dotenv::dotenv;
122    ///
123    /// dotenv().ok();
124    /// let config = SupabaseConfig::default();
125    /// let storage = Storage::new_with_config(config)
126    ///     .from()
127    ///     .empty_bucket("thefux")
128    ///     .execute();
129    /// ```
130    pub fn empty_bucket(mut self, bucket_id: &str) -> Executor {
131        self.method = Method::POST;
132        self.url
133            .path_segments_mut()
134            .unwrap()
135            .push("bucket")
136            .push(bucket_id);
137
138        self.create_executor()
139    }
140
141    /// get bucket details
142    ///
143    /// # Arguments
144    ///
145    /// * `bucket_id` - The identifier of the bucket to empty.
146    ///
147    /// # Returns
148    ///
149    /// * `Executor` - The constructed `Executor` instance for executing the request.
150    ///
151    /// # Example
152    /// ```
153    /// use supabase_storage::{
154    ///     Storage,
155    ///     config::SupabaseConfig,
156    /// };
157    /// use dotenv::dotenv;
158    ///
159    /// dotenv().ok();
160    /// let config = SupabaseConfig::default();
161    /// let storage = Storage::new_with_config(config)
162    ///     .from()
163    ///     .get_bucket_details("thefux")
164    ///     .execute();
165    /// ```
166    pub fn get_bucket_details(mut self, bucket_id: &str) -> Executor {
167        self.url
168            .path_segments_mut()
169            .unwrap()
170            .push("bucket")
171            .push(bucket_id);
172        self.create_executor()
173    }
174
175    /// update the bucket
176    ///
177    /// # Arguments
178    ///
179    /// * `bucket_id` - The identifier of the bucket to empty.
180    ///
181    /// # Returns
182    ///
183    /// * `Executor` - The constructed `Executor` instance for executing the request.
184    ///
185    /// # Example
186    /// ```
187    /// use supabase_storage::{
188    ///     Storage,
189    ///     config::SupabaseConfig,
190    /// };
191    /// use dotenv::dotenv;
192    ///
193    /// dotenv().ok();
194    /// let config = SupabaseConfig::default();
195    /// let storage = Storage::new_with_config(config)
196    ///     .from()
197    ///     .update_bucket("thefux", r#"{ "public": true }"#)
198    ///     .execute();
199    /// ```
200    pub fn update_bucket(mut self, bucket_id: &str, body: &str) -> Executor {
201        self.headers
202            .insert("Content-Type", HeaderValue::from_static("application/json"));
203        self.method = Method::PUT;
204        self.url
205            .path_segments_mut()
206            .unwrap()
207            .push("bucket")
208            .push(bucket_id);
209        self.body = Some(BodyType::StringBody(body.into()));
210        self.create_executor()
211    }
212
213    /// update bucket using a struct
214    ///
215    /// # Arguments
216    ///
217    /// * `bucket_id` - The identifier of the bucket to empty.
218    ///
219    /// # Returns
220    ///
221    /// * `Executor` - The constructed `Executor` instance for executing the request.
222    ///
223    /// # Example
224    /// ```
225    /// use supabase_storage::{
226    ///     Storage,
227    ///     config::SupabaseConfig,
228    ///     model::bucket::BucketUpdate,
229    /// };
230    /// use dotenv::dotenv;
231    ///
232    /// dotenv().ok();
233    /// let config = SupabaseConfig::default();
234    /// let storage = Storage::new_with_config(config)
235    ///     .from()
236    ///     .update_bucket_from("thefux", BucketUpdate {
237    ///         public: false,
238    ///         file_size_limit: Some(0),
239    ///         allowed_mime_types: Some(vec!["application/pdf".to_string()]),
240    ///     })
241    ///     .execute();
242    /// ```
243    pub fn update_bucket_from(mut self, bucket_id: &str, body: BucketUpdate) -> Executor {
244        self.method = Method::PUT;
245        self.url
246            .path_segments_mut()
247            .unwrap()
248            .push("bucket")
249            .push(bucket_id);
250        self.body = Some(BodyType::StringBody(
251            serde_json::to_string(&body).unwrap_or_default(),
252        ));
253        self.create_executor()
254    }
255
256    /// delete a bucket
257    ///
258    /// # Arguments
259    ///
260    /// * `bucket_id` - The identifier of the bucket to empty.
261    ///
262    /// # Returns
263    ///
264    /// * `Executor` - The constructed `Executor` instance for executing the request.
265    ///
266    /// # Example
267    /// ```
268    /// use supabase_storage::{
269    ///     Storage,
270    ///     config::SupabaseConfig,
271    /// };
272    /// use dotenv::dotenv;
273    ///
274    /// dotenv().ok();
275    /// let config = SupabaseConfig::default();
276    /// let storage = Storage::new_with_config(config)
277    ///     .from()
278    ///     .delete_bucket("thefux")
279    ///     .execute();
280    /// ```
281    pub fn delete_bucket(mut self, bucket_id: &str) -> Executor {
282        self.method = Method::DELETE;
283        self.url
284            .path_segments_mut()
285            .unwrap()
286            .push("bucket")
287            .push(bucket_id);
288        self.create_executor()
289    }
290}