Skip to content

Commit

Permalink
Add async-std support and replaced reqwest with surf (#58) (#72)
Browse files Browse the repository at this point in the history
* Replaced tokio with async-std 1.6.5 and reqwest with surf 2.1.0

* Test with both async-std and tokio

* Fix clipy warning

* Add features to choose http lib: hyper-client (default), curl-client, h1-client or wasm-client
  • Loading branch information
JEnoch authored Nov 12, 2020
1 parent 7ee95f4 commit cc8750a
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 103 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ use influxdb::{Client, Query, Timestamp};
use influxdb::InfluxDbWriteable;
use chrono::{DateTime, Utc};

#[tokio::main]
#[async_std::main]
// or #[tokio::main] if you prefer
async fn main() {
// Connect to db `test` on `http://localhost:8086`
let client = Client::new("http://localhost:8086", "test");
Expand Down
3 changes: 1 addition & 2 deletions benches/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use chrono::{DateTime, Utc};
use futures::stream::StreamExt;
use influxdb::Error;
use influxdb::InfluxDbWriteable;
use influxdb::{Client, Query};
Expand Down Expand Up @@ -45,7 +44,7 @@ async fn main() {

let mut successful_count = 0;
let mut error_count = 0;
while let Some(res) = rx.next().await {
while let Some(res) = rx.recv().await {
if res.is_err() {
error_count += 1;
} else {
Expand Down
11 changes: 8 additions & 3 deletions influxdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ futures = "0.3.4"
lazy_static = "1.4.0"
influxdb_derive = { version = "0.2.0", optional = true }
regex = "1.3.5"
reqwest = { version = "0.10.8", features = ["json"] }
surf = { version = "2.1.0", default-features = false }
serde = { version = "1.0.104", features = ["derive"], optional = true }
serde_json = { version = "1.0.48", optional = true }
thiserror = "1.0"

[features]
use-serde = ["serde", "serde_json"]
default = ["use-serde"]
curl-client = ["surf/curl-client"]
h1-client = ["surf/h1-client"]
hyper-client = ["surf/hyper-client"]
wasm-client = ["surf/wasm-client"]
default = ["use-serde", "hyper-client"]
derive = ["influxdb_derive"]

[dev-dependencies]
tokio = { version = "0.2.11", features = ["macros"] }
async-std = { version = "1.6.5", features = ["attributes"] }
tokio = { version = "0.2.22", features = ["rt-threaded", "macros"] }
107 changes: 46 additions & 61 deletions influxdb/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
//! ```
use futures::prelude::*;
use reqwest::{self, Client as ReqwestClient, StatusCode};
use surf::{self, Client as SurfClient, StatusCode};

use crate::query::QueryTypes;
use crate::Error;
use crate::Query;
use std::collections::HashMap;
use std::sync::Arc;

#[derive(Clone, Debug)]
/// Internal Representation of a Client
pub struct Client {
pub(crate) url: Arc<String>,
pub(crate) parameters: Arc<Vec<(&'static str, String)>>,
pub(crate) client: ReqwestClient,
pub(crate) parameters: Arc<HashMap<&'static str, String>>,
pub(crate) client: SurfClient,
}

impl Client {
Expand All @@ -51,10 +52,12 @@ impl Client {
S1: Into<String>,
S2: Into<String>,
{
let mut parameters = HashMap::<&str, String>::new();
parameters.insert("db", database.into());
Client {
url: Arc::new(url.into()),
parameters: Arc::new(vec![("db", database.into())]),
client: ReqwestClient::new(),
parameters: Arc::new(parameters),
client: SurfClient::new(),
}
}

Expand All @@ -78,16 +81,16 @@ impl Client {
S2: Into<String>,
{
let mut with_auth = self.parameters.as_ref().clone();
with_auth.push(("u", username.into()));
with_auth.push(("p", password.into()));
with_auth.insert("u", username.into());
with_auth.insert("p", password.into());
self.parameters = Arc::new(with_auth);
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`
&self.parameters.first().unwrap().1
self.parameters.get("db").unwrap()
}

/// Returns the URL of the InfluxDB installation the client is using
Expand All @@ -109,18 +112,8 @@ impl Client {
error: format!("{}", err),
})?;

let build = res
.headers()
.get("X-Influxdb-Build")
.unwrap()
.to_str()
.unwrap();
let version = res
.headers()
.get("X-Influxdb-Version")
.unwrap()
.to_str()
.unwrap();
let build = res.header("X-Influxdb-Build").unwrap().as_str();
let version = res.header("X-Influxdb-Version").unwrap().as_str();

Ok((build.to_owned(), version.to_owned()))
}
Expand All @@ -140,7 +133,7 @@ impl Client {
/// use influxdb::InfluxDbWriteable;
/// use std::time::{SystemTime, UNIX_EPOCH};
///
/// # #[tokio::main]
/// # #[async_std::main]
/// # async fn main() -> Result<(), influxdb::Error> {
/// let start = SystemTime::now();
/// let since_the_epoch = start
Expand Down Expand Up @@ -169,60 +162,55 @@ impl Client {
&'q Q: Into<QueryTypes<'q>>,
{
let query = q.build().map_err(|err| Error::InvalidQueryError {
error: format!("{}", err),
error: err.to_string(),
})?;

let request_builder = match q.into() {
QueryTypes::Read(_) => {
let read_query = query.get();
let url = &format!("{}/query", &self.url);
let query = [("q", &read_query)];
let mut parameters = self.parameters.as_ref().clone();
parameters.insert("q", read_query.clone());

if read_query.contains("SELECT") || read_query.contains("SHOW") {
self.client
.get(url)
.query(self.parameters.as_ref())
.query(&query)
self.client.get(url).query(&parameters)
} else {
self.client
.post(url)
.query(self.parameters.as_ref())
.query(&query)
self.client.post(url).query(&parameters)
}
}
QueryTypes::Write(write_query) => {
let url = &format!("{}/write", &self.url);
let precision = [("precision", write_query.get_precision())];
let mut parameters = self.parameters.as_ref().clone();
parameters.insert("precision", write_query.get_precision());

self.client
.post(url)
.query(self.parameters.as_ref())
.query(&precision)
.body(query.get())
self.client.post(url).body(query.get()).query(&parameters)
}
};

let request = request_builder
.build()
.map_err(|err| Error::UrlConstructionError {
error: format!("{}", &err),
})?;
}
.map_err(|err| Error::UrlConstructionError {
error: err.to_string(),
})?;

let res = self
let request = request_builder.build();
let mut res = self
.client
.execute(request)
.map_err(|err| Error::ConnectionError { error: err })
.send(request)
.map_err(|err| Error::ConnectionError {
error: err.to_string(),
})
.await?;

match res.status() {
StatusCode::UNAUTHORIZED => return Err(Error::AuthorizationError),
StatusCode::FORBIDDEN => return Err(Error::AuthenticationError),
StatusCode::Unauthorized => return Err(Error::AuthorizationError),
StatusCode::Forbidden => return Err(Error::AuthenticationError),
_ => {}
}

let s = res.text().await.map_err(|_| Error::DeserializationError {
error: "response could not be converted to UTF-8".to_string(),
})?;
let s = res
.body_string()
.await
.map_err(|_| Error::DeserializationError {
error: "response could not be converted to UTF-8".to_string(),
})?;

// todo: improve error parsing without serde
if s.contains("\"error\"") {
Expand All @@ -249,16 +237,13 @@ mod tests {
#[test]
fn test_with_auth() {
let client = Client::new("http://localhost:8068", "database");
assert_eq!(vec![("db", "database".to_string())], *client.parameters);
assert_eq!(client.parameters.len(), 1);
assert_eq!(client.parameters.get("db").unwrap(), "database");

let with_auth = client.with_auth("username", "password");
assert_eq!(
vec![
("db", "database".to_string()),
("u", "username".to_string()),
("p", "password".to_string())
],
*with_auth.parameters
);
assert_eq!(with_auth.parameters.len(), 3);
assert_eq!(with_auth.parameters.get("db").unwrap(), "database");
assert_eq!(with_auth.parameters.get("u").unwrap(), "username");
assert_eq!(with_auth.parameters.get("p").unwrap(), "password");
}
}
7 changes: 2 additions & 5 deletions influxdb/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ pub enum Error {
AuthorizationError,

#[error("connection error: {error}")]
/// Error happens when reqwest fails
ConnectionError {
#[from]
error: reqwest::Error,
},
/// Error happens when HTTP request fails
ConnectionError { error: String },
}
32 changes: 17 additions & 15 deletions influxdb/src/integrations/serde_integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! weather: WeatherWithoutCityName,
//! }
//!
//! # #[tokio::main]
//! # #[async_std::main]
//! # async fn main() -> Result<(), influxdb::Error> {
//! let client = Client::new("http://localhost:8086", "test");
//! let query = Query::raw_read_query(
Expand All @@ -48,7 +48,7 @@
mod de;

use reqwest::StatusCode;
use surf::StatusCode;

use serde::{de::DeserializeOwned, Deserialize};

Expand Down Expand Up @@ -140,31 +140,33 @@ impl Client {
}

let url = &format!("{}/query", &self.url);
let query = [("q", &read_query)];
let mut parameters = self.parameters.as_ref().clone();
parameters.insert("q", read_query);
let request = self
.client
.get(url)
.query(self.parameters.as_ref())
.query(&query)
.build()
.query(&parameters)
.map_err(|err| Error::UrlConstructionError {
error: format!("{}", err),
})?;
error: err.to_string(),
})?
.build();

let res = self
let mut res = self
.client
.execute(request)
.send(request)
.await
.map_err(|err| Error::ConnectionError { error: err })?;
.map_err(|err| Error::ConnectionError {
error: err.to_string(),
})?;

match res.status() {
StatusCode::UNAUTHORIZED => return Err(Error::AuthorizationError),
StatusCode::FORBIDDEN => return Err(Error::AuthenticationError),
StatusCode::Unauthorized => return Err(Error::AuthorizationError),
StatusCode::Forbidden => return Err(Error::AuthenticationError),
_ => {}
}

let body = res.bytes().await.map_err(|err| Error::ProtocolError {
error: format!("{}", err),
let body = res.body_bytes().await.map_err(|err| Error::ProtocolError {
error: err.to_string(),
})?;

// Try parsing InfluxDBs { "error": "error message here" }
Expand Down
3 changes: 2 additions & 1 deletion influxdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
//! use influxdb::InfluxDbWriteable;
//! use chrono::{DateTime, Utc};
//!
//! #[tokio::main]
//! #[async_std::main]
//! // or #[tokio::main] if you prefer
//! async fn main() {
//! // Connect to db `test` on `http://localhost:8086`
//! let client = Client::new("http://localhost:8086", "test");
Expand Down
2 changes: 1 addition & 1 deletion influxdb/src/query/write_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl Query for WriteQuery {
.join(",");

if !tags.is_empty() {
tags.insert_str(0, ",");
tags.insert(0, ',');
}
let fields = self
.fields
Expand Down
4 changes: 2 additions & 2 deletions influxdb/tests/derive_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_build_query() {
/// INTEGRATION TEST
///
/// This integration tests that writing data and retrieving the data again is working
#[tokio::test]
#[async_std::test]
async fn test_derive_simple_write() {
const TEST_NAME: &str = "test_derive_simple_write";

Expand Down Expand Up @@ -72,7 +72,7 @@ async fn test_derive_simple_write() {
/// This integration tests that writing data and retrieving the data again is working
#[cfg(feature = "derive")]
#[cfg(feature = "use-serde")]
#[tokio::test]
#[async_std::test]
async fn test_write_and_read_option() {
const TEST_NAME: &str = "test_write_and_read_option";

Expand Down
Loading

0 comments on commit cc8750a

Please sign in to comment.