Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions benches/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::{DateTime, Utc};
use influxdb::Error;
use influxdb::InfluxDbWriteable;
use influxdb::InfluxVersion1;
use influxdb::{Client, ReadQuery};
use std::sync::Arc;
use std::time::Instant;
Expand All @@ -22,7 +23,7 @@ async fn main() {
let number_of_total_requests = 20000;
let concurrent_requests = 1000;

let client = Client::new(url, db_name);
let client: Client<InfluxVersion1> = Client::new(url, db_name);
let concurrency_limit = Arc::new(Semaphore::new(concurrent_requests));

prepare_influxdb(&client, db_name).await;
Expand Down Expand Up @@ -64,7 +65,7 @@ async fn main() {
);
}

async fn prepare_influxdb(client: &Client, db_name: &str) {
async fn prepare_influxdb<V>(client: &Client<V>, db_name: &str) {
let create_db_stmt = format!("CREATE DATABASE {}", db_name);
client
.query(&ReadQuery::new(create_db_stmt))
Expand Down
32 changes: 24 additions & 8 deletions influxdb/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,30 @@ use std::fmt::{self, Debug, Formatter};
use std::sync::Arc;
#[cfg(feature = "surf")]
use surf::{Client as HttpClient, RequestBuilder, Response as HttpResponse};
use std::marker::PhantomData;

use crate::query::QueryType;
use crate::Error;
use crate::Query;

/// Marker type for InfluxDB Version 1
#[derive(Clone)]
pub struct InfluxVersion1;
/// Marker type for InfluxDB Version 2
#[derive(Clone)]
pub struct InfluxVersion2;
/// Marker type for InfluxDB Version 3
#[derive(Clone)]
pub struct InfluxVersion3;

#[derive(Clone)]
/// Internal Representation of a Client
pub struct Client {
pub struct Client<V, H = reqwest::Client> {
pub(crate) url: Arc<String>,
pub(crate) parameters: Arc<HashMap<&'static str, String>>,
pub(crate) token: Option<String>,
pub(crate) client: HttpClient,
pub(crate) client: H,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change makes much sense, given that we already decided to sunset surf support in #147

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was thinking of doing #147 first.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, still not sure why the generic?

_version: PhantomData<V>,
}

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

impl Debug for Client {
impl<V, H> Debug for Client<V, H> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Client")
.field("url", &self.url)
Expand All @@ -62,7 +74,7 @@ impl Debug for Client {
}
}

impl Client {
impl<V> Client<V, reqwest::Client> {
/// Instantiates a new [`Client`](crate::Client)
///
/// # Arguments
Expand Down Expand Up @@ -90,6 +102,7 @@ impl Client {
parameters: Arc::new(parameters),
client: HttpClient::new(),
token: None,
_version: PhantomData,
}
}

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

#[cfg(test)]
mod tests {

use crate::client::InfluxVersion1;

use super::Client;
use indoc::indoc;

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

#[test]
fn test_fn_database() {
let client = Client::new("http://localhost:8068", "database");
let client: Client<InfluxVersion1> = Client::new("http://localhost:8068", "database");
assert_eq!(client.database_name(), "database");
assert_eq!(client.database_url(), "http://localhost:8068");
}

#[test]
fn test_with_auth() {
let client = Client::new("http://localhost:8068", "database");
let client: Client<InfluxVersion1> = Client::new("http://localhost:8068", "database");
assert_eq!(client.parameters.len(), 1);
assert_eq!(client.parameters.get("db").unwrap(), "database");

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

let client = Client::new("http://localhost:8068", "database");
let client: Client<InfluxVersion1> = Client::new("http://localhost:8068", "database");
let with_auth = client.with_token("token");
assert_eq!(with_auth.parameters.len(), 1);
assert_eq!(with_auth.parameters.get("db").unwrap(), "database");
Expand Down
2 changes: 1 addition & 1 deletion influxdb/src/integrations/serde_integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub struct TaggedSeries<TAG, T> {
pub values: Vec<T>,
}

impl Client {
impl<V> Client<V, reqwest::Client> {
pub async fn json_query(&self, q: ReadQuery) -> Result<DatabaseQueryResult, Error> {
let query = q.build().map_err(|err| Error::InvalidQueryError {
error: err.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion influxdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ mod client;
mod error;
mod query;

pub use client::Client;
pub use client::{Client, InfluxVersion1, InfluxVersion2, InfluxVersion3};
pub use error::Error;
pub use query::{
read_query::ReadQuery,
Expand Down
2 changes: 1 addition & 1 deletion influxdb/tests/derive_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use influxdb::{Query, ReadQuery, Timestamp};
#[cfg(feature = "serde")]
use serde_derive::Deserialize;

use utilities::{assert_result_ok, create_client, create_db, delete_db, run_test};
use utilities::{assert_result_ok, run_test};

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "derive", derive(InfluxDbWriteable))]
Expand Down
Loading
Loading