From e1589ca789a74b0b0d25d20e6793dc83a60c8f01 Mon Sep 17 00:00:00 2001 From: Moritz Gunz Date: Sun, 15 Dec 2019 21:22:32 +0100 Subject: [PATCH] Update the readme to use async await --- README.md | 16 ++++------------ src/lib.rs | 19 +++++++------------ 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 91d8309..fa89fcb 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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"); diff --git a/src/lib.rs b/src/lib.rs index c9bbf7f..5ab7c52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"); @@ -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`