use std::time::Duration;
use reqwest::Client;
use crate::error::Result;
const DEFAULT_TIMEOUT_SECS: u64 = 30;
const CONNECT_TIMEOUT_SECS: u64 = 10;
pub fn build_http_client(timeout_secs: Option<u64>) -> Result<Client> {
let timeout = Duration::from_secs(timeout_secs.unwrap_or(DEFAULT_TIMEOUT_SECS));
let client = Client::builder()
.timeout(timeout)
.connect_timeout(Duration::from_secs(CONNECT_TIMEOUT_SECS))
.tcp_keepalive(Duration::from_secs(60))
.pool_idle_timeout(Duration::from_secs(90))
.user_agent(format!(
"mkt/{} (+https://mktcli.com)",
env!("CARGO_PKG_VERSION")
))
.gzip(true)
.brotli(true)
.build()?;
Ok(client)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn build_client_with_defaults() {
let client = build_http_client(None);
assert!(client.is_ok());
}
#[test]
fn build_client_with_custom_timeout() {
let client = build_http_client(Some(60));
assert!(client.is_ok());
}
}