Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v2 query params #153

Closed
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
28 changes: 25 additions & 3 deletions influxdb/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Client {
pub(crate) url: Arc<String>,
pub(crate) parameters: Arc<HashMap<&'static str, String>>,
pub(crate) token: Option<String>,
pub(crate) org: Option<String>,
pub(crate) client: HttpClient,
}

Expand Down Expand Up @@ -80,16 +81,21 @@ impl Client {
#[must_use = "Creating a client is pointless unless you use it"]
pub fn new<S1, S2>(url: S1, database: S2) -> Self
where
S1: Into<String>,
S1: Into<String> + Clone,
S2: Into<String>,
{
let mut parameters = HashMap::<&str, String>::new();
parameters.insert("db", database.into());

let url_clone = url.clone(); // Clone url

parameters.insert("bucket", database.into());

Client {
url: Arc::new(url.into()),
parameters: Arc::new(parameters),
client: HttpClient::new(),
token: None,
org: None,
}
}

Expand Down Expand Up @@ -140,6 +146,18 @@ 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<S>(mut self, org: S) -> Self
where
S: Into<String>,
{
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`
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand Down