mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-19 02:10:03 +00:00
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:
parent
263c836757
commit
599bcaf333
26 changed files with 960 additions and 192 deletions
|
@ -13,7 +13,10 @@ use url::Url;
|
|||
use crate::{
|
||||
errors::{BinstallError, InvalidPkgFmtError},
|
||||
helpers::{
|
||||
download::Download, futures_resolver::FuturesResolver, remote::Client,
|
||||
download::Download,
|
||||
futures_resolver::FuturesResolver,
|
||||
gh_api_client::{GhApiClient, GhReleaseArtifact, HasReleaseArtifact},
|
||||
remote::Client,
|
||||
tasks::AutoAbortJoinHandle,
|
||||
},
|
||||
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
|
||||
|
@ -26,6 +29,7 @@ use hosting::RepositoryHost;
|
|||
|
||||
pub struct GhCrateMeta {
|
||||
client: Client,
|
||||
gh_api_client: GhApiClient,
|
||||
data: Arc<Data>,
|
||||
target_data: Arc<TargetData>,
|
||||
resolution: OnceCell<(Url, PkgFmt)>,
|
||||
|
@ -62,10 +66,29 @@ impl GhCrateMeta {
|
|||
// go check all potential URLs at once
|
||||
urls.map(move |url| {
|
||||
let client = self.client.clone();
|
||||
let gh_api_client = self.gh_api_client.clone();
|
||||
|
||||
async move {
|
||||
debug!("Checking for package at: '{url}'");
|
||||
|
||||
if let Some(artifact) = GhReleaseArtifact::try_extract_from_url(&url) {
|
||||
debug!("Using GitHub Restful API to check for existence of artifact, which will also cache the API response");
|
||||
|
||||
match gh_api_client.has_release_artifact(artifact).await? {
|
||||
HasReleaseArtifact::Yes => return Ok(Some((url, pkg_fmt))),
|
||||
HasReleaseArtifact::No | HasReleaseArtifact::NoSuchRelease=> return Ok(None),
|
||||
|
||||
HasReleaseArtifact::RateLimit { retry_after } => {
|
||||
warn!("Your GitHub API token (if any) has reached its rate limit and cannot be used again until {retry_after:?}, so we will fallback to HEAD/GET on the url.");
|
||||
warn!("If you did not supply the github token, consider supply one since GitHub by default limit the number of requests for unauthoized user to 60 requests per hour per origin IP address.");
|
||||
}
|
||||
HasReleaseArtifact::Unauthorized => {
|
||||
warn!("GitHub API somehow requires a token for the API access, so we will fallback to HEAD/GET on the url.");
|
||||
warn!("Please consider supplying a token to cargo-binstall to speedup resolution.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(client
|
||||
.remote_gettable(url.clone())
|
||||
.await?
|
||||
|
@ -79,11 +102,13 @@ impl GhCrateMeta {
|
|||
impl super::Fetcher for GhCrateMeta {
|
||||
fn new(
|
||||
client: Client,
|
||||
gh_api_client: GhApiClient,
|
||||
data: Arc<Data>,
|
||||
target_data: Arc<TargetData>,
|
||||
) -> Arc<dyn super::Fetcher> {
|
||||
Arc::new(Self {
|
||||
client,
|
||||
gh_api_client,
|
||||
data,
|
||||
target_data,
|
||||
resolution: OnceCell::new(),
|
||||
|
|
|
@ -6,7 +6,9 @@ use url::Url;
|
|||
|
||||
use crate::{
|
||||
errors::BinstallError,
|
||||
helpers::{download::Download, remote::Client, tasks::AutoAbortJoinHandle},
|
||||
helpers::{
|
||||
download::Download, gh_api_client::GhApiClient, remote::Client, tasks::AutoAbortJoinHandle,
|
||||
},
|
||||
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
|
||||
};
|
||||
|
||||
|
@ -25,6 +27,7 @@ pub struct QuickInstall {
|
|||
impl super::Fetcher for QuickInstall {
|
||||
fn new(
|
||||
client: Client,
|
||||
_gh_api_client: GhApiClient,
|
||||
data: Arc<Data>,
|
||||
target_data: Arc<TargetData>,
|
||||
) -> Arc<dyn super::Fetcher> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue