feat(playlists): add Qobuz playlist management API
- Add create_playlist API method and command - Add delete_playlist API method and command - Add add_tracks_to_playlist API method and command - Add remove_tracks_from_playlist API method and command - Add update_playlist API method and command - Register all new commands in invoke_handler
This commit is contained in:
1 parent
fb105948ac
commit
caca535cc0
3 files changed
+195
No files matched your search
@@ -478,6 +478,114 @@ impl QobuzClient {
|
|||||||
Ok(serde_json::from_value(playlists.clone())?)
|
Ok(serde_json::from_value(playlists.clone())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new playlist
|
||||||
|
pub async fn create_playlist(&self, name: &str, description: Option<&str>, is_public: bool) -> Result<Playlist> {
|
||||||
|
let url = endpoints::build_url(paths::PLAYLIST_CREATE);
|
||||||
|
|
||||||
|
let mut params = vec![
|
||||||
|
("name", name.to_string()),
|
||||||
|
("is_public", is_public.to_string()),
|
||||||
|
];
|
||||||
|
if let Some(desc) = description {
|
||||||
|
params.push(("description", desc.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let response: Playlist = self
|
||||||
|
.http
|
||||||
|
.get(&url)
|
||||||
|
.header("X-App-Id", self.app_id().await?)
|
||||||
|
.header("X-User-Auth-Token", self.auth_token().await?)
|
||||||
|
.query(¶ms)
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.json()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete a playlist
|
||||||
|
pub async fn delete_playlist(&self, playlist_id: u64) -> Result<()> {
|
||||||
|
let url = endpoints::build_url(paths::PLAYLIST_DELETE);
|
||||||
|
|
||||||
|
self.http
|
||||||
|
.get(&url)
|
||||||
|
.header("X-App-Id", self.app_id().await?)
|
||||||
|
.header("X-User-Auth-Token", self.auth_token().await?)
|
||||||
|
.query(&[("playlist_id", playlist_id.to_string())])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add tracks to a playlist
|
||||||
|
pub async fn add_tracks_to_playlist(&self, playlist_id: u64, track_ids: &[u64]) -> Result<()> {
|
||||||
|
let url = endpoints::build_url(paths::PLAYLIST_ADD_TRACKS);
|
||||||
|
let track_ids_str = track_ids.iter().map(|id| id.to_string()).collect::<Vec<_>>().join(",");
|
||||||
|
|
||||||
|
self.http
|
||||||
|
.get(&url)
|
||||||
|
.header("X-App-Id", self.app_id().await?)
|
||||||
|
.header("X-User-Auth-Token", self.auth_token().await?)
|
||||||
|
.query(&[
|
||||||
|
("playlist_id", playlist_id.to_string()),
|
||||||
|
("track_ids", track_ids_str),
|
||||||
|
])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove tracks from a playlist
|
||||||
|
pub async fn remove_tracks_from_playlist(&self, playlist_id: u64, playlist_track_ids: &[u64]) -> Result<()> {
|
||||||
|
let url = endpoints::build_url(paths::PLAYLIST_DELETE_TRACKS);
|
||||||
|
let track_ids_str = playlist_track_ids.iter().map(|id| id.to_string()).collect::<Vec<_>>().join(",");
|
||||||
|
|
||||||
|
self.http
|
||||||
|
.get(&url)
|
||||||
|
.header("X-App-Id", self.app_id().await?)
|
||||||
|
.header("X-User-Auth-Token", self.auth_token().await?)
|
||||||
|
.query(&[
|
||||||
|
("playlist_id", playlist_id.to_string()),
|
||||||
|
("playlist_track_ids", track_ids_str),
|
||||||
|
])
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update playlist metadata
|
||||||
|
pub async fn update_playlist(&self, playlist_id: u64, name: Option<&str>, description: Option<&str>, is_public: Option<bool>) -> Result<Playlist> {
|
||||||
|
let url = endpoints::build_url(paths::PLAYLIST_UPDATE);
|
||||||
|
|
||||||
|
let mut params = vec![("playlist_id", playlist_id.to_string())];
|
||||||
|
if let Some(n) = name {
|
||||||
|
params.push(("name", n.to_string()));
|
||||||
|
}
|
||||||
|
if let Some(d) = description {
|
||||||
|
params.push(("description", d.to_string()));
|
||||||
|
}
|
||||||
|
if let Some(p) = is_public {
|
||||||
|
params.push(("is_public", p.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let response: Playlist = self
|
||||||
|
.http
|
||||||
|
.get(&url)
|
||||||
|
.header("X-App-Id", self.app_id().await?)
|
||||||
|
.header("X-User-Auth-Token", self.auth_token().await?)
|
||||||
|
.query(¶ms)
|
||||||
|
.send()
|
||||||
|
.await?
|
||||||
|
.json()
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
/// Add item to favorites
|
/// Add item to favorites
|
||||||
pub async fn add_favorite(&self, fav_type: &str, item_id: &str) -> Result<()> {
|
pub async fn add_favorite(&self, fav_type: &str, item_id: &str) -> Result<()> {
|
||||||
let url = endpoints::build_url(paths::FAVORITE_CREATE);
|
let url = endpoints::build_url(paths::FAVORITE_CREATE);
|
||||||
|
|||||||
@@ -49,3 +49,85 @@ pub async fn search_playlists(
|
|||||||
.await
|
.await
|
||||||
.map_err(|e| format!("Failed to search playlists: {}", e))
|
.map_err(|e| format!("Failed to search playlists: {}", e))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a new playlist
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn create_playlist(
|
||||||
|
name: String,
|
||||||
|
description: Option<String>,
|
||||||
|
is_public: Option<bool>,
|
||||||
|
state: State<'_, AppState>,
|
||||||
|
) -> Result<Playlist, String> {
|
||||||
|
log::info!("Command: create_playlist \"{}\"", name);
|
||||||
|
|
||||||
|
let client = state.client.lock().await;
|
||||||
|
client
|
||||||
|
.create_playlist(&name, description.as_deref(), is_public.unwrap_or(false))
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to create playlist: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Delete a playlist
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn delete_playlist(
|
||||||
|
playlist_id: u64,
|
||||||
|
state: State<'_, AppState>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
log::info!("Command: delete_playlist {}", playlist_id);
|
||||||
|
|
||||||
|
let client = state.client.lock().await;
|
||||||
|
client
|
||||||
|
.delete_playlist(playlist_id)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to delete playlist: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Add tracks to a playlist
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn add_tracks_to_playlist(
|
||||||
|
playlist_id: u64,
|
||||||
|
track_ids: Vec<u64>,
|
||||||
|
state: State<'_, AppState>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
log::info!("Command: add_tracks_to_playlist {} ({} tracks)", playlist_id, track_ids.len());
|
||||||
|
|
||||||
|
let client = state.client.lock().await;
|
||||||
|
client
|
||||||
|
.add_tracks_to_playlist(playlist_id, &track_ids)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to add tracks to playlist: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Remove tracks from a playlist
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn remove_tracks_from_playlist(
|
||||||
|
playlist_id: u64,
|
||||||
|
playlist_track_ids: Vec<u64>,
|
||||||
|
state: State<'_, AppState>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
log::info!("Command: remove_tracks_from_playlist {} ({} tracks)", playlist_id, playlist_track_ids.len());
|
||||||
|
|
||||||
|
let client = state.client.lock().await;
|
||||||
|
client
|
||||||
|
.remove_tracks_from_playlist(playlist_id, &playlist_track_ids)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to remove tracks from playlist: {}", e))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Update playlist metadata
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn update_playlist(
|
||||||
|
playlist_id: u64,
|
||||||
|
name: Option<String>,
|
||||||
|
description: Option<String>,
|
||||||
|
is_public: Option<bool>,
|
||||||
|
state: State<'_, AppState>,
|
||||||
|
) -> Result<Playlist, String> {
|
||||||
|
log::info!("Command: update_playlist {}", playlist_id);
|
||||||
|
|
||||||
|
let client = state.client.lock().await;
|
||||||
|
client
|
||||||
|
.update_playlist(playlist_id, name.as_deref(), description.as_deref(), is_public)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format!("Failed to update playlist: {}", e))
|
||||||
|
}
|
||||||
@@ -136,6 +136,11 @@ pub fn run() {
|
|||||||
commands::get_user_playlists,
|
commands::get_user_playlists,
|
||||||
commands::get_playlist,
|
commands::get_playlist,
|
||||||
commands::search_playlists,
|
commands::search_playlists,
|
||||||
|
commands::create_playlist,
|
||||||
|
commands::delete_playlist,
|
||||||
|
commands::add_tracks_to_playlist,
|
||||||
|
commands::remove_tracks_from_playlist,
|
||||||
|
commands::update_playlist,
|
||||||
// Favorites commands
|
// Favorites commands
|
||||||
commands::get_favorites,
|
commands::get_favorites,
|
||||||
commands::add_favorite,
|
commands::add_favorite,
|
||||||
|
|||||||
Reference in new issue
Block a user