use std::time::Duration;
use reqwest::header::{AUTHORIZATION, HeaderMap, RETRY_AFTER};
use serde::de::DeserializeOwned;
use crate::error::{Error, ErrorKind, kind_for_status};
#[derive(Debug, Clone)]
pub(crate) struct Transport {
http: reqwest::Client,
base_url: String,
api_key: Option<String>,
}
impl Transport {
pub(crate) fn new(http: reqwest::Client, base_url: String, api_key: Option<String>) -> Self {
Self { http, base_url, api_key }
}
pub(crate) async fn get_json<T: DeserializeOwned>(
&self,
path: &str,
query: &[(&str, &str)],
timeout: Duration,
) -> Result<T, Error> {
let response = self.send(path, query, timeout).await?;
let status = response.status().as_u16();
let retry_after = parse_retry_after(response.headers());
let body = response.text().await?;
if !(200..300).contains(&status) {
return Err(Error::from_response(status, retry_after, &body));
}
serde_json::from_str(&body).map_err(|e| Error::Api {
kind: ErrorKind::ServerError,
message: format!("could not decode the response: {e}"),
status,
retry_after: None,
})
}
pub(crate) async fn get_redirect(
&self,
path: &str,
query: &[(&str, &str)],
timeout: Duration,
) -> Result<String, Error> {
let response = self.send(path, query, timeout).await?;
let status = response.status().as_u16();
let retry_after = parse_retry_after(response.headers());
let location = response
.headers()
.get(reqwest::header::LOCATION)
.and_then(|v| v.to_str().ok())
.map(str::to_owned);
match (status, location) {
(300..400, Some(location)) => Ok(location),
(300..400, None) => Err(Error::Api {
kind: ErrorKind::ServerError,
message: "the redirect carried no Location header".to_owned(),
status,
retry_after: None,
}),
(200..300, _) => Err(Error::Config(
"the download redirect was followed, so its Location is gone: build the \
reqwest::Client you passed to ClientBuilder::http_client with \
reqwest::redirect::Policy::none()"
.to_owned(),
)),
_ => {
let body = response.text().await?;
Err(Error::from_response(status, retry_after, &body))
}
}
}
pub(crate) async fn get_file(&self, url: &str) -> Result<reqwest::Response, Error> {
let response = self.http.get(url).send().await?;
let status = response.status().as_u16();
if (200..300).contains(&status) {
return Ok(response);
}
let retry_after = parse_retry_after(response.headers());
Err(Error::Api {
kind: kind_for_status(status, retry_after),
message: format!("object storage refused the download link with status {status}"),
status,
retry_after,
})
}
pub(crate) async fn get_keyless(&self, path: &str, timeout: Duration) -> Result<Answer, Error> {
let url = format!("{}{}", self.base_url, path);
Answer::read(self.http.get(url).timeout(timeout).send().await?).await
}
pub(crate) async fn post_form(
&self,
path: &str,
form: &[(&str, &str)],
timeout: Duration,
) -> Result<Answer, Error> {
let url = format!("{}{}", self.base_url, path);
Answer::read(self.http.post(url).form(form).timeout(timeout).send().await?).await
}
async fn send(
&self,
path: &str,
query: &[(&str, &str)],
timeout: Duration,
) -> Result<reqwest::Response, Error> {
let url = format!("{}{}", self.base_url, path);
let mut request = self.http.get(url).query(query).timeout(timeout);
if let Some(key) = &self.api_key {
request = request.header(AUTHORIZATION, format!("Bearer {key}"));
}
Ok(request.send().await?)
}
}
pub(crate) struct Answer {
pub(crate) status: u16,
pub(crate) retry_after: Option<Duration>,
pub(crate) body: String,
}
impl Answer {
async fn read(response: reqwest::Response) -> Result<Self, Error> {
let status = response.status().as_u16();
let retry_after = parse_retry_after(response.headers());
Ok(Self { status, retry_after, body: response.text().await? })
}
}
fn parse_retry_after(headers: &HeaderMap) -> Option<Duration> {
let value = headers.get(RETRY_AFTER)?.to_str().ok()?.trim().to_owned();
if let Ok(seconds) = value.parse::<u64>() {
return Some(Duration::from_secs(seconds));
}
let when = chrono::DateTime::parse_from_rfc2822(&value).ok()?;
let wait = when.timestamp() - chrono::Utc::now().timestamp();
Some(Duration::from_secs(wait.max(0) as u64))
}