387 lines
12 KiB
Rust
387 lines
12 KiB
Rust
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 put_json<T: DeserializeOwned>(url: &str, body: &str) -> Result<T, ApiError> {
|
|
let resp = Request::put(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 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(())
|
|
}
|
|
|
|
// --- Auth ---
|
|
pub async fn check_setup_required() -> Result<SetupRequired, ApiError> {
|
|
get_json(&format!("{BASE}/auth/setup-required")).await
|
|
}
|
|
|
|
pub async fn setup(username: &str, password: &str) -> Result<UserInfo, ApiError> {
|
|
let body = serde_json::json!({"username": username, "password": password}).to_string();
|
|
post_json(&format!("{BASE}/auth/setup"), &body).await
|
|
}
|
|
|
|
pub async fn login(username: &str, password: &str) -> Result<UserInfo, ApiError> {
|
|
let body = serde_json::json!({"username": username, "password": password}).to_string();
|
|
post_json(&format!("{BASE}/auth/login"), &body).await
|
|
}
|
|
|
|
pub async fn logout() -> Result<(), ApiError> {
|
|
let resp = Request::post(&format!("{BASE}/auth/logout"))
|
|
.send()
|
|
.await
|
|
.map_err(|e| ApiError(e.to_string()))?;
|
|
if !resp.ok() {
|
|
return Err(ApiError(format!("HTTP {}", resp.status())));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn get_me() -> Result<UserInfo, ApiError> {
|
|
get_json(&format!("{BASE}/auth/me")).await
|
|
}
|
|
|
|
// --- Lyrics ---
|
|
pub async fn get_lyrics(artist: &str, title: &str) -> Result<LyricsResult, ApiError> {
|
|
get_json(&format!("{BASE}/lyrics?artist={artist}&title={title}")).await
|
|
}
|
|
|
|
// --- 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<ArtistListItem>, ApiError> {
|
|
get_json(&format!("{BASE}/artists?limit={limit}&offset={offset}")).await
|
|
}
|
|
|
|
pub async fn get_artist_full(id: &str) -> Result<FullArtistDetail, ApiError> {
|
|
get_json(&format!("{BASE}/artists/{id}/full")).await
|
|
}
|
|
|
|
pub async fn get_artist_full_quick(id: &str) -> Result<FullArtistDetail, ApiError> {
|
|
get_json(&format!("{BASE}/artists/{id}/full?quick=true")).await
|
|
}
|
|
|
|
pub async fn get_album(mbid: &str) -> Result<MbAlbumDetail, ApiError> {
|
|
get_json(&format!("{BASE}/albums/{mbid}")).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
|
|
}
|
|
|
|
// --- 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
|
|
}
|
|
|
|
// --- Pipeline ---
|
|
pub async fn trigger_pipeline() -> Result<PipelineRef, ApiError> {
|
|
post_empty(&format!("{BASE}/pipeline")).await
|
|
}
|
|
|
|
// --- Monitor ---
|
|
pub async fn set_artist_monitored(id: i32, monitored: bool) -> Result<serde_json::Value, ApiError> {
|
|
if monitored {
|
|
post_empty(&format!("{BASE}/artists/{id}/monitor")).await
|
|
} else {
|
|
let resp = Request::delete(&format!("{BASE}/artists/{id}/monitor"))
|
|
.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()))
|
|
}
|
|
}
|
|
|
|
pub async fn trigger_monitor_check() -> Result<TaskRef, ApiError> {
|
|
post_empty(&format!("{BASE}/monitor/check")).await
|
|
}
|
|
|
|
// --- Scheduler ---
|
|
pub async fn skip_scheduled_pipeline() -> Result<serde_json::Value, ApiError> {
|
|
post_empty(&format!("{BASE}/scheduler/skip-pipeline")).await
|
|
}
|
|
|
|
pub async fn skip_scheduled_monitor() -> Result<serde_json::Value, ApiError> {
|
|
post_empty(&format!("{BASE}/scheduler/skip-monitor")).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_config() -> Result<AppConfig, ApiError> {
|
|
get_json(&format!("{BASE}/config")).await
|
|
}
|
|
|
|
pub async fn save_config(config: &AppConfig) -> Result<AppConfig, ApiError> {
|
|
let body = serde_json::to_string(config).map_err(|e| ApiError(e.to_string()))?;
|
|
let resp = Request::put(&format!("{BASE}/config"))
|
|
.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()))
|
|
}
|
|
|
|
// --- Playlists ---
|
|
pub async fn generate_playlist(req: &GenerateRequest) -> Result<GeneratedPlaylist, ApiError> {
|
|
let body = serde_json::to_string(req).map_err(|e| ApiError(e.to_string()))?;
|
|
post_json(&format!("{BASE}/playlists/generate"), &body).await
|
|
}
|
|
|
|
pub async fn save_playlist(
|
|
name: &str,
|
|
description: Option<&str>,
|
|
track_ids: &[i32],
|
|
) -> Result<serde_json::Value, ApiError> {
|
|
let body = serde_json::json!({
|
|
"name": name,
|
|
"description": description,
|
|
"track_ids": track_ids,
|
|
})
|
|
.to_string();
|
|
post_json(&format!("{BASE}/playlists"), &body).await
|
|
}
|
|
|
|
pub async fn list_playlists() -> Result<Vec<PlaylistSummary>, ApiError> {
|
|
get_json(&format!("{BASE}/playlists")).await
|
|
}
|
|
|
|
pub async fn get_playlist(id: i32) -> Result<PlaylistDetail, ApiError> {
|
|
get_json(&format!("{BASE}/playlists/{id}")).await
|
|
}
|
|
|
|
pub async fn delete_playlist(id: i32) -> Result<(), ApiError> {
|
|
delete(&format!("{BASE}/playlists/{id}")).await
|
|
}
|
|
|
|
pub fn export_m3u_url(id: i32) -> String {
|
|
format!("{BASE}/playlists/{id}/m3u")
|
|
}
|
|
|
|
pub async fn add_track_to_playlist(
|
|
playlist_id: i32,
|
|
track_id: i32,
|
|
) -> Result<serde_json::Value, ApiError> {
|
|
let body = serde_json::json!({"track_id": track_id}).to_string();
|
|
post_json(&format!("{BASE}/playlists/{playlist_id}/tracks"), &body).await
|
|
}
|
|
|
|
pub async fn remove_track_from_playlist(playlist_id: i32, track_id: i32) -> Result<(), ApiError> {
|
|
delete(&format!("{BASE}/playlists/{playlist_id}/tracks/{track_id}")).await
|
|
}
|
|
|
|
pub async fn reorder_playlist_tracks(
|
|
playlist_id: i32,
|
|
track_ids: &[i32],
|
|
) -> Result<serde_json::Value, ApiError> {
|
|
let body = serde_json::json!({"track_ids": track_ids}).to_string();
|
|
put_json(&format!("{BASE}/playlists/{playlist_id}/tracks"), &body).await
|
|
}
|
|
|
|
pub async fn search_tracks(query: &str) -> Result<Vec<Track>, ApiError> {
|
|
get_json(&format!("{BASE}/tracks?q={query}&limit=50")).await
|
|
}
|
|
|
|
// --- Subsonic ---
|
|
|
|
pub async fn get_subsonic_password_status() -> Result<SubsonicPasswordStatus, ApiError> {
|
|
get_json(&format!("{BASE}/auth/subsonic-password-status")).await
|
|
}
|
|
|
|
pub async fn set_subsonic_password(password: &str) -> Result<serde_json::Value, ApiError> {
|
|
let body = serde_json::json!({"password": password}).to_string();
|
|
put_json(&format!("{BASE}/auth/subsonic-password"), &body).await
|
|
}
|
|
|
|
// --- YouTube Auth ---
|
|
|
|
pub async fn get_ytauth_status() -> Result<YtAuthStatus, ApiError> {
|
|
get_json(&format!("{BASE}/ytauth/status")).await
|
|
}
|
|
|
|
pub async fn ytauth_login_start() -> Result<serde_json::Value, ApiError> {
|
|
post_empty(&format!("{BASE}/ytauth/login-start")).await
|
|
}
|
|
|
|
pub async fn ytauth_login_stop() -> Result<serde_json::Value, ApiError> {
|
|
post_empty(&format!("{BASE}/ytauth/login-stop")).await
|
|
}
|
|
|
|
pub async fn ytauth_refresh() -> Result<serde_json::Value, ApiError> {
|
|
post_empty(&format!("{BASE}/ytauth/refresh")).await
|
|
}
|
|
|
|
pub async fn ytauth_clear_cookies() -> Result<(), ApiError> {
|
|
delete(&format!("{BASE}/ytauth/cookies")).await
|
|
}
|
|
|
|
// --- MusicBrainz Local DB ---
|
|
|
|
pub async fn get_mb_status() -> Result<MbStatus, ApiError> {
|
|
get_json(&format!("{BASE}/mb-status")).await
|
|
}
|
|
|
|
pub async fn trigger_mb_import() -> Result<TaskRef, ApiError> {
|
|
post_empty(&format!("{BASE}/mb-import")).await
|
|
}
|