fix: make request sending asynchronous too

This commit is contained in:
uku 2025-05-12 14:17:51 +02:00
parent 2f96459f58
commit fb227660bb
Signed by: uku
SSH key fingerprint: SHA256:4P0aN6M8ajKukNi6aPOaX0LacanGYtlfjmN+m/sHY/o
4 changed files with 100 additions and 70 deletions

View file

@ -1,11 +1,7 @@
use std::{path::Path, sync::LazyLock};
use color_eyre::eyre::{Result, bail};
use reqwest::{
StatusCode,
blocking::{Client, multipart::Form},
header::AUTHORIZATION,
};
use reqwest::{Client, StatusCode, header::AUTHORIZATION, multipart::Form};
use crate::Config;
@ -46,29 +42,36 @@ impl ZiplineFileInfo {
}
}
pub fn get_folders(config: &Config) -> Result<Vec<ZiplineFolder>> {
pub async fn get_folders(config: &Config) -> Result<Vec<ZiplineFolder>> {
let url = format!("{}api/user/folders?noincl=true", config.fixed_url());
let res = CLIENT
.get(url)
.header(AUTHORIZATION, &config.zipline_token)
.send()?;
.send()
.await?;
if res.status() != StatusCode::OK {
bail!("an error occurred ({}): {}", res.status(), res.text()?);
bail!(
"an error occurred ({}): {}",
res.status(),
res.text().await?
);
} else {
res.json().map_err(Into::into)
res.json().await.map_err(Into::into)
}
}
pub fn upload_file(
pub async fn upload_file(
config: &Config,
folder: Option<&ZiplineFolder>,
file_path: &Path,
) -> Result<ZiplineUploadResponse> {
let url = format!("{}api/upload", config.fixed_url());
let form = Form::new().file("file", file_path)?;
// TODO use Part::stream to provide a wrapped file with a custom stream impl to send progress
// (i hope it works)
let form = Form::new().file("file", file_path).await?;
let mut req = CLIENT
.post(url)
@ -80,42 +83,56 @@ pub fn upload_file(
req = req.header("x-zipline-folder", &folder.id);
}
let res = req.send()?;
let res = req.send().await?;
if res.status() != StatusCode::OK {
bail!("an error occurred ({}): {}", res.status(), res.text()?);
bail!(
"an error occurred ({}): {}",
res.status(),
res.text().await?
);
} else {
res.json().map_err(Into::into)
res.json().await.map_err(Into::into)
}
}
pub fn recalc_thumbnails(config: &Config) -> Result<()> {
pub async fn recalc_thumbnails(config: &Config) -> Result<()> {
let url = format!("{}api/server/thumbnails", config.fixed_url());
let res = CLIENT
.post(url)
.header(AUTHORIZATION, &config.zipline_token)
.json(&[("rerun", false)])
.send()?;
.send()
.await?;
if res.status() != StatusCode::OK {
bail!("an error occurred ({}): {}", res.status(), res.text()?);
bail!(
"an error occurred ({}): {}",
res.status(),
res.text().await?
);
} else {
Ok(())
}
}
pub fn get_file_details(config: &Config, id: &str) -> Result<ZiplineFileInfo> {
pub async fn get_file_details(config: &Config, id: &str) -> Result<ZiplineFileInfo> {
let url = format!("{}api/user/files/{id}", config.fixed_url());
let res = CLIENT
.get(url)
.header(AUTHORIZATION, &config.zipline_token)
.send()?;
.send()
.await?;
if res.status() != StatusCode::OK {
bail!("an error occurred ({}): {}", res.status(), res.text()?);
bail!(
"an error occurred ({}): {}",
res.status(),
res.text().await?
);
} else {
res.json().map_err(Into::into)
res.json().await.map_err(Into::into)
}
}