diff --git a/oma-fetch/examples/downloads.rs b/oma-fetch/examples/downloads.rs index 6dcf17b7..b69785ad 100644 --- a/oma-fetch/examples/downloads.rs +++ b/oma-fetch/examples/downloads.rs @@ -10,6 +10,7 @@ use oma_fetch::{ DownloadEntryBuilder, DownloadEvent, DownloadResult, DownloadSource, DownloadSourceType, OmaFetcher, }; +use reqwest::ClientBuilder; use tokio::io::AsyncWriteExt; #[tokio::main] @@ -50,7 +51,9 @@ async fn main() -> DownloadResult<()> { // .dir(PathBuf::from("./oma-fetcher-test")) // .build()?; - let fetcher = OmaFetcher::new(None, vec![file_1, file_2], None)?; + let client = ClientBuilder::new().user_agent("oma").build().unwrap(); + + let fetcher = OmaFetcher::new(&client, vec![file_1, file_2], None)?; tokio::fs::create_dir_all("./oma-fetcher-test") .await diff --git a/oma-fetch/src/lib.rs b/oma-fetch/src/lib.rs index ce8f65a3..b8bf9fb7 100644 --- a/oma-fetch/src/lib.rs +++ b/oma-fetch/src/lib.rs @@ -8,7 +8,7 @@ use derive_builder::Builder; use download::SingleDownloaderBuilder; use futures::StreamExt; -use reqwest::{Client, ClientBuilder}; +use reqwest::Client; pub mod checksum; mod download; @@ -90,8 +90,8 @@ impl Ord for DownloadSourceType { } } -pub struct OmaFetcher { - client: Client, +pub struct OmaFetcher<'a> { + client: &'a Client, download_list: Vec, limit_thread: usize, retry_times: usize, @@ -145,21 +145,14 @@ impl Summary { } /// OmaFetcher is a Download Manager -impl OmaFetcher { +impl<'a> OmaFetcher<'a> { pub fn new( - client: Option, + client: &'a Client, download_list: Vec, limit_thread: Option, - ) -> DownloadResult { - let client = client.unwrap_or( - ClientBuilder::new() - .user_agent("oma") - .build() - .map_err(DownloadError::ReqwestError)?, - ); - + ) -> DownloadResult> { Ok(Self { - client, + client: &client, download_list, limit_thread: limit_thread.unwrap_or(4), retry_times: 3, diff --git a/oma-pm/src/apt.rs b/oma-pm/src/apt.rs index 388bc509..85e451f1 100644 --- a/oma-pm/src/apt.rs +++ b/oma-pm/src/apt.rs @@ -25,8 +25,8 @@ use oma_apt::{ }; use oma_console::console::{self, style}; use oma_fetch::{ - DownloadEntryBuilder, DownloadEntryBuilderError, DownloadError, DownloadEvent, DownloadSource, - DownloadSourceType, OmaFetcher, Summary, + reqwest::ClientBuilder, DownloadEntryBuilder, DownloadEntryBuilderError, DownloadError, + DownloadEvent, DownloadSource, DownloadSourceType, OmaFetcher, Summary, }; use oma_utils::{ dpkg::{is_hold, DpkgError}, @@ -718,7 +718,9 @@ impl OmaApt { download_list.push(download_entry); } - let downloader = OmaFetcher::new(None, download_list, network_thread)?; + let client = ClientBuilder::new().user_agent("oma").build().unwrap(); + + let downloader = OmaFetcher::new(&client, download_list, network_thread)?; let res = downloader .start_download(|count, event| callback(count, event, Some(total_size))) diff --git a/oma-refresh/src/db.rs b/oma-refresh/src/db.rs index 21e9c6c3..1e9cb16f 100644 --- a/oma-refresh/src/db.rs +++ b/oma-refresh/src/db.rs @@ -8,7 +8,7 @@ use futures::{future::join, FutureExt, StreamExt}; use oma_apt_sources_lists::{SourceEntry, SourceError}; use oma_fetch::{ checksum::ChecksumError, - reqwest::{self, ClientBuilder}, + reqwest::{self, Client, ClientBuilder}, DownloadEntry, DownloadEntryBuilder, DownloadEntryBuilderError, DownloadEvent, DownloadResult, DownloadSource, DownloadSourceType, OmaFetcher, Summary, }; @@ -241,12 +241,12 @@ impl OmaRefresh { let client = ClientBuilder::new().user_agent("oma").build()?; let is_inrelease_map = self - .is_inrelease_map(&sourcelist, client, &m, &callback) + .get_is_inrelease_map(&sourcelist, &client, &m, &callback) .await?; let tasks = self.collect_download_release_tasks(&sourcelist, &m, is_inrelease_map)?; - let release_results = OmaFetcher::new(None, tasks, Some(self.limit))? + let release_results = OmaFetcher::new(&client, tasks, Some(self.limit))? .start_download(|c, event| callback(c, RefreshEvent::from(event), None)) .await; @@ -258,7 +258,7 @@ impl OmaRefresh { .collect_all_release_entry(all_inrelease, &sourcelist, &m) .await?; - let res = OmaFetcher::new(None, tasks, Some(self.limit))? + let res = OmaFetcher::new(&client, tasks, Some(self.limit))? .start_download(|count, event| callback(count, RefreshEvent::from(event), Some(total))) .await; @@ -267,10 +267,10 @@ impl OmaRefresh { Ok(()) } - async fn is_inrelease_map( + async fn get_is_inrelease_map( &self, sourcelist: &[OmaSourceEntry], - client: reqwest::Client, + client: &Client, m: &Option>, callback: &F, ) -> Result>