From 78a0e2b127f2a8954391651d80b2b98b8802b42e Mon Sep 17 00:00:00 2001 From: Sree Grandhe Date: Wed, 5 Jun 2024 21:07:04 -0500 Subject: [PATCH 1/3] Add v2 query params --- influxdb/src/client/mod.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index 264cfde..ffad5fc 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -34,6 +34,7 @@ pub struct Client { pub(crate) url: Arc, pub(crate) parameters: Arc>, pub(crate) token: Option, + pub(crate) org: Option, pub(crate) client: HttpClient, } @@ -80,16 +81,25 @@ impl Client { #[must_use = "Creating a client is pointless unless you use it"] pub fn new(url: S1, database: S2) -> Self where - S1: Into, + S1: Into + Clone, S2: Into, { let mut parameters = HashMap::<&str, String>::new(); - parameters.insert("db", database.into()); + + let url_clone = url.clone(); // Clone url + + if url_clone.into().contains("/api/v2") { + parameters.insert("bucket", database.into()); + } else { + parameters.insert("db", database.into()); + } + Client { url: Arc::new(url.into()), parameters: Arc::new(parameters), client: HttpClient::new(), token: None, + org: None, } } @@ -140,6 +150,14 @@ impl Client { self } + pub fn with_org(mut self, org: S) -> Self + where + S: Into, + { + self.org = Some(org.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` @@ -233,7 +251,7 @@ impl Client { })?; let mut parameters = self.parameters.as_ref().clone(); - let request_builder = match q.get_type() { + let mut request_builder = match q.get_type() { QueryType::ReadQuery => { let read_query = query.get(); let url = &format!("{}/query", &self.url); @@ -259,6 +277,10 @@ impl Client { error: err.to_string(), })?; + if let Some(ref org) = self.org { + request_builder = request_builder.query(&[("org", org)]); + } + let res = self .auth_if_needed(request_builder) .send() From 8eeb1d74f13c25001f3c274a0922824cafa710a5 Mon Sep 17 00:00:00 2001 From: Sree Grandhe Date: Wed, 5 Jun 2024 21:10:53 -0500 Subject: [PATCH 2/3] Add comments and linting for org --- influxdb/src/client/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index ffad5fc..205c872 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -93,7 +93,7 @@ impl Client { } else { parameters.insert("db", database.into()); } - + Client { url: Arc::new(url.into()), parameters: Arc::new(parameters), @@ -150,6 +150,10 @@ impl Client { self } + /// Add organization to [`Client`](crate::Client) + /// + /// This is designed for influxdb 2.0's backward-compatible API which + /// requires organization by default. pub fn with_org(mut self, org: S) -> Self where S: Into, From 36476db8e0bda4fd3f6ae1090c86072f645a06b4 Mon Sep 17 00:00:00 2001 From: Noah Hanover Date: Tue, 2 Jul 2024 14:24:53 -0500 Subject: [PATCH 3/3] change conditiona --- influxdb/src/client/mod.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/influxdb/src/client/mod.rs b/influxdb/src/client/mod.rs index 205c872..bf1d579 100644 --- a/influxdb/src/client/mod.rs +++ b/influxdb/src/client/mod.rs @@ -88,11 +88,7 @@ impl Client { let url_clone = url.clone(); // Clone url - if url_clone.into().contains("/api/v2") { - parameters.insert("bucket", database.into()); - } else { - parameters.insert("db", database.into()); - } + parameters.insert("bucket", database.into()); Client { url: Arc::new(url.into()),