From 5afd2bde837c65076e451180b3ede342f540fa17 Mon Sep 17 00:00:00 2001 From: Daniel Larsen Date: Wed, 12 Aug 2020 21:09:40 +1200 Subject: [PATCH] Handle no device responses Packing 3.5.0 --- src/SpotifyApi.NetCore.Tests/PlayerApiTests.cs | 13 ++++--------- src/SpotifyApi.NetCore/PlayerApi.cs | 6 ++++++ src/SpotifyApi.NetCore/SpotifyApi.NetCore.csproj | 2 +- src/SpotifyApi.NetCore/SpotifyWebApi.cs | 11 +++++++++-- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/SpotifyApi.NetCore.Tests/PlayerApiTests.cs b/src/SpotifyApi.NetCore.Tests/PlayerApiTests.cs index 138c536..23f7777 100644 --- a/src/SpotifyApi.NetCore.Tests/PlayerApiTests.cs +++ b/src/SpotifyApi.NetCore.Tests/PlayerApiTests.cs @@ -298,7 +298,7 @@ public async Task GetRecentlyPlayedTracks_GetNextPage_ItemsAny() [TestMethod] [TestCategory("Integration")] [TestCategory("User")] - public async Task GetCurrentlyPlayingTrack_UserAccessToken_NotNull() + public async Task GetCurrentlyPlayingTrack_UserAccessToken_NoException() { // arrange var http = new HttpClient(); @@ -309,9 +309,6 @@ public async Task GetCurrentlyPlayingTrack_UserAccessToken_NotNull() var context = await player.GetCurrentlyPlayingTrack( additionalTypes: new[] { "episode" }, accessToken: accessToken); - - // assert - Assert.IsNotNull(context); } [TestMethod] @@ -326,7 +323,7 @@ public async Task TransferPlayback_Device0Id_NoException() var devices = await player.GetDevices(accessToken: accessToken); // act - await player.TransferPlayback(devices[0].Id, accessToken: accessToken); + if (devices.Any()) await player.TransferPlayback(devices[0].Id, accessToken: accessToken); } [TestMethod] @@ -341,7 +338,7 @@ public async Task TransferPlayback_GetDevice1IdPlayTrue_NoException() var devices = await player.GetDevices(accessToken: accessToken); // act - await player.TransferPlayback(devices.Last().Id, play: true, accessToken: accessToken); + if (devices.Any()) await player.TransferPlayback(devices.Last().Id, play: true, accessToken: accessToken); } [TestMethod] @@ -500,13 +497,11 @@ public async Task GetDevices_AccessToken_ResultIsNotNull() [TestMethod] [TestCategory("Integration")] [TestCategory("User")] - public async Task GetCurrentPlaybackInfo_AccessToken_ResultIsNotNull() + public async Task GetCurrentPlaybackInfo_AccessToken_NoException() { // act var result = await new PlayerApi(new HttpClient()) .GetCurrentPlaybackInfo(accessToken: await TestsHelper.GetUserAccessToken()); - - Assert.IsNotNull(result); } } diff --git a/src/SpotifyApi.NetCore/PlayerApi.cs b/src/SpotifyApi.NetCore/PlayerApi.cs index 0048cda..f4e51a0 100644 --- a/src/SpotifyApi.NetCore/PlayerApi.cs +++ b/src/SpotifyApi.NetCore/PlayerApi.cs @@ -405,6 +405,9 @@ public async Task GetCurrentPlaybackInfo( builder.AppendToQueryAsCsv("additional_types", additionalTypes); var jObject = await GetJObject(builder.Uri, accessToken: accessToken); + // Todo #25 return 204 no content result + if (jObject == null) return null; + // Deserialize as Items of Track or Items of Episode if (jObject["currently_playing_type"].ToString() == "episode") return jObject.ToObject(); @@ -715,6 +718,9 @@ public async Task GetCurrentlyPlayingTrack( builder.AppendToQueryAsCsv("additional_types", additionalTypes); var jObject = await GetJObject(builder.Uri, accessToken: accessToken); + // Todo #25 return 204 no content result + if (jObject == null) return null; + // Deserialize as Items of Track or Items of Episode if (jObject["currently_playing_type"].ToString() == "episode") return jObject.ToObject(); diff --git a/src/SpotifyApi.NetCore/SpotifyApi.NetCore.csproj b/src/SpotifyApi.NetCore/SpotifyApi.NetCore.csproj index c39b404..fb7e6e3 100644 --- a/src/SpotifyApi.NetCore/SpotifyApi.NetCore.csproj +++ b/src/SpotifyApi.NetCore/SpotifyApi.NetCore.csproj @@ -4,7 +4,7 @@ SpotifyApi.NetCore Spotify Web API .NET Core Lightweight .NET Core client wrapper for the Spotify Web API - 3.4.0 + 3.5.0 Daniel Larsen and contributors Ringobot Copyright 2020 Daniel Larsen and contributors diff --git a/src/SpotifyApi.NetCore/SpotifyWebApi.cs b/src/SpotifyApi.NetCore/SpotifyWebApi.cs index d023611..a0b002b 100644 --- a/src/SpotifyApi.NetCore/SpotifyWebApi.cs +++ b/src/SpotifyApi.NetCore/SpotifyWebApi.cs @@ -120,8 +120,12 @@ protected internal virtual Task GetModelFromProperty( protected internal virtual async Task GetModelFromProperty( Uri uri, string rootPropertyName, - string accessToken = null) - => (await GetJObject(uri, accessToken: accessToken))[rootPropertyName].ToObject(); + string accessToken = null) + { + var jObject = await GetJObject(uri, accessToken: accessToken); + if (jObject == null) return default; + return jObject[rootPropertyName].ToObject(); + } /// /// GET uri and deserialize as @@ -134,6 +138,9 @@ protected internal virtual async Task GetJObject(Uri uri, string access new AuthenticationHeaderValue("Bearer", accessToken ?? (await GetAccessToken())) ); + // Todo #25 return 204 no content result + if (string.IsNullOrEmpty(json)) return null; + JObject deserialized = JsonConvert.DeserializeObject(json) as JObject; if (deserialized == null) throw new InvalidOperationException($"Failed to deserialize response as JSON. Response = {json.Substring(0, Math.Min(json.Length, 256))}");