public async Task GetCurrentPlaybackInfo_NoToken_ThrowsInvalidOperationException() { // arrange var http = new HttpClient(); var player = new PlayerApi(http); // act await player.GetCurrentPlaybackInfo(); }
public async Task <CurrentPlaybackContext> GetCurrentlyPlayingSongAsync() { try { var http = new HttpClient(); IConfiguration config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var player = new PlayerApi(http, AuthToken); return(await player.GetCurrentPlaybackInfo(AuthToken)); } catch (Exception ex) { throw ex; } }
public async Task ReorderPlaylist(IPlaylist playlist, Room room, RoomSong song) { CurrentPlaybackContext currentPlayback = null; try { var http = new HttpClient(); IConfiguration config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var player = new PlayerApi(http, AuthToken); currentPlayback = await player.GetCurrentPlaybackInfo(AuthToken); } catch (Exception ex) { } try { var client = new RestClient(@"https://api.spotify.com/v1"); client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator($"Bearer {AuthToken}"); var request = new RestRequest($@"playlists/{playlist.PlaylistID}/tracks", Method.PUT); request.RequestFormat = DataFormat.Json; if (song != null) { room.RoomSongs.SingleOrDefault(s => s.SongId == song.SongId).SongRating -= 1; } var spotifyUris = room.RoomSongs .OrderByDescending(s => s.SongRating) .Select(s => s.Song.SpotifyId) .ToList(); if (currentPlayback != null && spotifyUris.Any(s => s == currentPlayback.Item.Uri)) { // Reorder songs, only after the current one we are playing. // This ensures that if you're in the middle of a playlist, // the next song you listen to is the highest rated song for (int i = 0; i < spotifyUris.Count; ++i) { if (spotifyUris[0] == currentPlayback.Item.Uri) { break; } spotifyUris.Remove(spotifyUris[0]); } } else { spotifyUris = room.RoomSongs .OrderByDescending(s => s.SongRating) .Select(s => s.Song.SpotifyId) .ToList(); } request.AddJsonBody(new { uris = spotifyUris }); var response = await client.ExecuteTaskAsync(request); var content = response.Content; } catch (Exception ex) { } }