use std::path::PathBuf;
pub use reqwest::header::HeaderValue;
pub struct HttpOptions {
pub cache: Option<PathBuf>,
pub user_agent: Option<HeaderValue>,
pub max_parallel_downloads: MaxParallelDownloads,
}
impl Default for HttpOptions {
fn default() -> Self {
#[cfg(not(target_arch = "wasm32"))]
let user_agent = Some(HeaderValue::from_static(concat!(
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
)));
#[cfg(target_arch = "wasm32")]
let user_agent = None;
Self {
cache: None,
user_agent,
max_parallel_downloads: MaxParallelDownloads::default(),
}
}
}
pub struct MaxParallelDownloads(pub usize);
impl Default for MaxParallelDownloads {
fn default() -> Self {
Self(6)
}
}
impl MaxParallelDownloads {
pub fn value_manually_confirmed_with_provider_limits(value: usize) -> Self {
Self(value)
}
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use native::*;
#[cfg(target_arch = "wasm32")]
pub(crate) use web::*;
#[cfg(not(target_arch = "wasm32"))]
mod native {
use super::{HttpOptions, bare_client};
use http_cache_reqwest::{CACacheManager, Cache, CacheMode, HttpCache, HttpCacheOptions};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
pub fn http_client(http_options: &HttpOptions) -> ClientWithMiddleware {
let builder = ClientBuilder::new(bare_client(http_options));
if let Some(cache) = &http_options.cache {
builder.with(Cache(HttpCache {
mode: CacheMode::Default,
manager: CACacheManager {
path: cache.clone(),
remove_opts: Default::default(),
},
options: HttpCacheOptions::default(),
}))
} else {
builder
}
.build()
}
}
#[cfg(target_arch = "wasm32")]
mod web {
use super::{HttpOptions, bare_client};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
pub fn http_client(http_options: &HttpOptions) -> ClientWithMiddleware {
if http_options.cache.is_some() {
log::warn!(
"HTTP cache directory set, but ignored because, in WASM, caching is handled by the browser."
);
}
ClientBuilder::new(bare_client(http_options)).build()
}
}
fn bare_client(http_options: &HttpOptions) -> reqwest::Client {
let mut builder = reqwest::Client::builder();
if let Some(user_agent) = &http_options.user_agent {
builder = builder.user_agent(user_agent);
}
builder
.build()
.expect("could not initialize reqwest client")
}