Skip to content
Open
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
105 changes: 98 additions & 7 deletions influxdb/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ use crate::query::QueryType;
use crate::Error;
use crate::Query;

#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum InfluxDbVersion {
V1,
V2,
V3,
}

#[derive(Clone)]
/// Internal Representation of a Client
pub struct Client {
pub struct Client<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?

pub(crate) version: InfluxDbVersion,
}

struct RedactPassword<'a>(&'a HashMap<&'static str, String>);
Expand All @@ -50,17 +59,18 @@ impl<'a> Debug for RedactPassword<'a> {
}
}

impl Debug for Client {
impl<H> Debug for Client<H> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Client")
.field("url", &self.url)
.field("parameters", &RedactPassword(&self.parameters))
.field("version", &self.version)
.finish_non_exhaustive()
}
}

impl Client {
/// Instantiates a new [`Client`](crate::Client)
impl Client<reqwest::Client> {
/// Instantiates a new [`Client`](crate::Client) for InfluxDB v1.
///
/// # Arguments
///
Expand All @@ -82,11 +92,78 @@ impl Client {
{
let mut parameters = HashMap::<&str, String>::new();
parameters.insert("db", database.into());
Client {
Self {
url: Arc::new(url.into()),
parameters: Arc::new(parameters),
client: HttpClient::new(),
token: None,
version: InfluxDbVersion::V1,
}
}

/// Instantiates a new [`Client`](crate::Client) for InfluxDB v2.
///
/// # Arguments
///
/// * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`).
/// * `token`: The InfluxDB v2 authentication token.
/// * `bucket`: The InfluxDB v2 bucket.
///
/// # Examples
///
/// ```rust
/// use influxdb::Client;
///
/// let _client = Client::v2("http://localhost:8086", "some-token", "my-bucket");
/// ```
#[must_use = "Creating a client is pointless unless you use it"]
pub fn v2<S1, S2, S3>(url: S1, token: S2, bucket: S3) -> Self
where
S1: Into<String>,
S2: Into<String>,
S3: Into<String>,
{
let mut parameters = HashMap::<&str, String>::new();
parameters.insert("bucket", bucket.into());
Self {
url: Arc::new(url.into()),
parameters: Arc::new(parameters),
client: HttpClient::new(),
token: Some(token.into()),
version: InfluxDbVersion::V2,
}
}

/// Instantiates a new [`Client`](crate::Client) for InfluxDB v3.
///
/// # Arguments
///
/// * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`).
/// * `token`: The InfluxDB v3 authentication token.
/// * `database`: The InfluxDB v3 database.
///
/// # Examples
///
/// ```rust
/// use influxdb::Client;
///
/// let _client = Client::v3("http://localhost:8086", "some-token", "my-database");
/// ```
#[must_use = "Creating a client is pointless unless you use it"]
pub fn v3<S1, S2, S3>(url: S1, token: S2, database: S3) -> Self
where
S1: Into<String>,
S2: Into<String>,
S3: Into<String>,
{
let mut parameters = HashMap::<&str, String>::new();
parameters.insert("db", database.into());
Self {
url: Arc::new(url.into()),
parameters: Arc::new(parameters),
client: HttpClient::new(),
token: Some(token.into()),
version: InfluxDbVersion::V3,
}
}

Expand Down Expand Up @@ -289,7 +366,7 @@ pub(crate) fn check_status(res: &HttpResponse) -> Result<(), Error> {

#[cfg(test)]
mod tests {
use super::Client;
use super::{Client, InfluxDbVersion};
use indoc::indoc;

#[test]
Expand All @@ -304,6 +381,7 @@ mod tests {
"p": "<redacted>",
"u": "user",
},
version: V1,
..
}
"# };
Expand Down Expand Up @@ -335,4 +413,17 @@ mod tests {
assert_eq!(with_auth.parameters.get("db").unwrap(), "database");
assert_eq!(with_auth.token.unwrap(), "token");
}

#[test]
fn test_v2_and_v3_clients() {
let v2_client = Client::v2("http://localhost:8086", "token", "bucket");
assert_eq!(v2_client.version, InfluxDbVersion::V2);
assert_eq!(v2_client.token.unwrap(), "token");
assert_eq!(v2_client.parameters.get("bucket").unwrap(), "bucket");

let v3_client = Client::v3("http://localhost:8086", "token", "database");
assert_eq!(v3_client.version, InfluxDbVersion::V3);
assert_eq!(v3_client.token.unwrap(), "token");
assert_eq!(v3_client.parameters.get("db").unwrap(), "database");
}
}
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 Client<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 @@ -121,7 +121,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