Skip to content

Remove use of HashMap #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions influxdb/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use futures::prelude::*;
use crate::query::QueryType;
use crate::Error;
use crate::Query;
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Clone, Debug)]
/// Internal Representation of a Client
pub struct Client {
pub(crate) url: Arc<String>,
pub(crate) parameters: Arc<HashMap<&'static str, String>>,
pub(crate) database: (&'static str, String),
pub(crate) credentials: Option<([(&'static str, String); 2])>,
pub(crate) client: reqwest::Client,
}

Expand All @@ -51,12 +51,11 @@ impl Client {
S1: Into<String>,
S2: Into<String>,
{
let mut parameters = HashMap::<&str, String>::new();
parameters.insert("db", database.into());
Client {
url: Arc::new(url.into()),
parameters: Arc::new(parameters),
client: reqwest::Client::new(),
database: ("db", database.into()),
credentials: None,
}
}

Expand All @@ -79,17 +78,14 @@ impl Client {
S1: Into<String>,
S2: Into<String>,
{
let mut with_auth = self.parameters.as_ref().clone();
with_auth.insert("u", username.into());
with_auth.insert("p", password.into());
self.parameters = Arc::new(with_auth);
self.credentials = Some([("u", username.into()), ("p", password.into())]);
self
}

/// Returns the name of the database the client is using
pub fn database_name(&self) -> &str {
// safe to unwrap: we always set the database name in `Self::new`
self.parameters.get("db").unwrap()
&self.database.1
}

/// Returns the URL of the InfluxDB installation the client is using
Expand Down Expand Up @@ -183,29 +179,22 @@ impl Client {
QueryType::ReadQuery => {
let read_query = query.get();
let url = &format!("{}/query", &self.url);
let mut parameters = self.parameters.as_ref().clone();
parameters.insert("q", read_query.clone());

if read_query.contains("SELECT") || read_query.contains("SHOW") {
self.client
.request(reqwest::Method::GET, url)
.query(&parameters)
self.build_db_request(reqwest::Method::GET, url)
.query(&[("q", read_query)])
.build()
} else {
self.client
.request(reqwest::Method::POST, url)
.query(&parameters)
self.build_db_request(reqwest::Method::POST, url)
.query(&[("q", read_query)])
.build()
}
}
QueryType::WriteQuery(precision) => {
let url = &format!("{}/write", &self.url);
let mut parameters = self.parameters.as_ref().clone();
parameters.insert("precision", precision);
self.client
.request(reqwest::Method::POST, url)
self.build_db_request(reqwest::Method::POST, url)
.body(query.get())
.query(&parameters)
.query(&[("precision", precision)])
.build()
}
}
Expand Down Expand Up @@ -243,6 +232,16 @@ impl Client {

Ok(s)
}

pub fn build_db_request(&self, method: reqwest::Method, url: &str) -> reqwest::RequestBuilder {
let mut req = self.client.request(method, url).query(&[&self.database]);

if let Some(credentials) = &self.credentials {
req = req.query(&credentials);
}

req
}
}

#[cfg(test)]
Expand All @@ -259,13 +258,12 @@ mod tests {
#[test]
fn test_with_auth() {
let client = Client::new("http://localhost:8068", "database");
assert_eq!(client.parameters.len(), 1);
assert_eq!(client.parameters.get("db").unwrap(), "database");
assert_eq!(client.database, ("db", "database".to_string()));

let with_auth = client.with_auth("username", "password");
assert_eq!(with_auth.parameters.len(), 3);
assert_eq!(with_auth.parameters.get("db").unwrap(), "database");
assert_eq!(with_auth.parameters.get("u").unwrap(), "username");
assert_eq!(with_auth.parameters.get("p").unwrap(), "password");
assert_eq!(
with_auth.credentials,
Some([("u", "username".to_string()), ("p", "password".to_string()),])
);
}
}
7 changes: 2 additions & 5 deletions influxdb/src/integrations/serde_integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,9 @@ impl Client {
}

let url = &format!("{}/query", &self.url);
let mut parameters = self.parameters.as_ref().clone();
parameters.insert("q", read_query);
let request = self
.client
.request(reqwest::Method::GET, url)
.query(&parameters)
.build_db_request(reqwest::Method::GET, url)
.query(&[("q", read_query)])
.build()
.map_err(|err| Error::UrlConstructionError {
error: err.to_string(),
Expand Down