Some smaller fixes and ignore case when parsing type of iplayableitem, fixes #604

This commit is contained in:
Jonas Dellinger 2021-05-01 17:11:24 +02:00
parent d72235e42b
commit 16a831ad40
6 changed files with 44 additions and 14 deletions

View File

@ -51,7 +51,7 @@ namespace Example.CLI.PersistentConfig
var json = await File.ReadAllTextAsync(CredentialsPath);
var token = JsonConvert.DeserializeObject<PKCETokenResponse>(json);
var authenticator = new PKCEAuthenticator(clientId!, token);
var authenticator = new PKCEAuthenticator(clientId!, token!);
authenticator.TokenRefreshed += (sender, token) => File.WriteAllText(CredentialsPath, JsonConvert.SerializeObject(token));
var config = SpotifyClientConfig.CreateDefault()

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NUnit.Framework;
@ -18,5 +20,23 @@ namespace SpotifyAPI.Web.Tests
var serialized = JsonConvert.SerializeObject(context);
});
}
[Test]
public async Task PlayableItemConverter_Reserialize()
{
// This has lowercase field names since it's a spotify response
var fixture = await File.ReadAllTextAsync(
Path.Join(TestContext.CurrentContext.TestDirectory, "Fixtures/full_playlist_response.json")
);
var fullPlaylist = JsonConvert.DeserializeObject<FullPlaylist>(fixture);
// This whill have uppercase field names since we use default JsonConvert settings
var serialized = JsonConvert.SerializeObject(fullPlaylist);
Assert.DoesNotThrow(() =>
{
var deserialized = JsonConvert.DeserializeObject<FullPlaylist>(serialized);
});
}
}
}

View File

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp2.2</TargetFrameworks>
<LangVersion>9.0</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
@ -18,4 +19,10 @@
<ProjectReference Include="..\SpotifyAPI.Web.Auth\SpotifyAPI.Web.Auth.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="Fixtures\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@ -46,9 +46,12 @@ namespace SpotifyAPI.Web
{
JObject bodyObject = JObject.Parse(body!);
var error = bodyObject.Value<JToken>("error");
if (error.Type == JTokenType.String)
if (error == null)
{
return null;
}
else if (error.Type == JTokenType.String)
{
return error.ToString();
}

View File

@ -15,24 +15,23 @@ namespace SpotifyAPI.Web
{
Ensure.ArgumentNotNull(serializer, nameof(serializer));
var token = JToken.ReadFrom(reader);
if (token.Type == JTokenType.Null)
if (JToken.ReadFrom(reader) is not JObject obj)
{
return null;
}
var type = token["type"]?.Value<string>();
if (type == "track")
var type = obj.GetValue("type", StringComparison.OrdinalIgnoreCase)?.Value<string>();
if (string.Equals(type, "track", StringComparison.OrdinalIgnoreCase))
{
var obj = new FullTrack();
serializer.Populate(token.CreateReader(), obj);
return obj;
var track = new FullTrack();
serializer.Populate(obj.CreateReader(), track);
return track;
}
else if (type == "episode")
else if (string.Equals(type, "episode", StringComparison.OrdinalIgnoreCase))
{
var obj = new FullEpisode();
serializer.Populate(token.CreateReader(), obj);
return obj;
var episode = new FullEpisode();
serializer.Populate(obj.CreateReader(), episode);
return episode;
}
else
{