use crate::api::data::{Error as DataError, OwnedData};
use crate::api::nonce::{request_nonce, NonceError};
use crate::api::request::{ensure_success, ResponseError};
use crate::api::url::UrlBuilder;
use crate::client::Client;
use crate::file::remote_file::RemoteFile;
pub const PARAMS_DEFAULT_DOWNLOAD: u8 = 1;
pub const PARAMS_DEFAULT_DOWNLOAD_STR: &str = "1";
pub const PARAMS_DOWNLOAD_MIN: u8 = 1;
pub const PARAMS_DOWNLOAD_MAX: u8 = 20;
pub struct Params<'a> {
file: &'a RemoteFile,
params: ParamsData,
nonce: Vec<u8>,
}
impl<'a> Params<'a> {
pub fn new(file: &'a RemoteFile, params: ParamsData, nonce: Option<Vec<u8>>) -> Self {
Self {
file,
params,
nonce: nonce.unwrap_or_default(),
}
}
pub fn invoke(mut self, client: &Client) -> Result<(), Error> {
if self.nonce.is_empty() {
self.nonce = self.fetch_auth_nonce(client)?;
}
let data = OwnedData::from(self.params.clone(), &self.file)
.map_err(|err| -> PrepareError { err.into() })?;
self.change_params(client, &data)
}
fn fetch_auth_nonce(&self, client: &Client) -> Result<Vec<u8>, Error> {
request_nonce(client, UrlBuilder::download(self.file, false)).map_err(|err| err.into())
}
fn change_params(&self, client: &Client, data: &OwnedData<ParamsData>) -> Result<(), Error> {
let url = UrlBuilder::api_params(self.file);
let response = client
.post(url)
.json(&data)
.send()
.map_err(|_| ChangeError::Request)?;
ensure_success(&response).map_err(|err| err.into())
}
}
#[derive(Clone, Debug, Builder, Serialize)]
pub struct ParamsData {
#[serde(rename = "dlimit")]
download_limit: Option<u8>,
}
impl ParamsData {
pub fn new() -> Self {
ParamsData {
download_limit: None,
}
}
pub fn from(download_limit: Option<u8>) -> Self {
ParamsData { download_limit }
}
pub fn set_download_limit(
&mut self,
download_limit: Option<u8>,
) -> Result<(), ParamsDataError> {
if let Some(d) = download_limit {
if d < PARAMS_DOWNLOAD_MIN || d > PARAMS_DOWNLOAD_MAX {
return Err(ParamsDataError::DownloadBounds);
}
}
self.download_limit = download_limit;
Ok(())
}
pub fn is_empty(&self) -> bool {
self.download_limit.is_none()
}
}
impl Default for ParamsData {
fn default() -> ParamsData {
ParamsData {
download_limit: None,
}
}
}
#[derive(Fail, Debug)]
pub enum Error {
#[fail(display = "failed to prepare setting the parameters")]
Prepare(#[cause] PrepareError),
#[fail(display = "the file has expired or did never exist")]
Expired,
#[fail(display = "failed to send the parameter change request")]
Change(#[cause] ChangeError),
}
impl From<NonceError> for Error {
fn from(err: NonceError) -> Error {
match err {
NonceError::Expired => Error::Expired,
err => Error::Prepare(PrepareError::Auth(err)),
}
}
}
impl From<PrepareError> for Error {
fn from(err: PrepareError) -> Error {
Error::Prepare(err)
}
}
impl From<ChangeError> for Error {
fn from(err: ChangeError) -> Error {
Error::Change(err)
}
}
impl From<ResponseError> for Error {
fn from(err: ResponseError) -> Error {
match err {
ResponseError::Expired => Error::Expired,
err => Error::Change(ChangeError::Response(err)),
}
}
}
#[derive(Debug, Fail)]
pub enum ParamsDataError {
#[fail(display = "invalid number of downloads, must be between 1 and 20")]
DownloadBounds,
#[fail(display = "")]
Owned(#[cause] DataError),
}
#[derive(Fail, Debug)]
pub enum PrepareError {
#[fail(display = "failed to authenticate")]
Auth(#[cause] NonceError),
#[fail(display = "invalid parameters")]
ParamsData(#[cause] ParamsDataError),
}
impl From<DataError> for PrepareError {
fn from(err: DataError) -> PrepareError {
PrepareError::ParamsData(ParamsDataError::Owned(err))
}
}
#[derive(Fail, Debug)]
pub enum ChangeError {
#[fail(display = "failed to send parameter change request")]
Request,
#[fail(display = "bad response from server while changing parameters")]
Response(#[cause] ResponseError),
}