#![allow(clippy::result_large_err)]
use reqwest::StatusCode;
use serde::{Deserialize, Serialize};
use crate::{
action::{ActionError, Context, Pair},
action_impl::ActionImpl,
util::http_get_to_file,
};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HttpGet {
items: Vec<Pair>,
}
impl HttpGet {
pub(crate) fn new(items: Vec<Pair>) -> Self {
Self { items }
}
pub(crate) fn items(&self) -> &[Pair] {
&self.items
}
}
impl ActionImpl for HttpGet {
fn execute(&self, context: &mut Context) -> Result<(), ActionError> {
let deps = context.deps_dir();
for pair in self.items.iter() {
let filename = deps.join(pair.filename());
http_get_to_file(pair.url(), &filename).map_err(HttpGetError::Util)?;
}
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum HttpGetError {
#[error(transparent)]
Util(#[from] crate::util::UtilError),
#[error("failed to format time stamp")]
TimeFormat(#[source] time::error::Format),
#[error("failed to create HTTP client")]
ClientBuild(#[source] reqwest::Error),
#[error("failed to build a reqwest client")]
Client(#[source] reqwest::Error),
#[error("failed to build a reqwest request")]
BuildRequest(#[source] reqwest::Error),
#[error("failed to GET URL {0:?}")]
Get(String, reqwest::Error),
#[error("failed to get body of response from {0:?}")]
GetBody(String, reqwest::Error),
#[error("failure getting file with HTTP GET: status code {0}")]
UnwantedStatus(StatusCode),
}
impl From<HttpGetError> for ActionError {
fn from(value: HttpGetError) -> Self {
Self::HttpGet(value)
}
}