Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Created Apr 16, 2020
Embed
What would you like to do?
use futures::future::join_all;
use futures::StreamExt;
use reqwest::Client;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
struct ProjectsItem {
hash_id: String,
}
type ProjectsData = Vec<ProjectsItem>;
#[derive(Serialize, Deserialize, Debug)]
struct ProjectsResponse {
data: ProjectsData,
}
#[derive(Serialize, Deserialize, Debug)]
struct ProjectAsset {
image_url: String,
}
type ProjectAssets = Vec<ProjectAsset>;
#[derive(Serialize, Deserialize, Debug)]
struct ProjectResponse {
assets: ProjectAssets,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let username = "anniehexample";
let projects_url = format!(
"https://example.com/users/{}/projects.json",
username
);
let client = Client::new();
let project_list = client
.get(&projects_url)
.send()
.await?
.json::<ProjectsResponse>()
.await?;
println!("{:#?}", project_list);
let project_requests = project_list
.data
.into_iter()
.map(|project| {
let url = format!(
"https://example.com/projects/{}.json",
project.hash_id
);
client.get(&url).send()
})
.collect::<Vec<_>>();
let project_responses = join_all(project_requests)
.await
.into_iter()
.filter_map(Result::ok)
.map(|response| response.json::<ProjectResponse>());
let projects = join_all(project_responses).await;
println!("{:#?}", projects);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment