Skip to content

Commit

Permalink
multiple params support implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
emreyalvac committed Jan 29, 2023
1 parent c1c32b6 commit f38e8c3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ url = "2.2.2"
reqwest = { version = "0.11.11", 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"] }
29 changes: 25 additions & 4 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::constants::{
use crate::Firebase;
use std::collections::HashMap;
use url::Url;
use itertools::Itertools;

#[derive(Debug)]
pub struct Params {
pub uri: Url,
pub params: HashMap<String, String>,
Expand All @@ -19,14 +21,14 @@ impl Params {
}

pub fn set_params(&mut self) -> () {
for (k, v) in self.params.iter() {
self.uri.set_query(Some(format!("{}={}", k, v).as_str()));
for (k, v) in self.params.iter().sorted() {
self.uri.query_pairs_mut().append_pair(k, v);
}
}

pub fn add_param<T>(&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();
Expand Down Expand Up @@ -70,3 +72,22 @@ impl Params {
Firebase::new(self.uri.as_str()).unwrap()
}
}


#[cfg(test)]
mod tests {
use std::collections::HashMap;
use url::Url;
use crate::params::Params;

#[test]
fn check_params() {
let mut params: HashMap<String, String> = 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 };
param.set_params();

assert_eq!(param.uri.as_str(), "https://github.com/emreyalvac?param_1=value_1&param_2=value_2")
}
}

0 comments on commit f38e8c3

Please sign in to comment.