supabase_storage/build/executor.rs
1use crate::model::errors;
2use reqwest::{Error, Response, StatusCode};
3use serde::Deserialize;
4
5use super::builder::Builder;
6
7pub struct Executor {
8 pub builder: Builder,
9}
10
11impl Executor {
12 /// Creates a new `Executor` instance with the provided HTTP method, URL, client, and headers.
13 ///
14 /// # Arguments
15 ///
16 /// * `method` - The HTTP method for the request.
17 /// * `url` - The URL for the request.
18 /// * `client` - The `Client` to use for making the request.
19 /// * `headers` - The `HeaderMap` containing the headers for the request.
20 pub fn new(builder: Builder) -> Self {
21 Self { builder }
22 }
23
24 /// Executes the constructed HTTP request and returns the response as a `Result`.
25 ///
26 /// # Returns
27 ///
28 /// * `Result<Response, Error>` - The result of the executed request.
29 ///
30 /// # Example
31 ///
32 /// ```
33 /// use supabase_storage::{
34 /// Storage,
35 /// config::SupabaseConfig,
36 /// };
37 /// use dotenv::dotenv;
38 ///
39 /// #[tokio::main]
40 /// async fn main() {
41 /// dotenv().ok();
42 /// let config = SupabaseConfig::default();
43 /// let storage = Storage::new_with_config(config);
44 /// let response = storage
45 /// .from()
46 /// .get_bucket_details("thefux")
47 /// .execute()
48 /// .await
49 /// .unwrap()
50 /// .text()
51 /// .await
52 /// .unwrap();
53 ///
54 /// // Now 'response' contains the reponse as text.
55 /// println!("{:?}", response);
56 /// }
57 /// ```
58 pub async fn execute(self) -> Result<Response, Error> {
59 self.builder.build().send().await
60 }
61
62 /// Executes the constructed HTTP request and deserializes the response body into a generic struct.
63 ///
64 /// # Returns
65 ///
66 /// * `Result<T, errors::Error>` - The result of deserializing the response body into the provided generic struct.
67 ///
68 /// # Example
69 ///
70 /// ```
71 /// use supabase_storage::{
72 /// Storage,
73 /// config::SupabaseConfig,
74 /// model::bucket::BucketDetails,
75 /// };
76 /// use dotenv::dotenv;
77 ///
78 /// #[tokio::main]
79 /// async fn main() {
80 /// dotenv().ok();
81 /// let config = SupabaseConfig::default();
82 /// let storage = Storage::new_with_config(config);
83 /// let response = storage
84 /// .from()
85 /// .get_bucket_details("thefux")
86 /// .execute_from::<BucketDetails>()
87 /// .await;
88 ///
89 /// // Now 'response' contains the deserialized 'BucketDetails' based on the response.
90 /// println!("{:?}", response);
91 /// }
92 /// ```
93 pub async fn execute_from<T>(self) -> Result<T, errors::Error>
94 where
95 T: for<'de> Deserialize<'de>,
96 {
97 let response = self.builder.build().send().await.unwrap();
98 let status = response.status();
99
100 let text = response.text().await.unwrap();
101
102 match status {
103 StatusCode::OK => {
104 if let Ok(result) = serde_json::from_str(&text) {
105 Ok(result)
106 } else {
107 Err(serde_json::from_str(&text).unwrap())
108 }
109 }
110 _ => Err(serde_json::from_str(&text).unwrap()),
111 }
112 }
113}