use std::io::Read;
use std::time::Duration;
use serde_json::Value;
const TIMEOUT: Duration = Duration::from_secs(30);
const RETRIES: u32 = 3;
const LONGEST_WAIT: u64 = 60;
const ANSWER_LIMIT: u64 = 64 * 1024;
pub(crate) enum Outcome {
Accepted {
url: Option<String>,
warnings: Vec<String>,
},
NotTaken { status: u16, problem: Problem },
Unreachable { reason: String },
Refused { status: u16, problem: Problem },
}
#[derive(Default)]
pub(crate) struct Problem {
pub(crate) title: Option<String>,
pub(crate) detail: Option<String>,
pub(crate) errors: Vec<(String, String)>,
}
impl Problem {
fn read(body: &str) -> Self {
let value: Value = serde_json::from_str(body).unwrap_or(Value::Null);
let text = |value: &Value| value.as_str().map(str::to_owned);
Self {
title: text(&value["title"]),
detail: text(&value["detail"]),
errors: value["errors"]
.as_array()
.into_iter()
.flatten()
.take(20)
.map(|error| {
(
text(&error["pointer"]).unwrap_or_default(),
text(&error["message"]).unwrap_or_default(),
)
})
.collect(),
}
}
pub(crate) fn summary(&self) -> String {
[&self.title, &self.detail]
.into_iter()
.flatten()
.map(|text| text.trim_end_matches('.').to_owned() + ".")
.collect::<Vec<_>>()
.join(" ")
}
}
pub(crate) fn send(url: &str, token: &str, body: &[u8], wait: &mut dyn FnMut(Duration)) -> Outcome {
let agent = ureq::Agent::new_with_config(
ureq::Agent::config_builder()
.timeout_global(Some(TIMEOUT))
.http_status_as_error(false)
.max_redirects(0)
.proxy(if url.starts_with("http://") {
None
} else {
ureq::Proxy::try_from_env()
})
.user_agent(format!("mortise/{}", env!("CARGO_PKG_VERSION")))
.tls_config(
ureq::tls::TlsConfig::builder()
.root_certs(ureq::tls::RootCerts::PlatformVerifier)
.build(),
)
.build(),
);
let mut attempt = 0;
loop {
let (reason, asked) = match agent
.post(url)
.header("Authorization", format!("Bearer {token}"))
.header("Content-Type", "application/json")
.header("Content-Encoding", "gzip")
.header("Accept", "application/json")
.send(body)
{
Ok(mut response) => {
let status = response.status().as_u16();
let asked = response
.headers()
.get("retry-after")
.and_then(|value| value.to_str().ok())
.and_then(|value| value.trim().parse::<u64>().ok());
let mut answer = String::new();
let _ = response
.body_mut()
.with_config()
.reader()
.take(ANSWER_LIMIT)
.read_to_string(&mut answer);
match status {
200 | 202 => {
let value: Value = serde_json::from_str(&answer).unwrap_or(Value::Null);
return Outcome::Accepted {
url: value["url"].as_str().map(str::to_owned),
warnings: value["warnings"]
.as_array()
.into_iter()
.flatten()
.filter_map(|warning| warning.as_str().map(str::to_owned))
.collect(),
};
}
402 | 403 => {
return Outcome::NotTaken {
status,
problem: Problem::read(&answer),
};
}
429 | 500..=599 => (format!("HTTP {status}"), asked),
_ => {
return Outcome::Refused {
status,
problem: Problem::read(&answer),
};
}
}
}
Err(error) => (error.to_string(), None),
};
if attempt == RETRIES {
return Outcome::Unreachable { reason };
}
wait(delay(attempt, asked));
attempt += 1;
}
}
fn delay(attempt: u32, asked: Option<u64>) -> Duration {
Duration::from_secs(asked.map_or(2 << attempt, |asked| asked.min(LONGEST_WAIT)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn no_answer_is_tried_four_times_then_given_up() {
let port = std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let mut waits = Vec::new();
let outcome = send(
&format!("http://127.0.0.1:{port}/api/v1/snapshots"),
"mortise_test",
b"{}",
&mut |wait| waits.push(wait.as_secs()),
);
assert!(matches!(outcome, Outcome::Unreachable { .. }));
assert_eq!(waits, [2, 4, 8]);
assert_eq!(delay(0, Some(600)), Duration::from_secs(60));
}
}