Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Created April 16, 2020 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PurpleBooth/fd65f63396ea9dc484066b51e313c6ad to your computer and use it in GitHub Desktop.
Save PurpleBooth/fd65f63396ea9dc484066b51e313c6ad to your computer and use it in GitHub Desktop.
use std::collections::HashMap;
use futures::{stream, StreamExt};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::error::Error;
#[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<ProjectsItem>;
#[derive(Serialize, Deserialize, Debug)]
struct ProjectResponse {
assets: ProjectAssets
}
const PARALLEL_REQUESTS: usize = 2;
#[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 projects = client.get(&projects_url)
.send()
.await?
.json::<ProjectsResponse>()
.await?;
let mut stream = futures::stream::FuturesUnordered::new();
let bodies = stream::iter(projects.data)
.map(|project| {
let client = &client;
async move {
client.get(&format!("https://example.com/projects/{}.json", project.hash_id))
.send()
.await?
.json::<ProjectsResponse>()
.await
}
})
.buffer_unordered(PARALLEL_REQUESTS)
.await;
println!("{:#?}", bodies);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment