Refactor: Rm global var helpers::CLIENT

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-10 18:07:07 +10:00
parent 3c06c45792
commit fa63dbe5cf
No known key found for this signature in database
GPG key ID: 591C0B03040416D6
6 changed files with 42 additions and 36 deletions

View file

@ -2,6 +2,7 @@ use std::path::Path;
use std::sync::Arc;
use log::{debug, info, warn};
use reqwest::Client;
use reqwest::Method;
use serde::Serialize;
use url::Url;
@ -10,6 +11,7 @@ use super::Data;
use crate::{download_and_extract, remote_exists, BinstallError, PkgFmt, Template};
pub struct GhCrateMeta {
client: Client,
data: Data,
}
@ -23,8 +25,11 @@ impl GhCrateMeta {
#[async_trait::async_trait]
impl super::Fetcher for GhCrateMeta {
async fn new(data: &Data) -> Arc<Self> {
Arc::new(Self { data: data.clone() })
async fn new(client: &Client, data: &Data) -> Arc<Self> {
Arc::new(Self {
client: client.clone(),
data: data.clone(),
})
}
async fn check(&self) -> Result<bool, BinstallError> {
@ -37,13 +42,13 @@ impl super::Fetcher for GhCrateMeta {
}
info!("Checking for package at: '{url}'");
remote_exists(url, Method::HEAD).await
remote_exists(&self.client, url, Method::HEAD).await
}
async fn fetch_and_extract(&self, dst: &Path) -> Result<(), BinstallError> {
let url = self.url()?;
info!("Downloading package from: '{url}'");
download_and_extract(url, self.pkg_fmt(), dst).await
download_and_extract(&self.client, url, self.pkg_fmt(), dst).await
}
fn pkg_fmt(&self) -> PkgFmt {