Skip to content

Commit 4eac10e

Browse files
author
Altan Özlü
authored
Merge pull request #3 from celsworth/remove-hashmap
Remove use of HashMap
2 parents 52584e2 + 1b12360 commit 4eac10e

File tree

2 files changed

+29
-34
lines changed
  • influxdb/src

2 files changed

+29
-34
lines changed

influxdb/src/client/mod.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ use futures::prelude::*;
2020
use crate::query::QueryType;
2121
use crate::Error;
2222
use crate::Query;
23-
use std::collections::HashMap;
2423
use std::sync::Arc;
2524

2625
#[derive(Clone, Debug)]
2726
/// Internal Representation of a Client
2827
pub struct Client {
2928
pub(crate) url: Arc<String>,
30-
pub(crate) parameters: Arc<HashMap<&'static str, String>>,
29+
pub(crate) database: (&'static str, String),
30+
pub(crate) credentials: Option<([(&'static str, String); 2])>,
3131
pub(crate) client: reqwest::Client,
3232
}
3333

@@ -51,12 +51,11 @@ impl Client {
5151
S1: Into<String>,
5252
S2: Into<String>,
5353
{
54-
let mut parameters = HashMap::<&str, String>::new();
55-
parameters.insert("db", database.into());
5654
Client {
5755
url: Arc::new(url.into()),
58-
parameters: Arc::new(parameters),
5956
client: reqwest::Client::new(),
57+
database: ("db", database.into()),
58+
credentials: None,
6059
}
6160
}
6261

@@ -79,17 +78,14 @@ impl Client {
7978
S1: Into<String>,
8079
S2: Into<String>,
8180
{
82-
let mut with_auth = self.parameters.as_ref().clone();
83-
with_auth.insert("u", username.into());
84-
with_auth.insert("p", password.into());
85-
self.parameters = Arc::new(with_auth);
81+
self.credentials = Some([("u", username.into()), ("p", password.into())]);
8682
self
8783
}
8884

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

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

189183
if read_query.contains("SELECT") || read_query.contains("SHOW") {
190-
self.client
191-
.request(reqwest::Method::GET, url)
192-
.query(&parameters)
184+
self.build_db_request(reqwest::Method::GET, url)
185+
.query(&[("q", read_query)])
193186
.build()
194187
} else {
195-
self.client
196-
.request(reqwest::Method::POST, url)
197-
.query(&parameters)
188+
self.build_db_request(reqwest::Method::POST, url)
189+
.query(&[("q", read_query)])
198190
.build()
199191
}
200192
}
201193
QueryType::WriteQuery(precision) => {
202194
let url = &format!("{}/write", &self.url);
203-
let mut parameters = self.parameters.as_ref().clone();
204-
parameters.insert("precision", precision);
205-
self.client
206-
.request(reqwest::Method::POST, url)
195+
self.build_db_request(reqwest::Method::POST, url)
207196
.body(query.get())
208-
.query(&parameters)
197+
.query(&[("precision", precision)])
209198
.build()
210199
}
211200
}
@@ -243,6 +232,16 @@ impl Client {
243232

244233
Ok(s)
245234
}
235+
236+
pub fn build_db_request(&self, method: reqwest::Method, url: &str) -> reqwest::RequestBuilder {
237+
let mut req = self.client.request(method, url).query(&[&self.database]);
238+
239+
if let Some(credentials) = &self.credentials {
240+
req = req.query(&credentials);
241+
}
242+
243+
req
244+
}
246245
}
247246

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

265263
let with_auth = client.with_auth("username", "password");
266-
assert_eq!(with_auth.parameters.len(), 3);
267-
assert_eq!(with_auth.parameters.get("db").unwrap(), "database");
268-
assert_eq!(with_auth.parameters.get("u").unwrap(), "username");
269-
assert_eq!(with_auth.parameters.get("p").unwrap(), "password");
264+
assert_eq!(
265+
with_auth.credentials,
266+
Some([("u", "username".to_string()), ("p", "password".to_string()),])
267+
);
270268
}
271269
}

influxdb/src/integrations/serde_integration/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,9 @@ impl Client {
139139
}
140140

141141
let url = &format!("{}/query", &self.url);
142-
let mut parameters = self.parameters.as_ref().clone();
143-
parameters.insert("q", read_query);
144142
let request = self
145-
.client
146-
.request(reqwest::Method::GET, url)
147-
.query(&parameters)
143+
.build_db_request(reqwest::Method::GET, url)
144+
.query(&[("q", read_query)])
148145
.build()
149146
.map_err(|err| Error::UrlConstructionError {
150147
error: err.to_string(),

0 commit comments

Comments
 (0)