use std::error::Error;
use reqwest::Client;
use super::{CATBOX_API_URL, UASTRING};
pub async fn create<S: Into<String>>(
title: S,
desc: S,
user_hash: Option<S>,
files: Vec<S>,
) -> Result<String, Box<dyn Error>> {
let files: Vec<_> = files.into_iter().map(|file| file.into()).collect();
let form = [
("reqtype", "createalbum"),
(
"userhash",
&user_hash
.and_then(|hash| Some(hash.into()))
.unwrap_or_default(),
),
("title", &title.into()),
("desc", &desc.into()),
("files", &files.join(" ")),
];
Ok(Client::builder()
.user_agent(UASTRING)
.build()
.unwrap_or_else(|_| Client::new())
.post(CATBOX_API_URL)
.form(&form)
.send()
.await?
.text()
.await?)
}
pub async fn edit<S: Into<String>>(
short: S,
title: S,
desc: S,
user_hash: S,
files: Vec<S>,
) -> Result<String, Box<dyn Error>> {
let files: Vec<_> = files.into_iter().map(|file| file.into()).collect();
let form = [
("reqtype", "editalbum"),
("userhash", &user_hash.into()),
("short", &short.into()),
("title", &title.into()),
("desc", &desc.into()),
("files", &files.join(" ")),
];
Ok(Client::builder()
.user_agent(UASTRING)
.build()
.unwrap_or_else(|_| Client::new())
.post(CATBOX_API_URL)
.form(&form)
.send()
.await?
.text()
.await?)
}
pub async fn add_files<S: Into<String>>(
short: S,
user_hash: S,
files: Vec<S>,
) -> Result<String, Box<dyn Error>> {
let files: Vec<_> = files.into_iter().map(|file| file.into()).collect();
let form = [
("reqtype", "addtoalbum"),
("short", &short.into()),
("userhash", &user_hash.into()),
("files", &files.join(" ")),
];
Ok(Client::builder()
.user_agent(UASTRING)
.build()
.unwrap_or_else(|_| Client::new())
.post(CATBOX_API_URL)
.form(&form)
.send()
.await?
.text()
.await?)
}
pub async fn remove_files<S: Into<String>>(
short: S,
user_hash: S,
files: Vec<S>,
) -> Result<String, Box<dyn Error>> {
let files: Vec<_> = files.into_iter().map(|file| file.into()).collect();
let form = [
("reqtype", "removefromalbum"),
("userhash", &user_hash.into()),
("short", &short.into()),
("files", &files.join(" ")),
];
Ok(Client::builder()
.user_agent(UASTRING)
.build()
.unwrap_or_else(|_| Client::new())
.post(CATBOX_API_URL)
.form(&form)
.send()
.await?
.text()
.await?)
}
pub async fn delete<S: Into<String>>(short: S, user_hash: S) -> Result<String, Box<dyn Error>> {
let form = [
("reqtype", "deletealbum"),
("userhash", &user_hash.into()),
("short", &short.into()),
];
Ok(Client::builder()
.user_agent(UASTRING)
.build()
.unwrap_or_else(|_| Client::new())
.post(CATBOX_API_URL)
.form(&form)
.send()
.await?
.text()
.await?)
}