diff --git a/Cargo.lock b/Cargo.lock index 0bd4d22..b3b2437 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,7 +77,7 @@ dependencies = [ [[package]] name = "firebase-rs" -version = "2.0.5" +version = "2.0.6" dependencies = [ "itertools", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index e057a06..6fc15b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,10 +13,13 @@ keywords = ["firebase", "rest", "api", "web", "database"] [dependencies] url = "2.2.2" -reqwest = { version = "0.11.11", features = ["json"] } +reqwest = { version = "0.11.11", optional = true, features = ["json"] } serde_json = "1.0.82" serde = { version = "1.0.139", features = ["derive"] } itertools = "0.10.5" [dev-dependencies] tokio = { version = "1.20.0", features = ["rt", "macros"] } + +[features] +default = ["reqwest"] diff --git a/src/lib.rs b/src/lib.rs index 5cd2428..2dae09b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ use constants::{Method, Response, AUTH}; use errors::{RequestError, RequestResult, UrlParseError, UrlParseResult}; use params::Params; -use reqwest::StatusCode; +use reqwest::{Client, StatusCode}; use serde::de::DeserializeOwned; use serde::Serialize; use serde_json::Value; @@ -26,11 +26,13 @@ impl Firebase { /// let firebase = Firebase::new("https://myfirebase.firebaseio.com").unwrap(); /// ``` pub fn new(uri: &str) -> UrlParseResult - where - Self: Sized, + where + Self: Sized, { match check_uri(&uri) { - Ok(uri) => Ok(Self { uri }), + Ok(uri) => Ok(Self { + uri, + }), Err(err) => Err(err), } } @@ -38,16 +40,18 @@ impl Firebase { /// ``` /// use firebase_rs::Firebase; /// - /// let firebase = Firebase::new("https://myfirebase.firebaseio.com").unwrap(); + /// let firebase = Firebase::auth("https://myfirebase.firebaseio.com", "my_auth_key").unwrap(); /// ``` pub fn auth(uri: &str, auth_key: &str) -> UrlParseResult - where - Self: Sized, + where + Self: Sized, { match check_uri(&uri) { Ok(mut uri) => { uri.set_query(Some(&format!("{}={}", AUTH, auth_key))); - Ok(Self { uri }) + Ok(Self { + uri, + }) } Err(err) => Err(err), } @@ -71,28 +75,20 @@ impl Firebase { /// /// let firebase = Firebase::new("https://myfirebase.firebaseio.com").unwrap().at("users").at("USER_ID").at("f69111a8a5258c15286d3d0bd4688c55"); /// ``` - pub fn at(&self, path: &str) -> Self { - let mut new_path = String::default(); - - let paths = self.uri.path_segments().map(|p| p.collect::>()); - for mut path in paths.unwrap() { - if path.find(".json").is_some() { - path = path.trim_end_matches(".json"); - } - new_path += format!("{}/", path).as_str(); - } + pub fn at(&mut self, path: &str) -> &mut Self { + let re_path: String = self + .uri + .path_segments() + .unwrap_or_else(|| panic!("cannot be base")) + .map(|seg| format!("{}/", seg.trim_end_matches(".json"))) + .collect(); - new_path += path; + let new_path = re_path + path; - if new_path.find(".json").is_some() { - new_path = new_path.trim_end_matches(".json").to_string(); - } + self.uri + .set_path(&format!("{}.json", new_path.trim_end_matches(".json"))); - let mut uri = self.uri.clone(); - uri.set_path(&format!("{}.json", new_path)); - Self { - uri, - } + self } /// ``` @@ -106,7 +102,7 @@ impl Firebase { } async fn request(&self, method: Method, data: Option) -> RequestResult { - let client = reqwest::Client::new(); + let client = Client::new(); return match method { Method::GET => { @@ -171,8 +167,8 @@ impl Firebase { } async fn request_generic(&self, method: Method) -> RequestResult - where - T: Serialize + DeserializeOwned + Debug, + where + T: Serialize + DeserializeOwned + Debug, { let request = self.request(method, None).await; @@ -202,8 +198,8 @@ impl Firebase { /// # } /// ``` pub async fn set(&self, data: &T) -> RequestResult - where - T: Serialize + DeserializeOwned + Debug, + where + T: Serialize + DeserializeOwned + Debug, { let data = serde_json::to_value(&data).unwrap(); self.request(Method::POST, Some(data)).await @@ -249,8 +245,8 @@ impl Firebase { /// # } /// ``` pub async fn get(&self) -> RequestResult - where - T: Serialize + DeserializeOwned + Debug, + where + T: Serialize + DeserializeOwned + Debug, { self.request_generic::(Method::GET).await } @@ -283,8 +279,8 @@ impl Firebase { /// # } /// ``` pub async fn update(&self, data: &T) -> RequestResult - where - T: DeserializeOwned + Serialize + Debug, + where + T: DeserializeOwned + Serialize + Debug, { let value = serde_json::to_value(&data).unwrap(); self.request(Method::PATCH, Some(value)).await diff --git a/src/params.rs b/src/params.rs index 3ea76c7..6ceb835 100644 --- a/src/params.rs +++ b/src/params.rs @@ -2,9 +2,9 @@ use crate::constants::{ END_AT, EQUAL_TO, EXPORT, FORMAT, LIMIT_TO_FIRST, LIMIT_TO_LAST, ORDER_BY, SHALLOW, START_AT, }; use crate::Firebase; +use itertools::Itertools; use std::collections::HashMap; use url::Url; -use itertools::Itertools; #[derive(Debug)] pub struct Params { @@ -27,11 +27,10 @@ impl Params { } pub fn add_param(&mut self, key: &str, value: T) -> &mut Self - where - T: ToString, + where + T: ToString, { self.params.insert(key.to_string(), value.to_string()); - self.set_params(); self } @@ -68,26 +67,34 @@ impl Params { self.add_param(FORMAT, EXPORT) } - pub fn finish(&self) -> Firebase { + pub fn finish(&mut self) -> Firebase { + self.set_params(); Firebase::new(self.uri.as_str()).unwrap() } } - #[cfg(test)] mod tests { + use crate::params::Params; use std::collections::HashMap; use url::Url; - use crate::params::Params; #[test] fn check_params() { - let mut params: HashMap = HashMap::new(); - params.insert("param_1".to_owned(), "value_1".to_owned()); - params.insert("param_2".to_owned(), "value_2".to_owned()); - let mut param = Params { uri: Url::parse("https://github.com/emreyalvac").unwrap(), params }; + let mut params = HashMap::new(); + params.insert("param_1", "value_1"); + params.insert("param_2", "value_2"); + + let mut param = Params::new(Url::parse("https://github.com/emreyalvac").unwrap()); + + for (k, v) in params { + param.add_param(&k, v); + } param.set_params(); - assert_eq!(param.uri.as_str(), "https://github.com/emreyalvac?param_1=value_1¶m_2=value_2") + assert_eq!( + param.uri.as_str(), + "https://github.com/emreyalvac?param_1=value_1¶m_2=value_2" + ) } -} \ No newline at end of file +} diff --git a/src/utils.rs b/src/utils.rs index 9ed4dcf..95b6fdf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,7 +3,7 @@ use crate::UrlParseError; use url::Url; pub fn check_uri(uri: &str) -> UrlParseResult { - let uri = Url::parse(uri); + let uri = Url::parse(uri.trim_end_matches("/")); let uri = match uri { Ok(res) => res,