feat: basic initial implementation of spotify client link validator and song submission form refs: NOISSUE
This commit is contained in:
44
song_of_the_day/SpotifyIntegration/SpotifyClient.cs
Normal file
44
song_of_the_day/SpotifyIntegration/SpotifyClient.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user