use std::time::Duration;
fn agent(connect: Duration, read: Duration) -> ureq::Agent {
ureq::AgentBuilder::new()
.timeout_connect(connect)
.timeout_read(read)
.build()
}
fn describe(error: ureq::Error) -> String {
match error {
ureq::Error::Status(code, response) => {
format!("{code}, {}", response.into_string().unwrap_or_default())
}
ureq::Error::Transport(transport) => transport.to_string(),
}
}
pub fn put_json(url: &str, body: &serde_json::Value, timeout: Duration) -> Result<(), String> {
agent(timeout, timeout)
.put(url)
.send_json(body)
.map(|_| ())
.map_err(describe)
}
pub fn get_text(url: &str, timeout: Duration) -> Result<String, String> {
agent(timeout, timeout)
.get(url)
.call()
.map_err(describe)?
.into_string()
.map_err(|error| error.to_string())
}
pub fn healthy(url: &str, connect: Duration, read: Duration) -> bool {
agent(connect, read)
.get(url)
.call()
.is_ok_and(|response| response.status() == 200)
}