Use Quickinstall new release schema (#867)

- Refactor: Extract new fn `binstalk::helpers::remote::does_url_exist`
 - Use new quickinstall release schema in `binstalk::fetchers::QuickInstall`
 - Optimize `fetchers::QuickInstall`: Generate url once in `Fetcher::new`
    Avoid repeated string allocation plus `Url` parsing.
    This also makes changing package_url and stats_url easier.
 - Optimize `QuickInstall::report`: Use HEAD instead of GET

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-03-06 19:13:08 +11:00 committed by GitHub
parent 01c8ecb778
commit 72789874f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 53 deletions

View file

@ -9,7 +9,7 @@ use crate::{
helpers::{
download::{Download, ExtractedFiles},
gh_api_client::GhApiClient,
remote::Client,
remote::{does_url_exist, Client, Method},
tasks::AutoAbortJoinHandle,
},
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
@ -22,7 +22,12 @@ const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
pub struct QuickInstall {
client: Client,
gh_api_client: GhApiClient,
package: String,
package_url: Url,
stats_url: Url,
target_data: Arc<TargetData>,
}
@ -30,16 +35,28 @@ pub struct QuickInstall {
impl super::Fetcher for QuickInstall {
fn new(
client: Client,
_gh_api_client: GhApiClient,
gh_api_client: GhApiClient,
data: Arc<Data>,
target_data: Arc<TargetData>,
) -> Arc<dyn super::Fetcher> {
let crate_name = &data.name;
let version = &data.version;
let target = &target_data.target;
let package = format!("{crate_name}-{version}-{target}");
Arc::new(Self {
client,
package: format!("{crate_name}-{version}-{target}"),
gh_api_client,
package_url: Url::parse(&format!(
"{BASE_URL}/{crate_name}-{version}/{package}.tar.gz",
))
.expect("package_url is pre-generated and should never be invalid url"),
stats_url: Url::parse(&format!("{STATS_URL}/{package}.tar.gz",))
.expect("stats_url is pre-generated and should never be invalid url"),
package,
target_data,
})
}
@ -60,16 +77,19 @@ impl super::Fetcher for QuickInstall {
});
}
let url = self.package_url();
debug!("Checking for package at: '{url}'");
Ok(self.client.remote_gettable(Url::parse(&url)?).await?)
does_url_exist(
self.client.clone(),
self.gh_api_client.clone(),
&self.package_url,
)
.await
})
}
async fn fetch_and_extract(&self, dst: &Path) -> Result<ExtractedFiles, BinstallError> {
let url = self.package_url();
let url = &self.package_url;
debug!("Downloading package from: '{url}'");
Ok(Download::new(self.client.clone(), Url::parse(&url)?)
Ok(Download::new(self.client.clone(), url.clone())
.and_extract(self.pkg_fmt(), dst)
.await?)
}
@ -103,27 +123,11 @@ impl super::Fetcher for QuickInstall {
}
impl QuickInstall {
fn package_url(&self) -> String {
format!(
"{base_url}/{package}/{package}.tar.gz",
base_url = BASE_URL,
package = self.package
)
}
fn stats_url(&self) -> String {
format!(
"{stats_url}/{package}.tar.gz",
stats_url = STATS_URL,
package = self.package
)
}
pub async fn report(&self) -> Result<(), BinstallError> {
let url = Url::parse(&self.stats_url())?;
let url = self.stats_url.clone();
debug!("Sending installation report to quickinstall ({url})");
self.client.remote_gettable(url).await?;
self.client.request(Method::HEAD, url).send(true).await?;
Ok(())
}