From 59f8634207991d8ce4708da82017b985ee1c0778 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 30 May 2021 19:14:50 +0200 Subject: github::fetch_repos(): Fetch all repos from all pages Also switch from `reqwest::blocking` to async because I was getting this error, probably because I call `fetch_repos()` in the async 'tokio' function `main()`: thread 'main' panicked at 'Cannot drop a runtime in a context where blocking is not allowed. This happens when a runtime is dropped from within an asynchronous context.', $HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.6.1/src/runtime/blocking/shutdown.rs:51:21 --- Cargo.toml | 2 +- src/github.rs | 35 ++++++++++++++++++++++++----------- src/main.rs | 6 +++--- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 175f63f..854cc11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" [dependencies] anyhow = "1.0.40" git2 = "0.13.20" -reqwest = { version = "0.11.3", features = ["blocking", "json"] } +reqwest = { version = "0.11.3", features = ["json"] } serde = { version = "1.0.126", features = ["derive"] } thiserror = "1.0.25" tokio = { version = "1.6.1", features = ["macros", "rt"] } diff --git a/src/github.rs b/src/github.rs index b937f9b..f5598ba 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1,4 +1,4 @@ -use reqwest::blocking::ClientBuilder; +use reqwest::ClientBuilder; use serde::Deserialize; use thiserror; @@ -41,7 +41,7 @@ impl Repo { } -pub fn fetch_repos() -> Result, Error> { +pub async fn fetch_repos() -> Result, Error> { let mut headers = reqwest::header::HeaderMap::new(); headers.insert("Accept", "application/vnd.github.v3+json".parse()?); @@ -50,15 +50,28 @@ pub fn fetch_repos() -> Result, Error> { .default_headers(headers) .build()?; - let repos = client.request( - reqwest::Method::GET, - format!( - "https://api.github.com/users/{}/repos", - "teddywing", - ), - ) - .send()? - .json::>()?; + let mut repos = Vec::new(); + + for i in 1.. { + let repo_page = client.request( + reqwest::Method::GET, + format!( + "https://api.github.com/users/{}/repos?page={}&per_page=100", + "teddywing", + i, + ), + ) + .send() + .await? + .json::>() + .await?; + + if repo_page.is_empty() { + break; + } + + repos.extend(repo_page); + } Ok(repos) } diff --git a/src/main.rs b/src/main.rs index 13b2b07..77083cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,9 @@ use std::path::{Path, PathBuf}; #[tokio::main] async fn main() { - // let repos = github::fetch_repos().unwrap(); - // - // dbg!(&repos); + let repos = github::fetch_repos().await.unwrap(); + + dbg!(&repos); // git::mirror( // "https://github.com/teddywing/google-calendar-rsvp.git", -- cgit v1.2.3