using System; using NLog; using SpotifyAPI.Web; using SpotifyAPI.Web.Enums; using SpotifyAPI.Web.Models; namespace NzbDrone.Core.ImportLists.Spotify { public interface ISpotifyProxy { PrivateProfile GetPrivateProfile(SpotifyImportListBase list, SpotifyWebAPI api) where TSettings : SpotifySettingsBase, new(); Paging GetUserPlaylists(SpotifyImportListBase list, SpotifyWebAPI api, string id) where TSettings : SpotifySettingsBase, new(); FollowedArtists GetFollowedArtists(SpotifyImportListBase list, SpotifyWebAPI api) where TSettings : SpotifySettingsBase, new(); Paging GetSavedAlbums(SpotifyImportListBase list, SpotifyWebAPI api) where TSettings : SpotifySettingsBase, new(); Paging GetPlaylistTracks(SpotifyImportListBase list, SpotifyWebAPI api, string id, string fields) where TSettings : SpotifySettingsBase, new(); Paging GetSavedTracks(SpotifyImportListBase list, SpotifyWebAPI api) where TSettings : SpotifySettingsBase, new(); Paging GetNextPage(SpotifyImportListBase list, SpotifyWebAPI api, Paging item) where TSettings : SpotifySettingsBase, new(); FollowedArtists GetNextPage(SpotifyImportListBase list, SpotifyWebAPI api, FollowedArtists item) where TSettings : SpotifySettingsBase, new(); } public class SpotifyProxy : ISpotifyProxy { private readonly Logger _logger; public SpotifyProxy(Logger logger) { _logger = logger; } public PrivateProfile GetPrivateProfile(SpotifyImportListBase list, SpotifyWebAPI api) where TSettings : SpotifySettingsBase, new() { return Execute(list, api, x => x.GetPrivateProfile()); } public Paging GetUserPlaylists(SpotifyImportListBase list, SpotifyWebAPI api, string id) where TSettings : SpotifySettingsBase, new() { return Execute(list, api, x => x.GetUserPlaylists(id)); } public FollowedArtists GetFollowedArtists(SpotifyImportListBase list, SpotifyWebAPI api) where TSettings : SpotifySettingsBase, new() { return Execute(list, api, x => x.GetFollowedArtists(FollowType.Artist, 50)); } public Paging GetSavedAlbums(SpotifyImportListBase list, SpotifyWebAPI api) where TSettings : SpotifySettingsBase, new() { return Execute(list, api, x => x.GetSavedAlbums(50)); } public Paging GetPlaylistTracks(SpotifyImportListBase list, SpotifyWebAPI api, string id, string fields) where TSettings : SpotifySettingsBase, new() { return Execute(list, api, x => x.GetPlaylistTracks(id, fields: fields)); } public Paging GetSavedTracks(SpotifyImportListBase list, SpotifyWebAPI api) where TSettings : SpotifySettingsBase, new() { return Execute(list, api, x => x.GetSavedTracks(50)); } public Paging GetNextPage(SpotifyImportListBase list, SpotifyWebAPI api, Paging item) where TSettings : SpotifySettingsBase, new() { return Execute(list, api, (x) => x.GetNextPage(item)); } public FollowedArtists GetNextPage(SpotifyImportListBase list, SpotifyWebAPI api, FollowedArtists item) where TSettings : SpotifySettingsBase, new() { return Execute(list, api, (x) => x.GetNextPage(item.Artists)); } public T Execute(SpotifyImportListBase list, SpotifyWebAPI api, Func method, bool allowReauth = true) where T : BasicModel where TSettings : SpotifySettingsBase, new() { var result = method(api); if (result.HasError()) { // If unauthorized, refresh token and try again if (result.Error.Status == 401) { if (allowReauth) { _logger.Debug("Spotify authorization error, refreshing token and retrying"); list.RefreshToken(); api.AccessToken = list.AccessToken; return Execute(list, api, method, false); } else { throw new SpotifyAuthorizationException(result.Error.Message); } } else { throw new SpotifyException("[{0}] {1}", result.Error.Status, result.Error.Message); } } return result; } } }