pub mod types;
#[cfg(target_vendor = "apple")]
pub mod foundation;
#[cfg(windows)]
pub mod windows;
pub mod reqwest;
use crate::{
Result,
cookies::{Cookie, CookieAcceptPolicy},
};
use std::time::Duration;
use types::{BackendRequest, BackendResponse};
use url::Url;
#[derive(Clone, Debug, Default)]
pub struct BackendConfig {
pub timeout: Option<Duration>,
pub user_agent: Option<String>,
pub ignore_certificate_errors: Option<bool>,
pub default_headers: Option<http::HeaderMap>,
pub use_cookies: Option<bool>,
pub cookie_jar: Option<crate::CookieJar>,
pub http_proxy: Option<crate::client::ProxyConfig>,
pub https_proxy: Option<crate::client::ProxyConfig>,
pub socks_proxy: Option<crate::client::ProxyConfig>,
}
#[derive(Clone)]
pub enum Backend {
#[cfg(target_vendor = "apple")]
Foundation(foundation::FoundationBackend),
#[cfg(windows)]
Windows(windows::WindowsBackend),
Reqwest(reqwest::ReqwestBackend),
}
impl Backend {
pub fn default_for_platform() -> Result<Self> {
#[cfg(target_vendor = "apple")]
{
Ok(Backend::Foundation(foundation::FoundationBackend::new()?))
}
#[cfg(all(windows, not(target_vendor = "apple")))]
{
Ok(Backend::Windows(windows::WindowsBackend::new()?))
}
#[cfg(not(any(target_vendor = "apple", windows)))]
{
Ok(Backend::Reqwest(reqwest::ReqwestBackend::new()?))
}
}
pub fn reqwest() -> Result<Self> {
Ok(Backend::Reqwest(reqwest::ReqwestBackend::new()?))
}
#[cfg(target_vendor = "apple")]
pub fn foundation() -> Result<Self> {
Ok(Backend::Foundation(foundation::FoundationBackend::new()?))
}
#[cfg(target_vendor = "apple")]
pub fn foundation_with_config(config: BackendConfig) -> Result<Self> {
Ok(Backend::Foundation(
foundation::FoundationBackend::with_config(config)?,
))
}
#[cfg(windows)]
pub fn windows() -> Result<Self> {
Ok(Backend::Windows(windows::WindowsBackend::new()?))
}
#[cfg(windows)]
pub fn windows_with_config(config: BackendConfig) -> Result<Self> {
Ok(Backend::Windows(windows::WindowsBackend::with_config(
config,
)?))
}
pub fn reqwest_with_config(config: BackendConfig) -> Result<Self> {
Ok(Backend::Reqwest(reqwest::ReqwestBackend::with_config(
config,
)?))
}
pub async fn execute(&self, request: BackendRequest) -> Result<BackendResponse> {
match self {
#[cfg(target_vendor = "apple")]
Backend::Foundation(f) => f.execute(request).await,
#[cfg(windows)]
Backend::Windows(w) => w.execute(request).await,
Backend::Reqwest(r) => r.execute(request).await,
}
}
pub async fn execute_background_download(
&self,
url: Url,
file_path: std::path::PathBuf,
session_identifier: Option<String>,
progress_callback: Option<Box<dyn Fn(u64, Option<u64>) + Send + Sync + 'static>>,
) -> Result<crate::client::download::DownloadResponse> {
match self {
#[cfg(target_vendor = "apple")]
Backend::Foundation(f) => {
f.execute_background_download(url, file_path, session_identifier, progress_callback)
.await
}
#[cfg(windows)]
Backend::Windows(w) => {
w.execute_background_download(url, file_path, session_identifier, progress_callback)
.await
}
Backend::Reqwest(r) => {
r.execute_background_download(url, file_path, session_identifier, progress_callback)
.await
}
}
}
pub fn cookie_jar(&self) -> Option<&crate::CookieJar> {
match self {
#[cfg(target_vendor = "apple")]
Backend::Foundation(f) => f.cookie_jar(),
#[cfg(windows)]
Backend::Windows(w) => w.cookie_jar(),
Backend::Reqwest(r) => r.cookie_jar(),
}
}
}
#[derive(Clone, Debug)]
pub enum CookieStorage {
#[cfg(target_vendor = "apple")]
Foundation(foundation::FoundationCookieStorage),
Reqwest(reqwest::ReqwestCookieStorage),
}
impl CookieStorage {
pub fn new() -> Self {
#[cfg(target_vendor = "apple")]
{
CookieStorage::Foundation(foundation::FoundationCookieStorage::new())
}
#[cfg(not(target_vendor = "apple"))]
{
CookieStorage::Reqwest(reqwest::ReqwestCookieStorage::new())
}
}
#[cfg(target_vendor = "apple")]
pub fn for_group_container(identifier: &str) -> Result<Self> {
Ok(CookieStorage::Foundation(
foundation::FoundationCookieStorage::for_group_container(identifier),
))
}
pub fn all_cookies(&self) -> Vec<Cookie> {
match self {
#[cfg(target_vendor = "apple")]
CookieStorage::Foundation(storage) => storage.all_cookies(),
CookieStorage::Reqwest(storage) => storage.all_cookies(),
}
}
pub fn cookies_for_url(&self, url: &str) -> Result<Vec<Cookie>> {
match self {
#[cfg(target_vendor = "apple")]
CookieStorage::Foundation(storage) => storage.cookies_for_url(url),
CookieStorage::Reqwest(storage) => storage.cookies_for_url(url),
}
}
pub fn add_cookie(&self, cookie: Cookie) -> Result<()> {
match self {
#[cfg(target_vendor = "apple")]
CookieStorage::Foundation(storage) => storage.add_cookie(cookie),
CookieStorage::Reqwest(storage) => storage.add_cookie(cookie),
}
}
pub fn remove_cookie(&self, cookie: Cookie) -> Result<()> {
match self {
#[cfg(target_vendor = "apple")]
CookieStorage::Foundation(storage) => storage.remove_cookie(cookie),
CookieStorage::Reqwest(storage) => storage.remove_cookie(cookie),
}
}
pub fn clear(&self) {
match self {
#[cfg(target_vendor = "apple")]
CookieStorage::Foundation(storage) => storage.clear(),
CookieStorage::Reqwest(storage) => storage.clear(),
}
}
pub fn set_cookie_accept_policy(&self, policy: CookieAcceptPolicy) {
match self {
#[cfg(target_vendor = "apple")]
CookieStorage::Foundation(storage) => storage.set_cookie_accept_policy(policy),
CookieStorage::Reqwest(storage) => storage.set_cookie_accept_policy(policy),
}
}
}