Skip to content

Replace Surf with Reqwest to allow for upgrading Tokio to 1.x. #93

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

Closed
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
5 changes: 1 addition & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ jobs:
integration_test:
name: Integration Tests (stable/ubuntu-latest)
runs-on: ubuntu-latest
strategy:
matrix:
http-backend: [curl-client, h1-client, h1-client-rustls, hyper-client]
services:
influxdb:
image: influxdb:1.8
Expand All @@ -70,7 +67,7 @@ jobs:
steps:
- uses: actions/checkout@v1
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --manifest-path=./influxdb/Cargo.toml --no-default-features --features 'use-serde derive ${{ matrix.http-backend }}' --no-fail-fast
- run: cargo test --manifest-path=./influxdb/Cargo.toml --all --all-features --no-fail-fast

coverage:
name: Code Coverage (stable/ubuntu-20.04)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async fn main() {
assert!(write_result.is_ok(), "Write result was not okay");

// Let's see if the data we wrote is there
let read_query = Query::raw_read_query("SELECT * FROM weather");
let read_query = <dyn Query>::raw_read_query("SELECT * FROM weather");

let read_result = client.query(&read_query).await;
assert!(read_result.is_ok(), "Read result was not ok");
Expand Down
2 changes: 1 addition & 1 deletion benches/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async fn main() {
async fn prepare_influxdb(client: &Client, db_name: &str) {
let create_db_stmt = format!("CREATE DATABASE {}", db_name);
client
.query(&Query::raw_read_query(create_db_stmt))
.query(&<dyn Query>::raw_read_query(create_db_stmt))
.await
.expect("failed to create database");
}
Expand Down
11 changes: 3 additions & 8 deletions influxdb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,16 @@ futures = "0.3.4"
lazy_static = "1.4.0"
influxdb_derive = { version = "0.4.0", optional = true }
regex = "1.3.5"
surf = { version = "2.2.0", default-features = false }
reqwest = { version = "0.11" }
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"]
curl-client = ["surf/curl-client"]
h1-client = ["surf/h1-client"]
h1-client-rustls = ["surf/h1-client-rustls"]
hyper-client = ["surf/hyper-client"]
wasm-client = ["surf/wasm-client"]
default = ["use-serde", "hyper-client"]
default = ["use-serde"]
derive = ["influxdb_derive"]

[dev-dependencies]
async-std = { version = "1.6.5", features = ["attributes"] }
tokio = { version = "0.2.22", features = ["rt-threaded", "macros"] }
tokio = { version = "1.8.1", features = ["macros", "test-util"] }
27 changes: 12 additions & 15 deletions influxdb/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! ```

use futures::prelude::*;
use surf::{self, Client as SurfClient, StatusCode};
use reqwest::{self, Client as ReqwestClient, StatusCode};

use crate::query::QueryType;
use crate::Error;
Expand All @@ -29,7 +29,7 @@ use std::sync::Arc;
pub struct Client {
pub(crate) url: Arc<String>,
pub(crate) parameters: Arc<HashMap<&'static str, String>>,
pub(crate) client: SurfClient,
pub(crate) client: ReqwestClient,
}

impl Client {
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Client {
Client {
url: Arc::new(url.into()),
parameters: Arc::new(parameters),
client: SurfClient::new(),
client: ReqwestClient::new(),
}
}

Expand Down Expand Up @@ -112,8 +112,8 @@ impl Client {
error: format!("{}", err),
})?;

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

Ok((build.to_owned(), version.to_owned()))
}
Expand Down Expand Up @@ -184,28 +184,25 @@ impl Client {

self.client.post(url).body(query.get()).query(&parameters)
}
}
.map_err(|err| Error::UrlConstructionError {
error: err.to_string(),
})?;
};

let request = request_builder.build();
let mut res = self
let request = request_builder.build().unwrap();
let res = self
.client
.send(request)
.execute(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
.body_string()
.text()
.await
.map_err(|_| Error::DeserializationError {
error: "response could not be converted to UTF-8".to_string(),
Expand Down
19 changes: 8 additions & 11 deletions influxdb/src/integrations/serde_integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! # #[async_std::main]
//! # async fn main() -> Result<(), influxdb::Error> {
//! let client = Client::new("http://localhost:8086", "test");
//! let query = Query::raw_read_query(
//! let query = <dyn Query>::raw_read_query(
//! "SELECT temperature FROM /weather_[a-z]*$/ WHERE time > now() - 1m ORDER BY DESC",
//! );
//! let mut db_result = client.json_query(query).await?;
Expand All @@ -48,7 +48,7 @@

mod de;

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

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

Expand Down Expand Up @@ -147,26 +147,23 @@ impl Client {
.client
.get(url)
.query(&parameters)
.map_err(|err| Error::UrlConstructionError {
error: err.to_string(),
})?
.build();
.build().unwrap();

let mut res = self
let res = self
.client
.send(request)
.execute(request)
.await
.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.body_bytes().await.map_err(|err| Error::ProtocolError {
let body = res.bytes().await.map_err(|err| Error::ProtocolError {
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 @@ -56,7 +56,7 @@
//! assert!(write_result.is_ok(), "Write result was not okay");
//!
//! // Let's see if the data we wrote is there
//! let read_query = Query::raw_read_query("SELECT * FROM weather");
//! let read_query = <dyn Query>::raw_read_query("SELECT * FROM weather");
//!
//! let read_result = client.query(&read_query).await;
//! assert!(read_result.is_ok(), "Read result was not ok");
Expand Down
4 changes: 2 additions & 2 deletions influxdb/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//!
//! assert!(write_query.is_ok());
//!
//! let read_query = Query::raw_read_query("SELECT * FROM weather")
//! let read_query = <dyn Query>::raw_read_query("SELECT * FROM weather")
//! .build();
//!
//! assert!(read_query.is_ok());
Expand Down Expand Up @@ -133,7 +133,7 @@ impl dyn Query {
/// ```rust
/// use influxdb::Query;
///
/// Query::raw_read_query("SELECT * FROM weather"); // Is of type [`ReadQuery`](crate::ReadQuery)
/// <dyn Query>::raw_read_query("SELECT * FROM weather"); // Is of type [`ReadQuery`](crate::ReadQuery)
/// ```
pub fn raw_read_query<S>(read_query: S) -> ReadQuery
where
Expand Down
10 changes: 5 additions & 5 deletions influxdb/src/query/read_query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Read Query Builder returned by Query::raw_read_query
//! Read Query Builder returned by <dyn Query>::raw_read_query
//!
//! Can only be instantiated by using Query::raw_read_query
//! Can only be instantiated by using <dyn Query>::raw_read_query

use crate::query::{QueryType, ValidQuery};
use crate::{Error, Query};
Expand Down Expand Up @@ -47,14 +47,14 @@ mod tests {

#[test]
fn test_read_builder_single_query() {
let query = Query::raw_read_query("SELECT * FROM aachen").build();
let query = <dyn Query>::raw_read_query("SELECT * FROM aachen").build();

assert_eq!(query.unwrap(), "SELECT * FROM aachen");
}

#[test]
fn test_read_builder_multi_query() {
let query = Query::raw_read_query("SELECT * FROM aachen")
let query = <dyn Query>::raw_read_query("SELECT * FROM aachen")
.add_query("SELECT * FROM cologne")
.build();

Expand All @@ -63,7 +63,7 @@ mod tests {

#[test]
fn test_correct_query_type() {
let query = Query::raw_read_query("SELECT * FROM aachen");
let query = <dyn Query>::raw_read_query("SELECT * FROM aachen");

assert_eq!(query.get_type(), QueryType::ReadQuery);
}
Expand Down
6 changes: 3 additions & 3 deletions influxdb/tests/derive_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_build_query() {
/// INTEGRATION TEST
///
/// This integration tests that writing data and retrieving the data again is working
#[async_std::test]
#[tokio::test]
#[cfg(not(tarpaulin_include))]
async fn test_derive_simple_write() {
const TEST_NAME: &str = "test_derive_simple_write";
Expand Down Expand Up @@ -82,7 +82,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")]
#[async_std::test]
#[tokio::test]
#[cfg(not(tarpaulin_include))]
async fn test_write_and_read_option() {
const TEST_NAME: &str = "test_write_and_read_option";
Expand All @@ -102,7 +102,7 @@ async fn test_write_and_read_option() {
.await;
assert_result_ok(&write_result);
let query =
Query::raw_read_query("SELECT time, pressure, wind_strength FROM weather_reading");
<dyn Query>::raw_read_query("SELECT time, pressure, wind_strength FROM weather_reading");
let result = client.json_query(query).await.and_then(|mut db_result| {
println!("{:?}", db_result);
db_result.deserialize_next::<WeatherReadingWithoutIgnored>()
Expand Down
Loading