supabase_storage/build/object/
public.rs

1use crate::build::{builder::Builder, executor::Executor};
2
3impl Builder {
4    /// get public object from the storage
5    ///
6    /// # Arguments
7    ///
8    /// * `bucket_id` - bucket id
9    /// * `object` - a wildcard
10    ///
11    /// # Returns
12    ///
13    /// * `Executor` - The constructed `Executor` instance for executing the request.
14    ///
15    /// # Example
16    /// ```
17    /// use supabase_storage::{
18    ///     Storage,
19    ///     config::SupabaseConfig,
20    ///     model::bucket::NewBucket,
21    /// };
22    /// use dotenv::dotenv;
23    ///
24    /// #[tokio::main]
25    /// async fn main() {
26    ///     dotenv().ok();
27    ///     let config = SupabaseConfig::default();
28    ///     let response = Storage::new_with_config(config)
29    ///         .from()
30    ///         .get_public_object("thefux", "file_name.pdf")
31    ///         .execute()
32    ///         .await
33    ///         .unwrap();
34    /// }
35    /// ```
36    pub fn get_public_object(mut self, bucket_id: &str, object: &str) -> Executor {
37        self.url
38            .path_segments_mut()
39            .unwrap()
40            .push("object")
41            .push("public")
42            .push(bucket_id)
43            .push(object);
44        self.create_executor()
45    }
46
47    /// get public object info
48    ///
49    /// # Arguments
50    ///
51    /// * `bucket_id` - bucket id
52    /// * `object` - a wildcard
53    ///
54    /// # Returns
55    ///
56    /// * `Executor` - The constructed `Executor` instance for executing the request.
57    ///
58    /// # Example
59    /// ```
60    /// use supabase_storage::{
61    ///     Storage,
62    ///     config::SupabaseConfig,
63    ///     model::bucket::NewBucket,
64    /// };
65    /// use dotenv::dotenv;
66    ///
67    /// #[tokio::main]
68    /// async fn main() {
69    ///     dotenv().ok();
70    ///     let config = SupabaseConfig::default();
71    ///     let response = Storage::new_with_config(config)
72    ///         .from()
73    ///         .get_public_object_info("thefux", "file_name.pdf")
74    ///         .execute()
75    ///         .await
76    ///         .unwrap();
77    /// }
78    /// ```
79    pub fn get_public_object_info(mut self, bucket_id: &str, object: &str) -> Executor {
80        self.url
81            .path_segments_mut()
82            .unwrap()
83            .push("object")
84            .push("info")
85            .push("public")
86            .push(bucket_id)
87            .push(object);
88        self.create_executor()
89    }
90}
91
92#[cfg(test)]
93mod test {
94    use reqwest::{header::HeaderMap, Client, Method};
95    use url::{Host, Origin};
96
97    use super::*;
98
99    #[test]
100    fn test_get_public_object() {
101        let executor = Builder::new(
102            url::Url::parse("http://localhost").unwrap(),
103            HeaderMap::new(),
104            Client::new(),
105        )
106        .get_public_object("thefux", "test.pdf");
107
108        assert_eq!(executor.builder.method, Method::GET);
109        assert_eq!(
110            executor.builder.url.origin(),
111            Origin::Tuple("http".into(), Host::Domain("localhost".into()), 80)
112        );
113        assert_eq!(
114            executor.builder.url.path(),
115            "/object/public/thefux/test.pdf"
116        );
117    }
118
119    #[test]
120    fn test_get_public_object_info() {
121        let executor = Builder::new(
122            url::Url::parse("http://localhost").unwrap(),
123            HeaderMap::new(),
124            Client::new(),
125        )
126        .get_public_object_info("thefux", "test.pdf");
127
128        assert_eq!(executor.builder.method, Method::GET);
129        assert_eq!(
130            executor.builder.url.origin(),
131            Origin::Tuple("http".into(), Host::Domain("localhost".into()), 80)
132        );
133        assert_eq!(
134            executor.builder.url.path(),
135            "/object/info/public/thefux/test.pdf"
136        );
137    }
138}