Major early updates

This commit is contained in:
Connor Johnstone
2026-03-18 10:56:19 -04:00
parent 50a0ddcdbc
commit 55607df07b
20 changed files with 2665 additions and 1 deletions

185
frontend/src/api.rs Normal file
View File

@@ -0,0 +1,185 @@
use gloo_net::http::Request;
use serde::de::DeserializeOwned;
use crate::types::*;
const BASE: &str = "/api";
#[derive(Debug, Clone)]
pub struct ApiError(pub String);
async fn get_json<T: DeserializeOwned>(url: &str) -> Result<T, ApiError> {
let resp = Request::get(url)
.send()
.await
.map_err(|e| ApiError(e.to_string()))?;
if !resp.ok() {
return Err(ApiError(format!("HTTP {}", resp.status())));
}
resp.json().await.map_err(|e| ApiError(e.to_string()))
}
async fn post_json<T: DeserializeOwned>(url: &str, body: &str) -> Result<T, ApiError> {
let resp = Request::post(url)
.header("Content-Type", "application/json")
.body(body)
.map_err(|e| ApiError(e.to_string()))?
.send()
.await
.map_err(|e| ApiError(e.to_string()))?;
if !resp.ok() {
return Err(ApiError(format!("HTTP {}", resp.status())));
}
resp.json().await.map_err(|e| ApiError(e.to_string()))
}
async fn post_empty<T: DeserializeOwned>(url: &str) -> Result<T, ApiError> {
let resp = Request::post(url)
.send()
.await
.map_err(|e| ApiError(e.to_string()))?;
if !resp.ok() {
return Err(ApiError(format!("HTTP {}", resp.status())));
}
resp.json().await.map_err(|e| ApiError(e.to_string()))
}
async fn delete(url: &str) -> Result<(), ApiError> {
let resp = Request::delete(url)
.send()
.await
.map_err(|e| ApiError(e.to_string()))?;
if !resp.ok() {
return Err(ApiError(format!("HTTP {}", resp.status())));
}
Ok(())
}
// --- Status ---
pub async fn get_status() -> Result<Status, ApiError> {
get_json(&format!("{BASE}/status")).await
}
// --- Search ---
pub async fn search_artist(query: &str, limit: u32) -> Result<Vec<ArtistResult>, ApiError> {
get_json(&format!("{BASE}/search/artist?q={query}&limit={limit}")).await
}
pub async fn search_album(query: &str, artist: Option<&str>, limit: u32) -> Result<Vec<AlbumResult>, ApiError> {
let mut url = format!("{BASE}/search/album?q={query}&limit={limit}");
if let Some(a) = artist {
url.push_str(&format!("&artist={a}"));
}
get_json(&url).await
}
pub async fn search_track(query: &str, artist: Option<&str>, limit: u32) -> Result<Vec<TrackResult>, ApiError> {
let mut url = format!("{BASE}/search/track?q={query}&limit={limit}");
if let Some(a) = artist {
url.push_str(&format!("&artist={a}"));
}
get_json(&url).await
}
// --- Library ---
pub async fn list_artists(limit: u64, offset: u64) -> Result<Vec<Artist>, ApiError> {
get_json(&format!("{BASE}/artists?limit={limit}&offset={offset}")).await
}
pub async fn get_artist(id: i32) -> Result<ArtistDetail, ApiError> {
get_json(&format!("{BASE}/artists/{id}")).await
}
pub async fn get_album(id: i32) -> Result<AlbumDetail, ApiError> {
get_json(&format!("{BASE}/albums/{id}")).await
}
pub async fn list_tracks(limit: u64, offset: u64) -> Result<Vec<Track>, ApiError> {
get_json(&format!("{BASE}/tracks?limit={limit}&offset={offset}")).await
}
// --- Watchlist ---
pub async fn add_artist(name: &str, mbid: Option<&str>) -> Result<AddSummary, ApiError> {
let body = match mbid {
Some(m) => format!(r#"{{"name":"{name}","mbid":"{m}"}}"#),
None => format!(r#"{{"name":"{name}"}}"#),
};
post_json(&format!("{BASE}/artists"), &body).await
}
pub async fn add_album(artist: &str, album: &str, mbid: Option<&str>) -> Result<AddSummary, ApiError> {
let body = match mbid {
Some(m) => format!(r#"{{"artist":"{artist}","album":"{album}","mbid":"{m}"}}"#),
None => format!(r#"{{"artist":"{artist}","album":"{album}"}}"#),
};
post_json(&format!("{BASE}/albums"), &body).await
}
pub async fn get_watchlist() -> Result<Vec<WatchListEntry>, ApiError> {
get_json(&format!("{BASE}/watchlist")).await
}
pub async fn remove_watchlist(id: i32) -> Result<(), ApiError> {
delete(&format!("{BASE}/watchlist/{id}")).await
}
// --- Downloads ---
pub async fn get_downloads(status: Option<&str>) -> Result<Vec<DownloadItem>, ApiError> {
let mut url = format!("{BASE}/downloads/queue");
if let Some(s) = status {
url.push_str(&format!("?status={s}"));
}
get_json(&url).await
}
pub async fn enqueue_download(query: &str) -> Result<DownloadItem, ApiError> {
post_json(
&format!("{BASE}/downloads"),
&format!(r#"{{"query":"{query}"}}"#),
)
.await
}
pub async fn sync_downloads() -> Result<SyncStats, ApiError> {
post_empty(&format!("{BASE}/downloads/sync")).await
}
pub async fn process_downloads() -> Result<TaskRef, ApiError> {
post_empty(&format!("{BASE}/downloads/process")).await
}
pub async fn retry_download(id: i32) -> Result<(), ApiError> {
let resp = Request::post(&format!("{BASE}/downloads/retry/{id}"))
.send()
.await
.map_err(|e| ApiError(e.to_string()))?;
if !resp.ok() {
return Err(ApiError(format!("HTTP {}", resp.status())));
}
Ok(())
}
pub async fn cancel_download(id: i32) -> Result<(), ApiError> {
delete(&format!("{BASE}/downloads/{id}")).await
}
// --- System ---
pub async fn trigger_index() -> Result<TaskRef, ApiError> {
post_empty(&format!("{BASE}/index")).await
}
pub async fn trigger_tag() -> Result<TaskRef, ApiError> {
post_empty(&format!("{BASE}/tag")).await
}
pub async fn trigger_organize() -> Result<TaskRef, ApiError> {
post_empty(&format!("{BASE}/organize")).await
}
pub async fn get_task(id: &str) -> Result<TaskInfo, ApiError> {
get_json(&format!("{BASE}/tasks/{id}")).await
}
pub async fn get_config() -> Result<AppConfig, ApiError> {
get_json(&format!("{BASE}/config")).await
}