Skip to content

Commit

Permalink
Update the readme to use async await
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoLegends committed Dec 15, 2019
1 parent b5ad78b commit e1589ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ For an example with using Serde deserialization, please refer to [serde_integrat

```rust
use influxdb::{Client, Query, Timestamp};
use serde::Deserialize;
use tokio::runtime::current_thread::Runtime;

// Create a Client with URL `http://localhost:8086`
// and database name `test`
Expand All @@ -67,21 +65,15 @@ let client = Client::new("http://localhost:8086", "test");
let write_query = Query::write_query(Timestamp::Now, "weather")
.add_field("temperature", 82);

// Since this library is async by default, we're going to need a Runtime,
// which can asynchonously run our query.
// The [tokio](https://crates.io/crates/tokio) crate lets us easily create a new Runtime.
let mut rt = Runtime::new().expect("Unable to create a runtime");

// To actually submit the data to InfluxDB, the `block_on` method can be used to
// halt execution of our program until it has been completed.
let write_result = rt.block_on(client.query(&write_query));
// Submit the query to InfluxDB.
let write_result = client.query(&write_query).await;
assert!(write_result.is_ok(), "Write result was not okay");

// Reading data is as simple as writing. First we need to create a query
let read_query = Query::raw_read_query("SELECT * FROM weather");

// Again, we're blocking until the request is done
let read_result = rt.block_on(client.query(&read_query));
// submit the request and wait until it's done
let read_result = client.query(&read_query).await;

assert!(read_result.is_ok(), "Read result was not ok");

Expand Down
19 changes: 7 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
//!
//! ```rust,no_run
//! use influxdb::{Client, Query, Timestamp};
//! use serde::Deserialize;
//! use tokio::runtime::current_thread::Runtime;
//!
//! # #[tokio::main]
//! # async fn main() {
//! // Create a Client with URL `http://localhost:8086`
//! // and database name `test`
//! let client = Client::new("http://localhost:8086", "test");
Expand All @@ -41,27 +41,22 @@
//! let write_query = Query::write_query(Timestamp::Now, "weather")
//! .add_field("temperature", 82);
//!
//! // Since this library is async by default, we're going to need a Runtime,
//! // which can asynchonously run our query.
//! // The [tokio](https://crates.io/crates/tokio) crate lets us easily create a new Runtime.
//! let mut rt = Runtime::new().expect("Unable to create a runtime");
//!
//! // To actually submit the data to InfluxDB, the `block_on` method can be used to
//! // halt execution of our program until it has been completed.
//! let write_result = rt.block_on(client.query(&write_query));
//! // Submit the query to InfluxDB.
//! let write_result = client.query(&write_query).await;
//! assert!(write_result.is_ok(), "Write result was not okay");
//!
//! // Reading data is as simple as writing. First we need to create a query
//! let read_query = Query::raw_read_query("SELECT * FROM weather");
//!
//! // Again, we're blocking until the request is done
//! let read_result = rt.block_on(client.query(&read_query));
//! // submit the request and wait until it's done
//! let read_result = client.query(&read_query).await;
//!
//! assert!(read_result.is_ok(), "Read result was not ok");
//!
//! // We can be sure the result was successful, so we can unwrap the result to get
//! // the response String from InfluxDB
//! println!("{}", read_result.unwrap());
//! # }
//! ```
//!
//! For further examples, check out the Integration Tests in `tests/integration_tests.rs`
Expand Down

0 comments on commit e1589ca

Please sign in to comment.