Impl GhApiClient and use it in cargo-binstall to speedup resolution process (#832)

Fixed #776

 - Add new feature gh-api-client to binstalk-downloader
 - Impl new type `binstalk_downloader::remote::{RequestBuilder, Response}`
 - Impl `binstalk_downloader::gh_api_client::GhApiClient`, exposed if `cfg(feature = "gh-api-client")` and add e2e and unit tests for it
 - Use `binstalk_downloader::gh_api_client::GhApiClient` to speedup `cargo-binstall`
 - Add new option `--github-token` to supply the token for GitHub restful API, or read from env variable `GITHUB_TOKEN` if not present.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-03-02 12:04:22 +11:00 committed by GitHub
parent 263c836757
commit 599bcaf333
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 960 additions and 192 deletions

View file

@ -13,6 +13,7 @@ use binstalk::{
ops::resolve::{CrateName, VersionReqExt},
};
use clap::{error::ErrorKind, CommandFactory, Parser, ValueEnum};
use compact_str::CompactString;
use log::LevelFilter;
use semver::VersionReq;
use strum::EnumCount;
@ -218,6 +219,10 @@ pub struct Args {
#[clap(help_heading = "Options", long)]
pub json_output: bool,
/// Provide the github token for accessing the restful API of api.github.com
#[clap(help_heading = "Options", long, env = "GITHUB_TOKEN")]
pub github_token: Option<CompactString>,
/// Print version information
#[clap(help_heading = "Meta", short = 'V')]
pub version: bool,

View file

@ -1,5 +1,5 @@
use std::{
fs,
env, fs,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
@ -10,6 +10,7 @@ use binstalk::{
fetchers::{Fetcher, GhCrateMeta, QuickInstall},
get_desired_targets,
helpers::{
gh_api_client::GhApiClient,
jobserver_client::LazyJobserverClient,
remote::{Certificate, Client},
tasks::AutoAbortJoinHandle,
@ -87,6 +88,8 @@ pub async fn install_crates(args: Args, jobserver_client: LazyJobserverClient) -
)
.map_err(BinstallError::from)?;
let gh_api_client = GhApiClient::new(client.clone(), args.github_token);
// Build crates.io api client
let crates_io_api_client =
CratesIoApiClient::with_http_client(client.get_inner().clone(), Duration::from_millis(100));
@ -111,6 +114,7 @@ pub async fn install_crates(args: Args, jobserver_client: LazyJobserverClient) -
install_path,
client,
crates_io_api_client,
gh_api_client,
jobserver_client,
});
@ -342,7 +346,7 @@ fn do_install_fetches(
if no_cleanup {
// Consume temp_dir without removing it from fs.
temp_dir.into_path();
let _ = temp_dir.into_path();
} else {
temp_dir.close().unwrap_or_else(|err| {
warn!("Failed to clean up some resources: {err}");