diff --git a/src-tauri/src/api/client.rs b/src-tauri/src/api/client.rs index 2194ce28a..1f8dad82f 100644 --- a/src-tauri/src/api/client.rs +++ b/src-tauri/src/api/client.rs @@ -452,6 +452,100 @@ impl QobuzClient { Ok(serde_json::from_value(genres.clone())?) } + /// Get discover index (home page content: playlists, ideal discography, etc.) + pub async fn get_discover_index( + &self, + genre_ids: Option>, + ) -> Result { + let url = endpoints::build_url(paths::DISCOVER_INDEX); + let mut query: Vec<(&str, String)> = vec![]; + + // Add genre_ids as comma-separated list if provided + if let Some(gids) = genre_ids { + if !gids.is_empty() { + let ids_str = gids.iter().map(|id| id.to_string()).collect::>().join(","); + query.push(("genre_ids", ids_str)); + } + } + + let response: Value = self + .http + .get(&url) + .header("X-App-Id", self.app_id().await?) + .header("X-User-Auth-Token", self.auth_token().await?) + .query(&query) + .send() + .await? + .json() + .await?; + + // Debug: log the response structure + if let Some(obj) = response.as_object() { + log::info!("Discover API response keys: {:?}", obj.keys().collect::>()); + if let Some(err) = obj.get("message") { + log::error!("Discover API error: {:?}", err); + } + if let Some(code) = obj.get("code") { + log::error!("Discover API error code: {:?}", code); + } + } + + Ok(serde_json::from_value(response)?) + } + + /// Get discover playlists with optional tag filter + /// Example: tags=label, tags=partner + pub async fn get_discover_playlists( + &self, + tag: Option, + limit: Option, + offset: Option, + ) -> Result { + let url = endpoints::build_url(paths::DISCOVER_PLAYLISTS); + let mut query: Vec<(&str, String)> = vec![]; + + // Add tag filter if provided (e.g., "label", "partner") + if let Some(ref t) = tag { + query.push(("tags", t.clone())); + } + + // Add limit (default 20) + let lim = limit.unwrap_or(20); + query.push(("limit", lim.to_string())); + + // Add offset (default 0) + let off = offset.unwrap_or(0); + query.push(("offset", off.to_string())); + + log::debug!("[API] get_discover_playlists URL: {} query: {:?}", url, query); + + // First get raw JSON to debug structure + let raw_response: serde_json::Value = self + .http + .get(&url) + .header("X-App-Id", self.app_id().await?) + .header("X-User-Auth-Token", self.auth_token().await?) + .query(&query) + .send() + .await? + .json() + .await?; + + log::debug!("[API] get_discover_playlists raw response keys: {:?}", raw_response.as_object().map(|o| o.keys().collect::>())); + + // Try to parse as expected structure + let response: DiscoverPlaylistsResponse = serde_json::from_value(raw_response.clone()) + .map_err(|e| { + log::error!("[API] Failed to parse discover playlists response: {}", e); + log::error!("[API] Raw response: {}", serde_json::to_string_pretty(&raw_response).unwrap_or_default()); + e + })?; + + log::debug!("[API] get_discover_playlists response: {} playlists", response.items.len()); + + Ok(response) + } + /// Get track by ID pub async fn get_track(&self, track_id: u64) -> Result { let url = endpoints::build_url(paths::TRACK_GET); @@ -468,6 +562,29 @@ impl QobuzClient { Ok(serde_json::from_value(response)?) } + /// Get artist by ID (basic info only - no albums, faster response) + pub async fn get_artist_basic(&self, artist_id: u64) -> Result { + let url = endpoints::build_url(paths::ARTIST_GET); + let locale = self.locale().await; + let query = vec![ + ("artist_id", artist_id.to_string()), + ("lang", locale), + // No "extra" parameter = only basic info (id, name, image) + ]; + + let response: Value = self + .http + .get(&url) + .header("X-App-Id", self.app_id().await?) + .query(&query) + .send() + .await? + .json() + .await?; + + Ok(serde_json::from_value(response)?) + } + /// Get artist by ID pub async fn get_artist( &self, diff --git a/src-tauri/src/api/endpoints.rs b/src-tauri/src/api/endpoints.rs index 493392909..bce20905a 100644 --- a/src-tauri/src/api/endpoints.rs +++ b/src-tauri/src/api/endpoints.rs @@ -45,6 +45,10 @@ pub mod paths { // Catalog (combined search) pub const CATALOG_SEARCH: &str = "/catalog/search"; + + // Discover (home page content) + pub const DISCOVER_INDEX: &str = "/discover/index"; + pub const DISCOVER_PLAYLISTS: &str = "/discover/playlists"; } /// Build full URL for an endpoint diff --git a/src-tauri/src/api/models.rs b/src-tauri/src/api/models.rs index 8b783d492..51a656e9a 100644 --- a/src-tauri/src/api/models.rs +++ b/src-tauri/src/api/models.rs @@ -7,9 +7,9 @@ use serde::{Deserialize, Serialize}; #[repr(u32)] pub enum Quality { Mp3 = 5, - Lossless = 6, // 16-bit/44.1kHz (CD Quality) - HiRes = 7, // 24-bit/≤96kHz - UltraHiRes = 27, // 24-bit/>96kHz + Lossless = 6, // 16-bit/44.1kHz (CD Quality) + HiRes = 7, // 24-bit/≤96kHz + UltraHiRes = 27, // 24-bit/>96kHz } impl Quality { @@ -38,7 +38,12 @@ impl Quality { /// Quality levels in descending order for fallback pub fn fallback_order() -> &'static [Quality] { - &[Quality::UltraHiRes, Quality::HiRes, Quality::Lossless, Quality::Mp3] + &[ + Quality::UltraHiRes, + Quality::HiRes, + Quality::Lossless, + Quality::Mp3, + ] } } @@ -344,3 +349,120 @@ pub struct Favorites { pub tracks: Option>, pub artists: Option>, } + +// ============ Discover API Models ============ + +/// Discover index response +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DiscoverResponse { + pub containers: DiscoverContainers, +} + +/// All discover containers +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DiscoverContainers { + pub playlists: Option>, + pub ideal_discography: Option>, + pub playlists_tags: Option>, + pub new_releases: Option>, +} + +/// Generic discover container +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DiscoverContainer { + pub id: String, + pub data: DiscoverData, +} + +/// Generic discover data with items +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DiscoverData { + pub has_more: bool, + pub items: Vec, +} + +/// Playlist from discover endpoint (different structure than regular Playlist) +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DiscoverPlaylist { + pub id: u64, + pub name: String, + pub owner: PlaylistOwner, + pub image: DiscoverPlaylistImage, + pub description: Option, + pub duration: u32, + pub tracks_count: u32, + pub genres: Option>, + pub tags: Option>, +} + +/// Playlist image structure from discover (has rectangle and covers) +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DiscoverPlaylistImage { + pub rectangle: Option, + pub covers: Option>, +} + +/// Playlist tag (for filtering) +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PlaylistTag { + pub id: u64, + pub slug: String, + pub name: String, +} + +/// Response from discover/playlists endpoint +/// Note: This endpoint returns items directly at root level (not wrapped in "playlists") +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DiscoverPlaylistsResponse { + pub has_more: bool, + pub items: Vec, +} + +/// Album from discover endpoint +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DiscoverAlbum { + pub id: String, + pub title: String, + pub version: Option, + pub track_count: Option, + pub duration: Option, + pub parental_warning: Option, + pub image: DiscoverAlbumImage, + pub artists: Vec, + pub label: Option