How to fetch multiple with tokio
💡 full source code is on github
foo.json
{
"id": "foo",
"type": "Cat",
"weight": 123.45,
"createdAt": "2022-09-01"
}
bar.json
{
"id": "bar",
"type": "Duck",
"weight": 42.2424,
"createdAt": "2022-08-01"
}
Cargo.toml
[package]
name = "fetch-multiple-tokio"
version = "0.1.0"
edition = "2021"
[dependencies]
# Use No-std support https://serde.rs/no-std.html
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
# Use foe fetch, feature `json` for load json, `rustls-tls` for load via `TLS`.
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls"] }
# Use for handy return Result.
anyhow = "1.0.65"
# Use for async.
tokio = { version ="1.22", features = ["full"] }
futures = "0.3.25"
main.rs
use reqwest::Client; use serde::{Deserialize, Serialize}; use tokio::task; #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(rename_all = "camelCase")] struct AnimalData { id: String, weight: f32, created_at: String, } async fn fetch(url: &str) -> anyhow::Result<AnimalData> { Ok(Client::new() .get(url) .send() .await? .json::<AnimalData>() .await?) } async fn fetch_multiple(urls: [&'static str; 2]) -> anyhow::Result<Vec<AnimalData>> { // How to use task::spawn let tasks = urls.iter().map(|url| task::spawn(fetch(url))); // Collect task result. let mut results = vec![]; for task in tasks { results.push(task.await?); } // Return flattened results, silent if error. Ok(results // We use into_iter so we get Vec<AnimalData> instead of Vec<&AnimalData> .into_iter() .flatten() .collect::<Vec<_>>()) } #[tokio::main] async fn main() { let urls:[&'static str;2] = [ "https://raw.githubusercontent.com/gist-rs/book/main/examples/r4/31-fetch-multiple-tokio/src/foo.json", "https://raw.githubusercontent.com/gist-rs/book/main/examples/r4/31-fetch-multiple-tokio/src/bar.json" ]; let json = fetch_multiple(urls).await; println!("{json:#?}"); }
🤷
reqwest
+TLS
is not runnable via Rust Playground so output is shown below.
Run
Ok(
[
AnimalData {
id: "foo",
weight: 123.45,
created_at: "2022-09-01",
},
AnimalData {
id: "bar",
weight: 42.2424,
created_at: "2022-08-01",
},
],
)