use clap::ArgMatches;
use ffsend_api::action::params::{Error as ParamsError, Params as ApiParams, ParamsDataBuilder};
use ffsend_api::file::remote_file::RemoteFile;
use super::select_api_version;
use crate::client::create_config;
use crate::cmd::matcher::{main::MainMatcher, params::ParamsMatcher, Matcher};
use crate::error::ActionError;
#[cfg(feature = "history")]
use crate::history_tool;
use crate::util::{ensure_owner_token, print_success};
pub struct Params<'a> {
cmd_matches: &'a ArgMatches<'a>,
}
impl<'a> Params<'a> {
pub fn new(cmd_matches: &'a ArgMatches<'a>) -> Self {
Self { cmd_matches }
}
pub fn invoke(&self) -> Result<(), ActionError> {
let matcher_main = MainMatcher::with(self.cmd_matches).unwrap();
let matcher_params = ParamsMatcher::with(self.cmd_matches).unwrap();
let url = matcher_params.url();
let mut host = url.clone();
host.set_path("");
host.set_query(None);
host.set_fragment(None);
let client_config = create_config(&matcher_main);
let client = client_config.client(false);
let mut desired_version = matcher_main.api();
select_api_version(&client, host, &mut desired_version)?;
let api_version = desired_version.version().unwrap();
let mut file = RemoteFile::parse_url(url, matcher_params.owner())?;
#[cfg(feature = "history")]
history_tool::derive_file_properties(&matcher_main, &mut file);
ensure_owner_token(file.owner_token_mut(), &matcher_main, false);
let auth = false;
let data = ParamsDataBuilder::default()
.download_limit(
matcher_params
.download_limit(&matcher_main, api_version, auth)
.map(|d| d as u8),
)
.build()
.unwrap();
let result = ApiParams::new(&file, data, None).invoke(&client);
if let Err(ParamsError::Expired) = result {
#[cfg(feature = "history")]
history_tool::remove(&matcher_main, &file);
}
result?;
#[cfg(feature = "history")]
history_tool::add(&matcher_main, file, true);
print_success("Parameters set");
Ok(())
}
}