Example #1
0
        public async Task BasicUsage()
        {
            // Arrange
            const string username = "******";

            var mockHttp = new Mock <IHttpClient>();

            mockHttp.Setup(h => h.Get(It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync("{\"Id\":\"def456\",\"Name\":\"ghi789\"}");

            var mockAuth = new Mock <IAuthorizationApi>();

            var playlists = new PlaylistsApi(mockHttp.Object, mockAuth.Object);

            // Act
            var result1 = await playlists.GetPlaylists(username);

            Trace.WriteLine((object)result1);
            var result2 = await playlists.GetPlaylists <dynamic>(username);

            Trace.WriteLine((object)result2);
            var result3 = await playlists.GetPlaylists <object>(username);

            Trace.WriteLine(result3);
            var result4 = await playlists.GetPlaylists <TestPlaylistsModel>(username);

            Trace.WriteLine(result4);
        }
        public void Initialize()
        {
            var            http       = new HttpClient();
            IConfiguration testConfig = TestsHelper.GetLocalConfig();

            api      = new PlaylistsApi(http);
            usersApi = new UsersProfileApi(http);
        }
Example #3
0
        static async Task <string> Thing()
        {
            var http     = new RestHttpClient(new System.Net.Http.HttpClient());
            var memCache = new RuntimeMemoryCache(System.Runtime.Caching.MemoryCache.Default);
            var clientCredentialsAuthApi = new ClientCredentialsAuthorizationApi(http, System.Configuration.ConfigurationManager.AppSettings, memCache);
            var api = new PlaylistsApi(http, clientCredentialsAuthApi);

            var playlists = await api.GetPlaylists("davemateer");

            Trace.TraceInformation(playlists.ToString());
            return("xx");
        }
        public async Task GetTracks_NewAccountsService_ReturnsTracks()
        {
            // Testing @DanixPC's use case
            var http     = new HttpClient();
            var accounts = new AccountsService(http, TestsHelper.GetLocalConfig());
            var plapi    = new PlaylistsApi(http, accounts);
            var pl       = await plapi.GetTracks("4h4urfIy5cyCdFOc1Ff4iN");

            var pl_tr = pl.Items.ElementAt(0).Track.Name;

            var pl_page_2 = await plapi.GetTracks("4h4urfIy5cyCdFOc1Ff4iN", offset : 100);

            var pl_page_2_tr = pl_page_2.Items.ElementAt(0).Track.Name;
        }
        public async Task SearchPlaylists_PlaylistName_AnyItems()
        {
            // arrange
            const string query = "dance";

            var http     = new HttpClient();
            var accounts = new AccountsService(http, TestsHelper.GetLocalConfig());

            var api = new PlaylistsApi(http, accounts);

            // act
            var response = await api.SearchPlaylists(query);

            // assert
            Assert.IsTrue(response.Items.Any());
        }
        public async Task BasicUsage()
        {
            // Arrange
            const string username = "******";

            var mockHttp = new MockHttpClient();

            mockHttp.SetupSendAsync("{\"Id\":\"def456\",\"Name\":\"ghi789\"}");
            var accounts  = new MockAccountsService().Object;
            var playlists = new PlaylistsApi(mockHttp.HttpClient, accounts);

            // Act
            var result1 = await playlists.GetPlaylists(username);

            Trace.WriteLine(result1);
        }
        public async Task GetTracks_UsernameAndPlaylistId_GetAccessTokenCalled()
        {
            // Arrange
            const string playlistId = "jkl012";

            var mockHttp = new MockHttpClient();

            mockHttp.SetupSendAsync("{\"Id\":\"def456\",\"Name\":\"ghi789\"}");
            var mockAccounts = new MockAccountsService();

            var api = new PlaylistsApi(mockHttp.HttpClient, mockAccounts.Object);

            // Act
            await api.GetTracks(playlistId);

            // Assert
            mockAccounts.Verify(a => a.GetAccessToken());
        }
Example #8
0
        public async Task GetPlaylists_Username_GetAccessTokenCalled()
        {
            // Arrange
            const string username = "******";

            var mockHttp = new MockHttpClient();

            mockHttp.SetupSendAsync("{\"Id\":\"def456\",\"Name\":\"ghi789\"}");
            var mockAccounts = new MockAccountsService();

            var api = new PlaylistsApi(mockHttp.HttpClient, mockAccounts.Object);

            // Act
            await api.GetPlaylists(username);

            // Assert
            mockAccounts.Verify(a => a.GetAppAccessToken());
        }
Example #9
0
        public async Task <PlaylistModel> GetPlaylist(string id)
        {
            try
            {
                var http           = new HttpClient();
                var accounts       = new AccountsService(http, _configuration);
                var playlistsApi   = new PlaylistsApi(http, accounts);
                var tracksApi      = new TracksApi(http, accounts);
                int limit          = 100;
                var playlistTracks = await playlistsApi.GetTracks(id, limit : limit);

                int offset = 0;
                int j      = 0;
                // Go through all pages since Spotify only returns 100 tracks at once for a playlist
                var trackModels = new List <TrackModel>();
                while (playlistTracks.Items.Any())
                {
                    var tracksWithAudioFeatures = await tracksApi.GetTracksAudioFeatures(playlistTracks.Items.Select(x => x.Track.Id).ToArray());

                    trackModels.AddRange(tracksWithAudioFeatures.Select(x => new TrackModel
                    {
                        ExternalId   = x.Id,
                        Danceability = x.Danceability,
                        Energy       = x.Energy,
                        Valence      = x.Valence,
                        Popularity   = playlistTracks.Items.FirstOrDefault(y => y.Track.Id == x.Id)?.Track.Popularity ?? 0,
                        Artists      = playlistTracks.Items.FirstOrDefault(y => y.Track.Id == x.Id)?.Track.Artists.FirstOrDefault()?.Name ?? string.Empty,
                        Name         = playlistTracks.Items.FirstOrDefault(y => y.Track.Id == x.Id)?.Track.Name ?? string.Empty,
                        PreviewUrl   = playlistTracks.Items.FirstOrDefault(y => y.Track.Id == x.Id)?.Track.PreviewUrl ?? string.Empty,
                    }));
                    offset        += limit;
                    playlistTracks = await playlistsApi.GetTracks(id, limit : limit, offset : offset);
                }

                var newPlaylist = new PlaylistModel(id, trackModels);
                return(newPlaylist);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.ToString());
                throw new Exception("Error getting playlist from Spotify");
            }
        }
        public async Task GetTracks_NewEndpoint_ReturnsValidPlaylistTracks()
        {
            // Arrange
            const string playlistId = "37i9dQZF1DX3WvGXE8FqYX";

            var http     = new HttpClient();
            var accounts = new AccountsService(http, TestsHelper.GetLocalConfig());

            var api = new PlaylistsApi(http, accounts);

            // Act
            var response = await api.GetTracks(playlistId);

            // Assert
            Assert.IsNotNull(response.Items);
            Assert.IsTrue(response.Items.Length > 0);
            Assert.IsTrue(response.Items[0].Track.Name.Length > 0);
            Assert.IsTrue(response.Items[0].Track.Album.Name.Length > 0);
            Assert.IsTrue(response.Items[0].Track.Artists.Length > 0);
        }
Example #11
0
        public async Task GetPlaylists_Username_GetAccessTokenCalled()
        {
            // Arrange
            const string username = "******";

            var mockHttp = new Mock <IHttpClient>();

            mockHttp.Setup(h => h.Get(It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync("{\"Id\":\"def456\",\"Name\":\"ghi789\"}");

            var mockAuth = new Mock <IAuthorizationApi>();

            var api = new PlaylistsApi(mockHttp.Object, mockAuth.Object);

            // Act
            await api.GetPlaylists(username);

            // Assert
            mockAuth.Verify(a => a.GetAccessToken());
        }
Example #12
0
        public async Task GetTracks_UsernameAndPlaylistId_HttpGetCalled()
        {
            // Arrange
            const string username   = "******";
            const string playlistId = "jkl012";

            var mockHttp = new Mock <IHttpClient>();

            mockHttp.Setup(h => h.Get(It.IsAny <string>(), It.IsAny <AuthenticationHeaderValue>()))
            .ReturnsAsync("{\"Id\":\"def456\",\"Name\":\"ghi789\"}");

            var mockAuth = new Mock <IAuthorizationApi>();

            var api = new PlaylistsApi(mockHttp.Object, mockAuth.Object);

            // Act
            await api.GetTracks(username, playlistId);

            // Assert
            mockHttp.Verify(h => h.Get(PlaylistsApi.BaseUrl + "/users/abc123/playlists/jkl012/tracks", It.IsAny <AuthenticationHeaderValue>()));
        }