Skip to content

Commit ff46a3e

Browse files
committed
parametrize influxdb client version
1 parent b3598e8 commit ff46a3e

File tree

8 files changed

+143
-61
lines changed

8 files changed

+143
-61
lines changed

benches/client.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use chrono::{DateTime, Utc};
22
use influxdb::Error;
33
use influxdb::InfluxDbWriteable;
4+
use influxdb::InfluxVersion1;
45
use influxdb::{Client, ReadQuery};
56
use std::sync::Arc;
67
use std::time::Instant;
@@ -22,7 +23,7 @@ async fn main() {
2223
let number_of_total_requests = 20000;
2324
let concurrent_requests = 1000;
2425

25-
let client = Client::new(url, db_name);
26+
let client: Client<InfluxVersion1> = Client::new(url, db_name);
2627
let concurrency_limit = Arc::new(Semaphore::new(concurrent_requests));
2728

2829
prepare_influxdb(&client, db_name).await;
@@ -64,7 +65,7 @@ async fn main() {
6465
);
6566
}
6667

67-
async fn prepare_influxdb(client: &Client, db_name: &str) {
68+
async fn prepare_influxdb<V>(client: &Client<V>, db_name: &str) {
6869
let create_db_stmt = format!("CREATE DATABASE {}", db_name);
6970
client
7071
.query(&ReadQuery::new(create_db_stmt))

influxdb/src/client/mod.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,30 @@ use std::fmt::{self, Debug, Formatter};
2323
use std::sync::Arc;
2424
#[cfg(feature = "surf")]
2525
use surf::{Client as HttpClient, RequestBuilder, Response as HttpResponse};
26+
use std::marker::PhantomData;
2627

2728
use crate::query::QueryType;
2829
use crate::Error;
2930
use crate::Query;
3031

32+
/// Marker type for InfluxDB Version 1
33+
#[derive(Clone)]
34+
pub struct InfluxVersion1;
35+
/// Marker type for InfluxDB Version 2
36+
#[derive(Clone)]
37+
pub struct InfluxVersion2;
38+
/// Marker type for InfluxDB Version 3
39+
#[derive(Clone)]
40+
pub struct InfluxVersion3;
41+
3142
#[derive(Clone)]
3243
/// Internal Representation of a Client
33-
pub struct Client {
44+
pub struct Client<V, H = reqwest::Client> {
3445
pub(crate) url: Arc<String>,
3546
pub(crate) parameters: Arc<HashMap<&'static str, String>>,
3647
pub(crate) token: Option<String>,
37-
pub(crate) client: HttpClient,
48+
pub(crate) client: H,
49+
_version: PhantomData<V>,
3850
}
3951

4052
struct RedactPassword<'a>(&'a HashMap<&'static str, String>);
@@ -53,7 +65,7 @@ impl<'a> Debug for RedactPassword<'a> {
5365
}
5466
}
5567

56-
impl Debug for Client {
68+
impl<V, H> Debug for Client<V, H> {
5769
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
5870
f.debug_struct("Client")
5971
.field("url", &self.url)
@@ -62,7 +74,7 @@ impl Debug for Client {
6274
}
6375
}
6476

65-
impl Client {
77+
impl<V> Client<V, reqwest::Client> {
6678
/// Instantiates a new [`Client`](crate::Client)
6779
///
6880
/// # Arguments
@@ -90,6 +102,7 @@ impl Client {
90102
parameters: Arc::new(parameters),
91103
client: HttpClient::new(),
92104
token: None,
105+
_version: PhantomData,
93106
}
94107
}
95108

@@ -308,12 +321,15 @@ pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> {
308321

309322
#[cfg(test)]
310323
mod tests {
324+
325+
use crate::client::InfluxVersion1;
326+
311327
use super::Client;
312328
use indoc::indoc;
313329

314330
#[test]
315331
fn test_client_debug_redacted_password() {
316-
let client = Client::new("https://localhost:8086", "db").with_auth("user", "pass");
332+
let client: Client<InfluxVersion1> = Client::new("https://localhost:8086", "db").with_auth("user", "pass");
317333
let actual = format!("{client:#?}");
318334
let expected = indoc! { r#"
319335
Client {
@@ -331,14 +347,14 @@ mod tests {
331347

332348
#[test]
333349
fn test_fn_database() {
334-
let client = Client::new("http://localhost:8068", "database");
350+
let client: Client<InfluxVersion1> = Client::new("http://localhost:8068", "database");
335351
assert_eq!(client.database_name(), "database");
336352
assert_eq!(client.database_url(), "http://localhost:8068");
337353
}
338354

339355
#[test]
340356
fn test_with_auth() {
341-
let client = Client::new("http://localhost:8068", "database");
357+
let client: Client<InfluxVersion1> = Client::new("http://localhost:8068", "database");
342358
assert_eq!(client.parameters.len(), 1);
343359
assert_eq!(client.parameters.get("db").unwrap(), "database");
344360

@@ -348,7 +364,7 @@ mod tests {
348364
assert_eq!(with_auth.parameters.get("u").unwrap(), "username");
349365
assert_eq!(with_auth.parameters.get("p").unwrap(), "password");
350366

351-
let client = Client::new("http://localhost:8068", "database");
367+
let client: Client<InfluxVersion1> = Client::new("http://localhost:8068", "database");
352368
let with_auth = client.with_token("token");
353369
assert_eq!(with_auth.parameters.len(), 1);
354370
assert_eq!(with_auth.parameters.get("db").unwrap(), "database");

influxdb/src/integrations/serde_integration/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ pub struct TaggedSeries<TAG, T> {
120120
pub values: Vec<T>,
121121
}
122122

123-
impl Client {
123+
impl<V> Client<V, reqwest::Client> {
124124
pub async fn json_query(&self, q: ReadQuery) -> Result<DatabaseQueryResult, Error> {
125125
let query = q.build().map_err(|err| Error::InvalidQueryError {
126126
error: err.to_string(),

influxdb/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ mod client;
138138
mod error;
139139
mod query;
140140

141-
pub use client::Client;
141+
pub use client::{Client, InfluxVersion1, InfluxVersion2, InfluxVersion3};
142142
pub use error::Error;
143143
pub use query::{
144144
read_query::ReadQuery,

influxdb/tests/derive_integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use influxdb::{Query, ReadQuery, Timestamp};
1010
#[cfg(feature = "serde")]
1111
use serde_derive::Deserialize;
1212

13-
use utilities::{assert_result_ok, create_client, create_db, delete_db, run_test};
13+
use utilities::{assert_result_ok, run_test};
1414

1515
#[derive(Debug, PartialEq)]
1616
#[cfg_attr(feature = "derive", derive(InfluxDbWriteable))]

0 commit comments

Comments
 (0)