use std::time::Duration;
use serde::de::DeserializeOwned;
pub const TIMEOUT: Duration = Duration::from_secs(120);
pub fn builder(user_agent: &str) -> reqwest::ClientBuilder {
crate::install_crypto_provider();
reqwest::Client::builder()
.user_agent(user_agent)
.timeout(TIMEOUT)
}
pub fn blocking_builder(user_agent: &str) -> reqwest::blocking::ClientBuilder {
crate::install_crypto_provider();
reqwest::blocking::Client::builder()
.user_agent(user_agent)
.timeout(TIMEOUT)
}
pub async fn ok(resp: reqwest::Response, what: &str) -> Result<reqwest::Response, String> {
let status = resp.status();
if status.is_success() {
return Ok(resp);
}
let text = resp.text().await.unwrap_or_default();
Err(format!("{what}: HTTP {status}: {text}"))
}
pub async fn json_ok<T: DeserializeOwned>(
resp: reqwest::Response,
what: &str,
) -> Result<T, String> {
ok(resp, what)
.await?
.json()
.await
.map_err(|e| format!("{what}: {e}"))
}
pub fn blocking_ok(
resp: reqwest::blocking::Response,
what: &str,
) -> Result<reqwest::blocking::Response, String> {
let status = resp.status();
if status.is_success() {
return Ok(resp);
}
let text = resp.text().unwrap_or_default();
Err(format!("{what}: HTTP {status}: {text}"))
}
pub fn blocking_json_ok<T: DeserializeOwned>(
resp: reqwest::blocking::Response,
what: &str,
) -> Result<T, String> {
blocking_ok(resp, what)?
.json()
.map_err(|e| format!("{what}: {e}"))
}