public async Task <IEnumerable <SpotifyArtist> > SearchForArtist(string query, ArtistOption option, int limit = 10)
        {
            var artists = await _spotifyHttpClient.Search.SearchArtists(query, limit);

            var spotifyArtists = new List <SpotifyArtist>();

            foreach (var artist in artists)
            {
                artist.Tracks = option switch
                {
                    ArtistOption.Discography => await _albumService.GetTracksFromAlbumCollection(await _artistService.GetDiscographyForArtist(artist.Id)),
                    ArtistOption.Popular => await _artistService.GetTopTracksForArtist(artist.Id),
                    ArtistOption.Essential => (await SearchForPlaylist($"This Is {artist.Name}")).SelectMany(spotifyPlaylist => spotifyPlaylist.Tracks),
                    _ => artist.Tracks
                };

                if (artist.Tracks != null && !artist.Tracks.IsNullOrEmpty())
                {
                    spotifyArtists.Add(artist);
                }
            }

            if (spotifyArtists.Count == 0)
            {
                throw new NoSearchResultException();
            }

            return(spotifyArtists);
        }
Exemple #2
0
        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);
        }