Skip to content

Commit

Permalink
Handle no device responses
Browse files Browse the repository at this point in the history
Packing 3.5.0
  • Loading branch information
DanielLarsenNZ committed Aug 12, 2020
1 parent d71741b commit 5afd2bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
13 changes: 4 additions & 9 deletions src/SpotifyApi.NetCore.Tests/PlayerApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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);
}

}
Expand Down
6 changes: 6 additions & 0 deletions src/SpotifyApi.NetCore/PlayerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ public async Task<CurrentPlaybackContext> 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<CurrentEpisodePlaybackContext>();
Expand Down Expand Up @@ -715,6 +718,9 @@ public async Task<CurrentPlaybackContext> 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<CurrentEpisodePlaybackContext>();
Expand Down
2 changes: 1 addition & 1 deletion src/SpotifyApi.NetCore/SpotifyApi.NetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PackageId>SpotifyApi.NetCore</PackageId>
<Title>Spotify Web API .NET Core</Title>
<Description>Lightweight .NET Core client wrapper for the Spotify Web API</Description>
<Version>3.4.0</Version>
<Version>3.5.0</Version>
<Authors>Daniel Larsen and contributors</Authors>
<Company>Ringobot</Company>
<Copyright>Copyright 2020 Daniel Larsen and contributors</Copyright>
Expand Down
11 changes: 9 additions & 2 deletions src/SpotifyApi.NetCore/SpotifyWebApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ protected internal virtual Task<T> GetModelFromProperty<T>(
protected internal virtual async Task<T> GetModelFromProperty<T>(
Uri uri,
string rootPropertyName,
string accessToken = null)
=> (await GetJObject(uri, accessToken: accessToken))[rootPropertyName].ToObject<T>();
string accessToken = null)
{
var jObject = await GetJObject(uri, accessToken: accessToken);
if (jObject == null) return default;
return jObject[rootPropertyName].ToObject<T>();
}

/// <summary>
/// GET uri and deserialize as <see cref="JObject"/>
Expand All @@ -134,6 +138,9 @@ protected internal virtual async Task<JObject> 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))}");
Expand Down

0 comments on commit 5afd2bd

Please sign in to comment.