use trauma::download::Download;
use crate::CivitResult;
pub use self::defines::ModelInfo;
mod defines;
#[derive(Clone, Debug)]
pub struct RequestModel {
pub id: usize,
}
impl RequestModel {
pub fn new(id: usize) -> Self {
Self { id }
}
pub async fn send(&self) -> CivitResult<ModelInfo> {
Ok(reqwest::get(self.url()).await?.json().await?)
}
pub fn url(&self) -> String {
format!("https://civitai.com/api/v1/models/{}", self.id)
}
}
impl ModelInfo {
pub fn download_link(&self) -> &str {
match self.model_versions.first() {
Some(s) => s.download_url.as_str(),
None => "",
}
}
pub fn download(&self, local: &str) -> CivitResult<Download> {
let mut file_name = local;
let download_link = match self.model_versions.first() {
Some(s) => {
if file_name.is_empty() {
file_name = s.name.as_str()
}
s.download_url.as_str()
}
None => Err(trauma::Error::InvalidUrl("Missing download link".to_string()))?,
};
let mut task = Download::try_from(download_link)?;
task.filename = file_name.to_string();
Ok(task)
}
}