use std::time::Duration;
const DEFAULT_REST_TIMEOUT_SECS: u64 = 30;
const DEFAULT_REST_CONNECT_TIMEOUT_SECS: u64 = 10;
pub(crate) fn rest_client() -> reqwest::Client {
reqwest::Client::builder()
.timeout(env_secs("VTA_REST_TIMEOUT_SECS", DEFAULT_REST_TIMEOUT_SECS))
.connect_timeout(env_secs(
"VTA_REST_CONNECT_TIMEOUT_SECS",
DEFAULT_REST_CONNECT_TIMEOUT_SECS,
))
.build()
.expect("reqwest client with timeouts (TLS backend init)")
}
fn env_secs(var: &str, default: u64) -> Duration {
let secs = std::env::var(var)
.ok()
.and_then(|v| v.trim().parse::<u64>().ok())
.filter(|&n| n > 0)
.unwrap_or(default);
Duration::from_secs(secs)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn env_secs_uses_default_when_unset_or_junk() {
assert_eq!(
env_secs("VTA_REST_TIMEOUT_SECS_DEFINITELY_UNSET_XYZ", 30),
Duration::from_secs(30)
);
}
#[test]
fn rest_client_builds() {
let _ = rest_client();
}
}