Skip to content

Commit

Permalink
refactor(github): move fetch_all_repositories inner logic directly …
Browse files Browse the repository at this point in the history
…into `cached_fetch_all_repositories`
  • Loading branch information
uncenter committed Jan 15, 2025
1 parent d99c2fe commit cd4bbab
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
60 changes: 28 additions & 32 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,38 @@ pub fn fetch_repositories(
.repositories
}

fn fetch_all_repositories(
pub fn fetch_all_repositories(
cache: &mut Cache,
token: &str,
) -> Result<Vec<Option<RepositoriesOrganizationRepositoriesNodes>>> {
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<Option<RepositoriesOrganizationRepositoriesNodes>> = 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<Option<RepositoriesOrganizationRepositoriesNodes>> = 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<Vec<Option<RepositoriesOrganizationRepositoriesNodes>>> {
cache.get_or("all-repositories", || fetch_all_repositories(token))
Ok(repositories)
})
}

pub fn rest(path: &str, token: Option<String>) -> Result<reqwest::blocking::Response> {
Expand Down
8 changes: 3 additions & 5 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit cd4bbab

Please sign in to comment.