use dashmap::DashMap;
use reqwest::Client;
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug, warn};
#[derive(Clone)]
pub struct DataImpulseClient {
inner: Arc<DataImpulseInner>,
}
struct DataImpulseInner {
username: String,
password: String,
host: String,
port: u16,
user_agent: String,
timeout: Duration,
clients: DashMap<String, Client>,
}
impl DataImpulseClient {
pub fn new(
username: impl Into<String>,
password: impl Into<String>,
host: impl Into<String>,
port: u16,
user_agent: impl Into<String>,
timeout: Duration,
) -> Self {
Self {
inner: Arc::new(DataImpulseInner {
username: username.into(),
password: password.into(),
host: host.into(),
port,
user_agent: user_agent.into(),
timeout,
clients: DashMap::new(),
}),
}
}
pub fn client(&self, country_code: Option<&str>) -> Client {
let cc = country_code.map(|c| c.to_lowercase()).unwrap_or_default();
if let Some(c) = self.inner.clients.get(&cc) {
return c.clone();
}
let username = if cc.is_empty() {
self.inner.username.clone()
} else {
format!("{}__cr.{}", self.inner.username, cc)
};
let proxy_url = format!(
"http://{}:{}@{}:{}",
username, self.inner.password, self.inner.host, self.inner.port
);
debug!(
"DataImpulse: building client for country={:?} proxy={}",
cc, self.inner.host
);
let client = match reqwest::Client::builder()
.user_agent(&self.inner.user_agent)
.timeout(self.inner.timeout)
.connect_timeout(Duration::from_secs(8))
.proxy(reqwest::Proxy::all(&proxy_url).expect("valid DataImpulse proxy URL"))
.redirect(reqwest::redirect::Policy::limited(5))
.gzip(true)
.brotli(true)
.build()
{
Ok(c) => c,
Err(e) => {
warn!("DataImpulse: failed to build client for cc={cc}: {e}");
return reqwest::Client::new();
}
};
self.inner.clients.insert(cc, client.clone());
client
}
pub fn fresh_client(&self, country_code: Option<&str>) -> Client {
let cc = country_code.map(|c| c.to_lowercase()).unwrap_or_default();
let username = if cc.is_empty() {
self.inner.username.clone()
} else {
format!("{}__cr.{}", self.inner.username, cc)
};
let proxy_url = format!(
"http://{}:{}@{}:{}",
username, self.inner.password, self.inner.host, self.inner.port
);
match reqwest::Client::builder()
.user_agent(&self.inner.user_agent)
.timeout(self.inner.timeout)
.connect_timeout(Duration::from_secs(8))
.proxy(reqwest::Proxy::all(&proxy_url).expect("valid DataImpulse proxy URL"))
.redirect(reqwest::redirect::Policy::limited(5))
.gzip(true)
.brotli(true)
.pool_max_idle_per_host(0)
.build()
{
Ok(c) => c,
Err(_) => reqwest::Client::new(),
}
}
pub fn is_configured(&self) -> bool {
!self.inner.username.is_empty() && !self.inner.password.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn client_cached_per_country() {
let di = DataImpulseClient::new(
"testuser",
"testpass",
"gw.example.com",
823,
"TestAgent/1.0",
Duration::from_secs(10),
);
let c1 = di.client(Some("us"));
let c2 = di.client(Some("us"));
let c3 = di.client(Some("gb"));
drop(c1);
drop(c2);
drop(c3);
}
#[test]
fn configured_check() {
let di = DataImpulseClient::new("u", "p", "h", 823, "UA", Duration::from_secs(5));
assert!(di.is_configured());
let empty = DataImpulseClient::new("", "", "h", 823, "UA", Duration::from_secs(5));
assert!(!empty.is_configured());
}
}