Skip to content

Commit e1589ca

Browse files
committed
Update the readme to use async await
1 parent b5ad78b commit e1589ca

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ For an example with using Serde deserialization, please refer to [serde_integrat
5454

5555
```rust
5656
use influxdb::{Client, Query, Timestamp};
57-
use serde::Deserialize;
58-
use tokio::runtime::current_thread::Runtime;
5957

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

70-
// Since this library is async by default, we're going to need a Runtime,
71-
// which can asynchonously run our query.
72-
// The [tokio](https://crates.io/crates/tokio) crate lets us easily create a new Runtime.
73-
let mut rt = Runtime::new().expect("Unable to create a runtime");
74-
75-
// To actually submit the data to InfluxDB, the `block_on` method can be used to
76-
// halt execution of our program until it has been completed.
77-
let write_result = rt.block_on(client.query(&write_query));
68+
// Submit the query to InfluxDB.
69+
let write_result = client.query(&write_query).await;
7870
assert!(write_result.is_ok(), "Write result was not okay");
7971

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

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

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

src/lib.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
//!
2929
//! ```rust,no_run
3030
//! use influxdb::{Client, Query, Timestamp};
31-
//! use serde::Deserialize;
32-
//! use tokio::runtime::current_thread::Runtime;
3331
//!
32+
//! # #[tokio::main]
33+
//! # async fn main() {
3434
//! // Create a Client with URL `http://localhost:8086`
3535
//! // and database name `test`
3636
//! let client = Client::new("http://localhost:8086", "test");
@@ -41,27 +41,22 @@
4141
//! let write_query = Query::write_query(Timestamp::Now, "weather")
4242
//! .add_field("temperature", 82);
4343
//!
44-
//! // Since this library is async by default, we're going to need a Runtime,
45-
//! // which can asynchonously run our query.
46-
//! // The [tokio](https://crates.io/crates/tokio) crate lets us easily create a new Runtime.
47-
//! let mut rt = Runtime::new().expect("Unable to create a runtime");
48-
//!
49-
//! // To actually submit the data to InfluxDB, the `block_on` method can be used to
50-
//! // halt execution of our program until it has been completed.
51-
//! let write_result = rt.block_on(client.query(&write_query));
44+
//! // Submit the query to InfluxDB.
45+
//! let write_result = client.query(&write_query).await;
5246
//! assert!(write_result.is_ok(), "Write result was not okay");
5347
//!
5448
//! // Reading data is as simple as writing. First we need to create a query
5549
//! let read_query = Query::raw_read_query("SELECT * FROM weather");
5650
//!
57-
//! // Again, we're blocking until the request is done
58-
//! let read_result = rt.block_on(client.query(&read_query));
51+
//! // submit the request and wait until it's done
52+
//! let read_result = client.query(&read_query).await;
5953
//!
6054
//! assert!(read_result.is_ok(), "Read result was not ok");
6155
//!
6256
//! // We can be sure the result was successful, so we can unwrap the result to get
6357
//! // the response String from InfluxDB
6458
//! println!("{}", read_result.unwrap());
59+
//! # }
6560
//! ```
6661
//!
6762
//! For further examples, check out the Integration Tests in `tests/integration_tests.rs`

0 commit comments

Comments
 (0)