#[cfg(feature = "multi")]
use parking_lot::lock_api::Mutex;
use std::time::Duration;
use crate::constants::MY_USER_AGENT;
use crate::wrapper::request_data::WebRegWrapperData;
use reqwest::Client;
use crate::wrapper::WebRegWrapper;
pub struct WebRegWrapperBuilder {
cookies: Option<String>,
client: Client,
user_agent: String,
default_timeout: Duration,
close_after_request: bool,
}
impl WebRegWrapperBuilder {
pub fn new() -> Self {
Self {
cookies: None,
client: Client::new(),
user_agent: MY_USER_AGENT.to_owned(),
default_timeout: Duration::from_secs(30),
close_after_request: false,
}
}
pub fn with_cookies(mut self, cookie: impl Into<String>) -> Self {
self.cookies = Some(cookie.into());
self
}
pub fn with_client(mut self, client: Client) -> Self {
self.client = client;
self
}
pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
self.user_agent = user_agent.into();
self
}
pub fn with_default_timeout(mut self, timeout: Duration) -> Self {
self.default_timeout = timeout;
self
}
pub fn should_close_after_request(mut self, close: bool) -> Self {
self.close_after_request = close;
self
}
pub fn try_build_wrapper(self) -> Option<WebRegWrapper> {
if let Some(cookies) = self.cookies {
Some(WebRegWrapper {
data: WebRegWrapperData {
#[cfg(feature = "multi")]
cookies: Mutex::new(cookies),
#[cfg(not(feature = "multi"))]
cookies,
client: self.client,
user_agent: self.user_agent,
timeout: self.default_timeout,
close_after_request: self.close_after_request,
},
})
} else {
None
}
}
}
impl Default for WebRegWrapperBuilder {
fn default() -> Self {
Self::new()
}
}