Skip to content

Commit

Permalink
#6 use arc to prevent useless cloning (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
thefux authored Dec 10, 2023
1 parent 0eb5403 commit d308a29
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "supabase-storage"
authors = ["[email protected]"]
version = "0.1.1"
version = "0.2.0"
edition = "2021"
license = "MIT"
readme = "README.md"
Expand Down
25 changes: 23 additions & 2 deletions examples/get_object.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use dotenv::dotenv;
use reqwest::header::{HeaderMap, HeaderValue};
use supabase_storage::config::SupabaseConfig;
use supabase_storage::Storage;

Expand All @@ -7,12 +8,32 @@ async fn main() {
dotenv().ok();

let config = SupabaseConfig::default();
let storage = Storage::new_with_config(config);
let storage = Storage::new_with_config(config.clone());

let mut headers = HeaderMap::new();
if let Some(api_key) = config.clone().supabase_api_key {
headers.insert(
"Authorization",
HeaderValue::from_str(&format!("Bearer {}", api_key)).expect("header value is invalid"),
);
headers.insert(
"apiKey",
HeaderValue::from_str(&format!("{}", api_key)).expect("header value is invalid"),
);
}

let bucket_name = "thefux";
let object = "btc.pdf";

let response = storage
let mut response = storage
.from()
.get_object(bucket_name, object)
.execute()
.await
.unwrap();

println!("{:?}", response);
response = storage
.from()
.get_object(bucket_name, object)
.execute()
Expand Down
2 changes: 2 additions & 0 deletions src/build/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ impl Builder {
/// ```
pub fn update_bucket(mut self, bucket_id: &str, body: &str) -> Executor {
self.headers
.lock()
.unwrap()
.insert("Content-Type", HeaderValue::from_static("application/json"));
self.method = Method::PUT;
self.url
Expand Down
46 changes: 32 additions & 14 deletions src/build/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::{Arc, Mutex};

use super::executor::Executor;
use reqwest::{
header::{HeaderMap, HeaderValue, IntoHeaderName},
Expand All @@ -14,8 +16,8 @@ pub enum BodyType {

pub struct Builder {
pub url: Url,
pub headers: HeaderMap,
pub client: Client,
pub headers: Arc<Mutex<HeaderMap>>,
pub client: Arc<Mutex<Client>>,
pub method: Method,
pub body: Option<BodyType>,
}
Expand All @@ -35,11 +37,12 @@ impl Builder {
/// use reqwest::header::{HeaderMap, HeaderValue};
/// use reqwest::Client;
/// use url::Url;
/// use std::sync::{Arc, Mutex};
///
/// let url = Url::parse("http://localhost").unwrap();
/// let builder = Builder::new(url, HeaderMap::new(), Client::new());
/// let builder = Builder::new(url, Arc::new(Mutex::new(HeaderMap::new())), Arc::new(Mutex::new(Client::new())));
/// ```
pub fn new(url: Url, headers: HeaderMap, client: Client) -> Self {
pub fn new(url: Url, headers: Arc<Mutex<HeaderMap>>, client: Arc<Mutex<Client>>) -> Self {
Self {
url,
headers,
Expand All @@ -61,10 +64,14 @@ impl Builder {
// .body(self.body.unwrap_or_default())
// }
pub fn build(self) -> RequestBuilder {
// let headers = self.headers.lock().unwrap();
let headers = Arc::try_unwrap(self.headers).unwrap();
let mut request = self
.client
.lock()
.unwrap()
.request(self.method, self.url.to_string())
.headers(self.headers);
.headers(headers.into_inner().unwrap());

if let Some(body) = self.body {
match body {
Expand Down Expand Up @@ -94,14 +101,15 @@ impl Builder {
/// use reqwest::header::{HeaderMap, HeaderValue};
/// use reqwest::Client;
/// use url::Url;
/// use std::sync::{Arc, Mutex};
///
/// let url = Url::parse("http://localhost").unwrap();
///
/// let _ = Builder::new(url, HeaderMap::new(), Client::new())
/// let _ = Builder::new(url, Arc::new(Mutex::new(HeaderMap::new())), Arc::new(Mutex::new(Client::new())))
/// .header("Authorization", HeaderValue::from_static("Bearer <token>"));
/// ```
pub fn header(mut self, key: impl IntoHeaderName, value: HeaderValue) -> Self {
self.headers.insert(key, value);
pub fn header(self, key: impl IntoHeaderName, value: HeaderValue) -> Self {
self.headers.lock().unwrap().insert(key, value);
self
}

Expand All @@ -118,14 +126,15 @@ impl Builder {
/// use reqwest::header::{HeaderMap, HeaderValue};
/// use reqwest::Client;
/// use url::Url;
/// use std::sync::{Arc, Mutex};
///
/// #[tokio::main]
/// async fn main() {
/// let url = Url::parse("http://localhost").unwrap();
/// let mut headers = HeaderMap::new();
/// headers.insert("Authorization", HeaderValue::from_static("Bearer YOUR_ACCESS_TOKEN"));
///
/// let builder = Builder::new(url, headers, Client::new())
/// let builder = Builder::new(url, Arc::new(Mutex::new(headers)), Arc::new(Mutex::new(Client::new())))
/// .header("Authorization", HeaderValue::from_static("Bearer <token>"));
///
/// // Execute the request and handle the response
Expand Down Expand Up @@ -161,6 +170,7 @@ mod test {
header::{HeaderMap, HeaderValue},
Client,
};
use std::sync::{Arc, Mutex};
use url::Url;

use super::Builder;
Expand All @@ -170,16 +180,24 @@ mod test {
let mut headers = HeaderMap::new();
headers.insert("Authorization", HeaderValue::from_static("Bearer test"));
let url = Url::parse("http://localhost").unwrap();
let builder = Builder::new(url, headers, Client::new());
let builder = Builder::new(
url,
Arc::new(Mutex::new(headers)),
Arc::new(Mutex::new(Client::new())),
);
assert_eq!(builder.url.scheme(), "http");
assert_eq!(builder.headers.len(), 1);
assert_eq!(builder.headers.lock().unwrap().len(), 1);
}

#[test]
fn test_add_header() {
let url = Url::parse("http://localhost").unwrap();
let builder = Builder::new(url, HeaderMap::new(), Client::new())
.header("Authorization", HeaderValue::from_static("Bearer test"));
assert_eq!(builder.headers.len(), 1);
let builder = Builder::new(
url,
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.header("Authorization", HeaderValue::from_static("Bearer test"));
assert_eq!(builder.headers.lock().unwrap().len(), 1);
}
}
19 changes: 16 additions & 3 deletions src/build/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ impl Builder {
/// ```
pub fn delete_objects(mut self, bucket_id: &str, body: &str) -> Executor {
self.headers
.lock()
.unwrap()
.insert("Content-Type", HeaderValue::from_static("application/json"));
self.url
.path_segments_mut()
Expand Down Expand Up @@ -159,6 +161,8 @@ impl Builder {
.first_or_octet_stream()
.to_string();
self.headers
.lock()
.unwrap()
.insert("Content-Type", HeaderValue::from_str(&mime).unwrap());

self.url
Expand Down Expand Up @@ -295,6 +299,8 @@ impl Builder {
/// ```
pub fn download_object(mut self, bucket_id: &str) -> Executor {
self.headers
.lock()
.unwrap()
.insert("Content-Type", HeaderValue::from_static("application/json"));
self.method = Method::POST;
self.url
Expand All @@ -309,6 +315,7 @@ impl Builder {
#[cfg(test)]
mod test {
use reqwest::{header::HeaderMap, Client};
use std::sync::{Arc, Mutex};
use url::{Host, Origin};

use crate::build::builder::Builder;
Expand All @@ -317,13 +324,19 @@ mod test {
fn test_download_object() {
let executor = Builder::new(
url::Url::parse("http://localhost").unwrap(),
HeaderMap::new(),
Client::new(),
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.download_object("test_bucket");

assert_eq!(
executor.builder.headers.get("Content-Type").unwrap(),
executor
.builder
.headers
.lock()
.unwrap()
.get("Content-Type")
.unwrap(),
"application/json"
);
assert_eq!(executor.builder.url.path(), "/object/test_bucket");
Expand Down
15 changes: 12 additions & 3 deletions src/build/object/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ impl Builder {
/// ```
pub fn list_objects(mut self, bucket_id: &str, body: &str) -> Executor {
self.headers
.lock()
.unwrap()
.insert("Content-Type", HeaderValue::from_static("application/json"));
self.method = Method::POST;
self.url
Expand All @@ -66,6 +68,7 @@ impl Builder {
#[cfg(test)]
mod test {
use reqwest::{header::HeaderMap, Client};
use std::sync::{Arc, Mutex};
use url::{Host, Origin};

use crate::build::builder::{BodyType, Builder};
Expand All @@ -74,13 +77,19 @@ mod test {
fn test_list_objects() {
let executor = Builder::new(
url::Url::parse("http://localhost").unwrap(),
HeaderMap::new(),
Client::new(),
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.list_objects("test_bucket", r#"{"test": "body"}"#);

assert_eq!(
executor.builder.headers.get("Content-Type").unwrap(),
executor
.builder
.headers
.lock()
.unwrap()
.get("Content-Type")
.unwrap(),
"application/json"
);
assert_eq!(executor.builder.url.path(), "/object/list/test_bucket");
Expand Down
28 changes: 19 additions & 9 deletions src/build/object/move_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ impl From<Action> for &str {
impl Builder {
pub(crate) fn action_intern(mut self, move_obj: MoveCopyObject, action: &str) -> Executor {
self.headers
.lock()
.unwrap()
.insert("Content-Type", HeaderValue::from_static("application/json"));
self.method = Method::POST;
self.url
Expand Down Expand Up @@ -221,6 +223,8 @@ impl Builder {

#[cfg(test)]
mod test {
use std::sync::{Arc, Mutex};

use reqwest::{header::HeaderMap, Client, Method};
use url::{Host, Origin};

Expand All @@ -233,13 +237,19 @@ mod test {
fn test_copy_object() {
let executor = Builder::new(
url::Url::parse("http://localhost").unwrap(),
HeaderMap::new(),
Client::new(),
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.copy_object("thefux", "from", "to");

assert_eq!(
executor.builder.headers.get("Content-Type").unwrap(),
executor
.builder
.headers
.lock()
.unwrap()
.get("Content-Type")
.unwrap(),
"application/json"
);

Expand All @@ -265,8 +275,8 @@ mod test {
fn test_move_object() {
let executor = Builder::new(
url::Url::parse("http://localhost").unwrap(),
HeaderMap::new(),
Client::new(),
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.move_object("thefux", "from", "to");

Expand All @@ -277,8 +287,8 @@ mod test {
fn test_move_object_from() {
let executor = Builder::new(
url::Url::parse("http://localhost").unwrap(),
HeaderMap::new(),
Client::new(),
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.move_object_from(MoveCopyObject {
bucket_id: "thefux".to_string(),
Expand All @@ -302,8 +312,8 @@ mod test {
fn test_copy_object_from() {
let executor = Builder::new(
url::Url::parse("http://localhost").unwrap(),
HeaderMap::new(),
Client::new(),
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.copy_object_from(MoveCopyObject {
bucket_id: "thefux".to_string(),
Expand Down
10 changes: 6 additions & 4 deletions src/build/object/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ impl Builder {

#[cfg(test)]
mod test {
use std::sync::{Arc, Mutex};

use reqwest::{header::HeaderMap, Client, Method};
use url::{Host, Origin};

Expand All @@ -100,8 +102,8 @@ mod test {
fn test_get_public_object() {
let executor = Builder::new(
url::Url::parse("http://localhost").unwrap(),
HeaderMap::new(),
Client::new(),
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.get_public_object("thefux", "test.pdf");

Expand All @@ -120,8 +122,8 @@ mod test {
fn test_get_public_object_info() {
let executor = Builder::new(
url::Url::parse("http://localhost").unwrap(),
HeaderMap::new(),
Client::new(),
Arc::new(Mutex::new(HeaderMap::new())),
Arc::new(Mutex::new(Client::new())),
)
.get_public_object_info("thefux", "test.pdf");

Expand Down
Loading

0 comments on commit d308a29

Please sign in to comment.