44 lines
1.3 KiB
C#
44 lines
1.3 KiB
C#
using SpotifyAPI.Web;
|
|
|
|
public class SpotifyApiClient
|
|
{
|
|
private SpotifyClient _spotifyClient;
|
|
|
|
public SpotifyApiClient()
|
|
{
|
|
var config = SpotifyClientConfig.CreateDefault()
|
|
.WithAuthenticator(new ClientCredentialsAuthenticator(
|
|
AppConfiguration.Instance.SpotifyClientId,
|
|
AppConfiguration.Instance.SpotifyClientSecret));
|
|
|
|
_spotifyClient = new SpotifyClient(config);
|
|
}
|
|
|
|
public async Task<List<FullTrack>> GetTrackCandidatesAsync(string trackName, string artistName)
|
|
{
|
|
try
|
|
{
|
|
var searchResponse = await _spotifyClient.Search.Item(new SearchRequest(SearchRequest.Types.Track, $"{trackName} {artistName}")
|
|
{
|
|
Limit = 5
|
|
});
|
|
return searchResponse.Tracks.Items ?? new List<FullTrack>();
|
|
}
|
|
catch (APIException ex)
|
|
{
|
|
throw new Exception($"Error fetching tracks by query: \"{trackName} {artistName}\": {ex.Message}", ex);
|
|
}
|
|
}
|
|
|
|
public async Task<FullTrack> GetTrackByIdAsync(string trackId)
|
|
{
|
|
try
|
|
{
|
|
return await _spotifyClient.Tracks.Get(trackId);
|
|
}
|
|
catch (APIException ex)
|
|
{
|
|
throw new Exception($"Error fetching track by ID: {trackId}: {ex.Message}", ex);
|
|
}
|
|
}
|
|
} |