diff --git a/src/github.rs b/src/github.rs index 7f28337..b1d5dfd 100644 --- a/src/github.rs +++ b/src/github.rs @@ -36,42 +36,38 @@ pub fn fetch_repositories( .repositories } -fn fetch_all_repositories( +pub fn fetch_all_repositories( + cache: &mut Cache, token: &str, ) -> Result>> { - let client = Client::builder() - .user_agent("graphql-rust/0.10.0") - .default_headers( - std::iter::once(( - reqwest::header::AUTHORIZATION, - reqwest::header::HeaderValue::from_str(&format!("Bearer {token}")).unwrap(), - )) - .collect(), - ) - .build()?; - - let mut cursor = None; - let mut repositories: Vec> = vec![]; - - loop { - let data = fetch_repositories(&client, cursor); - - repositories.extend(data.nodes.expect("repositories nodes is null")); - - if !data.page_info.has_next_page { - break; + cache.get_or("all-repositories", || { + let client = Client::builder() + .user_agent("catppuccin-purr") + .default_headers( + std::iter::once(( + reqwest::header::AUTHORIZATION, + reqwest::header::HeaderValue::from_str(&format!("Bearer {token}"))?, + )) + .collect(), + ) + .build()?; + + let mut cursor = None; + let mut repositories: Vec> = vec![]; + + loop { + let data = fetch_repositories(&client, cursor); + + repositories.extend(data.nodes.expect("repositories nodes is null")); + + if !data.page_info.has_next_page { + break; + } + cursor = data.page_info.end_cursor; } - cursor = data.page_info.end_cursor; - } - - Ok(repositories) -} -pub fn cached_fetch_all_repositories( - cache: &mut Cache, - token: &str, -) -> Result>> { - cache.get_or("all-repositories", || fetch_all_repositories(token)) + Ok(repositories) + }) } pub fn rest(path: &str, token: Option) -> Result { diff --git a/src/query.rs b/src/query.rs index 9825cdd..66917e4 100644 --- a/src/query.rs +++ b/src/query.rs @@ -6,9 +6,7 @@ use serde_json::Value; use crate::{ cache::Cache, cli::{Key, Query, WhiskersCustomProperty}, - github::{ - self, cached_fetch_all_repositories, fetch_whiskers_custom_property, RepositoryResponse, - }, + github::{self, fetch_all_repositories, fetch_whiskers_custom_property, RepositoryResponse}, models::{self, ports::Port, shared::StringOrStrings}, }; @@ -163,7 +161,7 @@ pub fn query( println!("{}", res.stargazers_count); } else { - let repositories = cached_fetch_all_repositories(cache, &token)?; + let repositories = fetch_all_repositories(cache, &token)?; let stars: i64 = repositories .iter() @@ -214,7 +212,7 @@ pub fn query( let mut found_false = 0; let mut found_na = 0; - let repositories = cached_fetch_all_repositories(cache, &token)?; + let repositories = fetch_all_repositories(cache, &token)?; let result = repositories .iter() .flatten()