public async Task <SpotifyArtist> SearchForArtist(string query, ArtistOption option) { var metadataResponse = await _httpClient.GetAsync($"https://api.spotify.com/v1/search?q={query}&type=artist"); metadataResponse.EnsureSpotifySuccess(); var artist = (await JsonSerializerExtensions.DeserializeAnonymousTypeAsync( await metadataResponse.Content.ReadAsStreamAsync(), new { artists = new { items = default(IEnumerable <SpotifyArtist>) } })).artists.items.FirstOrDefault(); if (artist == null) { throw new NoSearchResultException(); } if (option == ArtistOption.Discography) { artist.Tracks = await _albumService.GetTracksFromAlbumCollection(await _artistService.GetDiscographyForArtist(artist.Id)); } if (option == ArtistOption.Popular) { artist.Tracks = await _artistService.GetTopTracksForArtist(artist.Id); } if (option == ArtistOption.Essential) { artist.Tracks = (await SearchForPlaylist($"This Is {artist.Name}")).Tracks; } if (artist.Tracks.IsNullOrEmpty()) { throw new NoSearchResultException(); } return(artist); }
public async Task <IEnumerable <SpotifyTrack> > GetTracksInPlaylist(string id) { var response = await _httpClient.GetAsync($"https://api.spotify.com/v1/playlists/{id}/tracks?market=GB"); response.EnsureSpotifySuccess(); var items = (await JsonSerializerExtensions.DeserializeAnonymousTypeAsync(await response.Content.ReadAsStreamAsync(), new { items = default(IEnumerable <SpotifyPlaylistTrack>) })).items.ToList(); if (items.IsNullOrEmpty()) { throw new NoSearchResultException(); } return(items.Select(x => x.Track)); }
public async Task <SpotifyAlbum> SearchForAlbum(string query) { var metadataResponse = await _httpClient.GetAsync($"https://api.spotify.com/v1/search?q={query}&type=album"); metadataResponse.EnsureSpotifySuccess(); var album = (await JsonSerializerExtensions.DeserializeAnonymousTypeAsync(await metadataResponse.Content.ReadAsStreamAsync(), new { albums = new { items = default(IEnumerable <SpotifyAlbum>) } })).albums.items.FirstOrDefault(); if (album == null) { throw new NoSearchResultException(); } var albumResponse = await _httpClient.GetAsync($"https://api.spotify.com/v1/albums/{album.Id}/tracks"); albumResponse.EnsureSpotifySuccess(); album.Tracks = (await JsonSerializerExtensions.DeserializeAnonymousTypeAsync( await albumResponse.Content.ReadAsStreamAsync(), new { items = default(IEnumerable <SpotifyTrack>) })).items; if (album.Tracks.IsNullOrEmpty()) { throw new NoSearchResultException(); } return(album); }