Fixed: NRE importing Spotify saved albums / followed artists (#962)
parent
e52013e85a
commit
1b72d9b60f
@ -0,0 +1,173 @@
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.ImportLists.Spotify;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using SpotifyAPI.Web;
|
||||
using SpotifyAPI.Web.Models;
|
||||
|
||||
namespace NzbDrone.Core.Test.ImportListTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class SpotifyFollowedArtistsFixture : CoreTest<SpotifyFollowedArtists>
|
||||
{
|
||||
// placeholder, we don't use real API
|
||||
private readonly SpotifyWebAPI api = null;
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_followed_is_null()
|
||||
{
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetFollowedArtists(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(default(FollowedArtists));
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_followed_artists_is_null()
|
||||
{
|
||||
var followed = new FollowedArtists {
|
||||
Artists = null
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetFollowedArtists(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(followed);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_followed_artist_items_is_null()
|
||||
{
|
||||
var followed = new FollowedArtists {
|
||||
Artists = new CursorPaging<FullArtist> {
|
||||
Items = null
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetFollowedArtists(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(followed);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
Subject.Fetch(api);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_artist_is_null()
|
||||
{
|
||||
var followed = new FollowedArtists {
|
||||
Artists = new CursorPaging<FullArtist> {
|
||||
Items = new List<FullArtist> {
|
||||
null
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetFollowedArtists(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(followed);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
Subject.Fetch(api);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_parse_followed_artist()
|
||||
{
|
||||
var followed = new FollowedArtists {
|
||||
Artists = new CursorPaging<FullArtist> {
|
||||
Items = new List<FullArtist> {
|
||||
new FullArtist {
|
||||
Name = "artist"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetFollowedArtists(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(followed);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_get_next_page_returns_null()
|
||||
{
|
||||
var followed = new FollowedArtists {
|
||||
Artists = new CursorPaging<FullArtist> {
|
||||
Items = new List<FullArtist> {
|
||||
new FullArtist {
|
||||
Name = "artist"
|
||||
}
|
||||
},
|
||||
Next = "DummyToMakeHasNextTrue"
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetFollowedArtists(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(followed);
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>()
|
||||
.Setup(x => x.GetNextPage(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<CursorPaging<FullArtist>>()))
|
||||
.Returns(default(CursorPaging<FullArtist>));
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>()
|
||||
.Verify(v => v.GetNextPage(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<CursorPaging<FullArtist>>()),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[TestCase(null)]
|
||||
[TestCase("")]
|
||||
public void should_skip_bad_artist_names(string name)
|
||||
{
|
||||
var followed = new FollowedArtists {
|
||||
Artists = new CursorPaging<FullArtist> {
|
||||
Items = new List<FullArtist> {
|
||||
new FullArtist {
|
||||
Name = name
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetFollowedArtists(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(followed);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,239 @@
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.ImportLists.Spotify;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using SpotifyAPI.Web;
|
||||
using SpotifyAPI.Web.Models;
|
||||
|
||||
namespace NzbDrone.Core.Test.ImportListTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class SpotifyPlaylistFixture : CoreTest<SpotifyPlaylist>
|
||||
{
|
||||
// placeholder, we don't use real API
|
||||
private readonly SpotifyWebAPI api = null;
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_playlist_tracks_is_null()
|
||||
{
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetPlaylistTracks(It.IsAny<SpotifyPlaylist>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns(default(Paging<PlaylistTrack>));
|
||||
|
||||
var result = Subject.Fetch(api, "playlistid");
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_playlist_tracks_items_is_null()
|
||||
{
|
||||
var playlistTracks = new Paging<PlaylistTrack> {
|
||||
Items = null
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetPlaylistTracks(It.IsAny<SpotifyPlaylist>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns(playlistTracks);
|
||||
|
||||
var result = Subject.Fetch(api, "playlistid");
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_playlist_track_is_null()
|
||||
{
|
||||
var playlistTracks = new Paging<PlaylistTrack> {
|
||||
Items = new List<PlaylistTrack> {
|
||||
null
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetPlaylistTracks(It.IsAny<SpotifyPlaylist>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns(playlistTracks);
|
||||
|
||||
var result = Subject.Fetch(api, "playlistid");
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_use_album_artist_when_it_exists()
|
||||
{
|
||||
var playlistTracks = new Paging<PlaylistTrack> {
|
||||
Items = new List<PlaylistTrack> {
|
||||
new PlaylistTrack {
|
||||
Track = new FullTrack {
|
||||
Album = new SimpleAlbum {
|
||||
Name = "Album",
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = "AlbumArtist"
|
||||
}
|
||||
}
|
||||
},
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = "TrackArtist"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetPlaylistTracks(It.IsAny<SpotifyPlaylist>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns(playlistTracks);
|
||||
|
||||
var result = Subject.Fetch(api, "playlistid");
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result[0].Artist.Should().Be("AlbumArtist");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_fall_back_to_track_artist_if_album_artist_missing()
|
||||
{
|
||||
var playlistTracks = new Paging<PlaylistTrack> {
|
||||
Items = new List<PlaylistTrack> {
|
||||
new PlaylistTrack {
|
||||
Track = new FullTrack {
|
||||
Album = new SimpleAlbum {
|
||||
Name = "Album",
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = null
|
||||
}
|
||||
}
|
||||
},
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = "TrackArtist"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetPlaylistTracks(It.IsAny<SpotifyPlaylist>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns(playlistTracks);
|
||||
|
||||
var result = Subject.Fetch(api, "playlistid");
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
result[0].Artist.Should().Be("TrackArtist");
|
||||
}
|
||||
|
||||
|
||||
[TestCase(null, null, "Album")]
|
||||
[TestCase("AlbumArtist", null, null)]
|
||||
[TestCase(null, "TrackArtist", null)]
|
||||
public void should_skip_bad_artist_or_album_names(string albumArtistName, string trackArtistName, string albumName)
|
||||
{
|
||||
var playlistTracks = new Paging<PlaylistTrack> {
|
||||
Items = new List<PlaylistTrack> {
|
||||
new PlaylistTrack {
|
||||
Track = new FullTrack {
|
||||
Album = new SimpleAlbum {
|
||||
Name = albumName,
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = albumArtistName
|
||||
}
|
||||
}
|
||||
},
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = trackArtistName
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetPlaylistTracks(It.IsAny<SpotifyPlaylist>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns(playlistTracks);
|
||||
|
||||
var result = Subject.Fetch(api, "playlistid");
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_get_next_page_returns_null()
|
||||
{
|
||||
var playlistTracks = new Paging<PlaylistTrack> {
|
||||
Items = new List<PlaylistTrack> {
|
||||
new PlaylistTrack {
|
||||
Track = new FullTrack {
|
||||
Album = new SimpleAlbum {
|
||||
Name = "Album",
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = null
|
||||
}
|
||||
}
|
||||
},
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = "TrackArtist"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Next = "DummyToMakeHasNextTrue"
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetPlaylistTracks(It.IsAny<SpotifyPlaylist>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>()))
|
||||
.Returns(playlistTracks);
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>()
|
||||
.Setup(x => x.GetNextPage(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<Paging<PlaylistTrack>>()))
|
||||
.Returns(default(Paging<PlaylistTrack>));
|
||||
|
||||
var result = Subject.Fetch(api, "playlistid");
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>()
|
||||
.Verify(x => x.GetNextPage(It.IsAny<SpotifyPlaylist>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<Paging<PlaylistTrack>>()),
|
||||
Times.Once());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,166 @@
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.ImportLists.Spotify;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
using SpotifyAPI.Web;
|
||||
using SpotifyAPI.Web.Models;
|
||||
|
||||
namespace NzbDrone.Core.Test.ImportListTests
|
||||
{
|
||||
[TestFixture]
|
||||
public class SpotifySavedAlbumsFixture : CoreTest<SpotifySavedAlbums>
|
||||
{
|
||||
// placeholder, we don't use real API
|
||||
private readonly SpotifyWebAPI api = null;
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_saved_albums_is_null()
|
||||
{
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetSavedAlbums(It.IsAny<SpotifySavedAlbums>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(default(Paging<SavedAlbum>));
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_saved_album_items_is_null()
|
||||
{
|
||||
var savedAlbums = new Paging<SavedAlbum> {
|
||||
Items = null
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetSavedAlbums(It.IsAny<SpotifySavedAlbums>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(savedAlbums);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_saved_album_is_null()
|
||||
{
|
||||
var savedAlbums = new Paging<SavedAlbum> {
|
||||
Items = new List<SavedAlbum> {
|
||||
null
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetSavedAlbums(It.IsAny<SpotifySavedAlbums>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(savedAlbums);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[TestCase("Artist", "Album")]
|
||||
public void should_parse_saved_album(string artistName, string albumName)
|
||||
{
|
||||
var savedAlbums = new Paging<SavedAlbum> {
|
||||
Items = new List<SavedAlbum> {
|
||||
new SavedAlbum {
|
||||
Album = new FullAlbum {
|
||||
Name = albumName,
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = artistName
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetSavedAlbums(It.IsAny<SpotifySavedAlbums>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(savedAlbums);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_if_get_next_page_returns_null()
|
||||
{
|
||||
var savedAlbums = new Paging<SavedAlbum> {
|
||||
Items = new List<SavedAlbum> {
|
||||
new SavedAlbum {
|
||||
Album = new FullAlbum {
|
||||
Name = "Album",
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = "Artist"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Next = "DummyToMakeHasNextTrue"
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetSavedAlbums(It.IsAny<SpotifySavedAlbums>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(savedAlbums);
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>()
|
||||
.Setup(x => x.GetNextPage(It.IsAny<SpotifyFollowedArtists>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<Paging<SavedAlbum>>()))
|
||||
.Returns(default(Paging<SavedAlbum>));
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().HaveCount(1);
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>()
|
||||
.Verify(x => x.GetNextPage(It.IsAny<SpotifySavedAlbums>(),
|
||||
It.IsAny<SpotifyWebAPI>(),
|
||||
It.IsAny<Paging<SavedAlbum>>()),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[TestCase(null, "Album")]
|
||||
[TestCase("Artist", null)]
|
||||
[TestCase(null, null)]
|
||||
public void should_skip_bad_artist_or_album_names(string artistName, string albumName)
|
||||
{
|
||||
var savedAlbums = new Paging<SavedAlbum> {
|
||||
Items = new List<SavedAlbum> {
|
||||
new SavedAlbum {
|
||||
Album = new FullAlbum {
|
||||
Name = albumName,
|
||||
Artists = new List<SimpleArtist> {
|
||||
new SimpleArtist {
|
||||
Name = artistName
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Mocker.GetMock<ISpotifyProxy>().
|
||||
Setup(x => x.GetSavedAlbums(It.IsAny<SpotifySavedAlbums>(),
|
||||
It.IsAny<SpotifyWebAPI>()))
|
||||
.Returns(savedAlbums);
|
||||
|
||||
var result = Subject.Fetch(api);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
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<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new();
|
||||
Paging<SimplePlaylist> GetUserPlaylists<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, string id)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new();
|
||||
FollowedArtists GetFollowedArtists<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new();
|
||||
Paging<SavedAlbum> GetSavedAlbums<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new();
|
||||
Paging<PlaylistTrack> GetPlaylistTracks<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, string id, string fields)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new();
|
||||
Paging<T> GetNextPage<T, TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, Paging<T> item)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new();
|
||||
CursorPaging<T> GetNextPage<T, TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, CursorPaging<T> item)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new();
|
||||
}
|
||||
|
||||
public class SpotifyProxy : ISpotifyProxy
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public SpotifyProxy(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public PrivateProfile GetPrivateProfile<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new()
|
||||
{
|
||||
return Execute(list, api, x => x.GetPrivateProfile());
|
||||
}
|
||||
|
||||
public Paging<SimplePlaylist> GetUserPlaylists<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, string id)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new()
|
||||
{
|
||||
return Execute(list, api, x => x.GetUserPlaylists(id));
|
||||
}
|
||||
|
||||
public FollowedArtists GetFollowedArtists<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new()
|
||||
{
|
||||
return Execute(list, api, x => x.GetFollowedArtists(FollowType.Artist));
|
||||
}
|
||||
|
||||
public Paging<SavedAlbum> GetSavedAlbums<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new()
|
||||
{
|
||||
return Execute(list, api, x => x.GetSavedAlbums(50));
|
||||
}
|
||||
|
||||
public Paging<PlaylistTrack> GetPlaylistTracks<TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, string id, string fields)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new()
|
||||
{
|
||||
return Execute(list, api, x => x.GetPlaylistTracks(id, fields: fields));
|
||||
}
|
||||
|
||||
public Paging<T> GetNextPage<T, TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, Paging<T> item)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new()
|
||||
{
|
||||
return Execute(list, api, (x) => x.GetNextPage(item));
|
||||
}
|
||||
|
||||
public CursorPaging<T> GetNextPage<T, TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, CursorPaging<T> item)
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new()
|
||||
{
|
||||
return Execute(list, api, (x) => x.GetNextPage(item));
|
||||
}
|
||||
|
||||
public T Execute<T, TSettings>(SpotifyImportListBase<TSettings> list, SpotifyWebAPI api, Func<SpotifyWebAPI, T> method, bool allowReauth = true)
|
||||
where T : BasicModel
|
||||
where TSettings : SpotifySettingsBase<TSettings>, new()
|
||||
{
|
||||
T 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue