use std::{path::PathBuf, sync::Arc, time::Duration};
use crate::{
TResult,
transport::{HttpTransportBuilder, RequestPlan, Response, Transport},
};
pub struct NcmClient {
transport: Arc<dyn Transport>,
}
#[derive(Clone, Debug)]
pub struct NcmClientBuilder {
cache_enabled: bool,
cache_ttl: Duration,
persist_cookies: bool,
cookie_path: PathBuf,
}
impl Default for NcmClientBuilder {
fn default() -> Self {
Self {
cache_enabled: true,
cache_ttl: Duration::from_secs(3 * 60),
persist_cookies: true,
cookie_path: std::env::temp_dir().join("ncmapi-cookies"),
}
}
}
impl NcmClientBuilder {
pub fn cache(mut self, enabled: bool) -> Self {
self.cache_enabled = enabled;
self
}
pub fn cache_ttl(mut self, ttl: Duration) -> Self {
self.cache_ttl = ttl;
self
}
pub fn persist_cookies(mut self, enabled: bool) -> Self {
self.persist_cookies = enabled;
self
}
pub fn cookie_path(mut self, path: impl Into<PathBuf>) -> Self {
self.cookie_path = path.into();
self
}
pub fn build(self) -> TResult<NcmClient> {
let transport = HttpTransportBuilder::new(&self.cookie_path)?
.cache(self.cache_enabled)
.cache_ttl(self.cache_ttl)
.persist_cookies(self.persist_cookies)
.build()?;
Ok(NcmClient {
transport: Arc::new(transport),
})
}
}
impl NcmClient {
pub fn builder() -> NcmClientBuilder {
NcmClientBuilder::default()
}
pub(crate) async fn execute(&self, request: RequestPlan) -> TResult<Response> {
self.transport.execute(request).await
}
#[cfg(test)]
pub(crate) fn with_transport(transport: Arc<dyn Transport>) -> Self {
Self { transport }
}
}