Skip to content

Commit 9f8b15d

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

File tree

3 files changed

+28
-44
lines changed

3 files changed

+28
-44
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/integrations/serde_integration.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,27 @@
2222
//! weather: WeatherWithoutCityName,
2323
//! }
2424
//!
25-
//! let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
25+
//! # #[tokio::main]
26+
//! # async fn main() -> Box<dyn std::error::Error> {
2627
//! let client = Client::new("http://localhost:8086", "test");
2728
//! let query = Query::raw_read_query(
2829
//! "SELECT temperature FROM /weather_[a-z]*$/ WHERE time > now() - 1m ORDER BY DESC",
2930
//! );
30-
//! let _result = rt
31-
//! .block_on(client.json_query(query))
32-
//! .map(|mut db_result| db_result.deserialize_next::<WeatherWithoutCityName>())
33-
//! .map(|it| {
34-
//! it.map(|series_vec| {
35-
//! series_vec
36-
//! .series
37-
//! .into_iter()
38-
//! .map(|mut city_series| {
39-
//! let city_name =
40-
//! city_series.name.split("_").collect::<Vec<&str>>().remove(2);
41-
//! Weather {
42-
//! weather: city_series.values.remove(0),
43-
//! city_name: city_name.to_string(),
44-
//! }
45-
//! })
46-
//! .collect::<Vec<Weather>>()
47-
//! })
48-
//! });
31+
//! let db_result = client.json_query(query).await?;
32+
//! let _result = db_result
33+
//! .deserialize_next::<WeatherWithoutCityName>()?
34+
//! .series
35+
//! .into_iter()
36+
//! .map(|mut city_series| {
37+
//! let city_name =
38+
//! city_series.name.split("_").collect::<Vec<&str>>().remove(2);
39+
//! Weather {
40+
//! weather: city_series.values.remove(0),
41+
//! city_name: city_name.to_string(),
42+
//! }
43+
//! })
44+
//! .collect::<Vec<Weather>>();
45+
//! # }
4946
//! ```
5047
5148
use reqwest::{Client as ReqwestClient, StatusCode, Url};

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)