Skip to content

Commit

Permalink
Merge pull request #48 from jesseward/loved-tracks
Browse files Browse the repository at this point in the history
Swap Runtime.Serialization to Text.json.Serialization in model responses
  • Loading branch information
jesseward authored Jun 10, 2023
2 parents cef6398 + d0624a2 commit ab440c7
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 92 deletions.
20 changes: 13 additions & 7 deletions Jellyfin.Plugin.Lastfm/Api/BaseLastfmApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
using System.Linq;
using System.Net.Http;
using System.Text;
Expand All @@ -23,7 +24,6 @@ public class BaseLastfmApiClient
private readonly HttpClient _httpClient;
private readonly ILogger _logger;


public BaseLastfmApiClient(IHttpClientFactory httpClientFactory, ILogger logger)
{
_httpClientFactory = httpClientFactory;
Expand All @@ -45,14 +45,17 @@ public async Task<TResponse> Post<TRequest, TResponse>(TRequest request) where T
// Append the signature
Helpers.AppendSignature(ref data);


using var requestMessage = new HttpRequestMessage(HttpMethod.Post, BuildPostUrl(request.Secure));
requestMessage.Content = new StringContent(SetPostData(data), Encoding.UTF8, "application/x-www-form-urlencoded");
using (var response = await _httpClient.SendAsync(requestMessage, CancellationToken.None))
{
var serializeOptions = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
try
{
var result = await response.Content.ReadFromJsonAsync<TResponse>();
var result = await response.Content.ReadFromJsonAsync<TResponse>(serializeOptions);
// Lets Log the error here to ensure all errors are logged
if (result.IsError())
_logger.LogError(result.Message);
Expand All @@ -61,7 +64,7 @@ public async Task<TResponse> Post<TRequest, TResponse>(TRequest request) where T
}
catch (Exception e)
{
_logger.LogDebug(e.Message);
_logger.LogError(e.Message);
}
}

Expand All @@ -77,9 +80,13 @@ public async Task<TResponse> Get<TRequest, TResponse>(TRequest request, Cancella
{
using (var response = await _httpClient.GetAsync(BuildGetUrl(request.ToDictionary(), request.Secure), cancellationToken))
{
var serializeOptions = new JsonSerializerOptions
{
NumberHandling = JsonNumberHandling.AllowReadingFromString
};
try
{
var result = await response.Content.ReadFromJsonAsync<TResponse>();
var result = await response.Content.ReadFromJsonAsync<TResponse>(serializeOptions);

// Lets Log the error here to ensure all errors are logged
if (result.IsError())
Expand All @@ -89,9 +96,8 @@ public async Task<TResponse> Get<TRequest, TResponse>(TRequest request, Cancella
}
catch (Exception e)
{
_logger.LogDebug(e.Message);
_logger.LogError(e.Message);
}

return null;
}
}
Expand Down
22 changes: 13 additions & 9 deletions Jellyfin.Plugin.Lastfm/Configuration/configPage.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<!DOCTYPE html>
<html>

<head>
<title>Last.fm Scrobbler Configuration</title>
</head>

<body>
<div id="LastfmScrobblerConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-require="emby-input,emby-button,emby-select,emby-checkbox">
<div id="LastfmScrobblerConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage"
data-require="emby-input,emby-button,emby-select,emby-checkbox">

<div data-role="content">
<div class="content-primary">
Expand Down Expand Up @@ -114,13 +117,13 @@
this.$el.find('#optionScrobble').prop('checked', data.Options.Scrobble);
this.$el.find('#optionFavourite').prop('checked', data.Options.SyncFavourites);

//Dont forget to call checkboxradio('refresh') otherwise the UI wont update
// Don't forget to call checkboxradio('refresh') otherwise the UI wont update
},

save: function (username, password) {
var userConfig = this.getCurrentSelectedUser();

//If the conig for the user doesnt exist, create one
//If the config for the user doesn't exist, create one
if (!userConfig) {
userConfig = $.extend({}, this.configDefaults, { MediaBrowserUserId: this.getCurrentSelectedUserId() });

Expand All @@ -143,19 +146,19 @@
//Get session with user data
ApiClient.LastfmGetMobileSession(username, password).then($.proxy(function (data) {
//Check if we have data
if (data && data.Session) {
if (data && data.session) {

userConfig.Username = data.Session.Name;
userConfig.SessionKey = data.Session.Key;
userConfig.Username = data.session.name;
userConfig.SessionKey = data.session.key;

//Save
this.doSave();

return;
}

Dashboard.alert(data.Message || "Something went wrong");
console.log(data.Message);
Dashboard.alert(data.message || "Something went wrong");
console.log(data.message);
}, this));
Dashboard.hideLoadingMsg();
},
Expand Down Expand Up @@ -253,4 +256,5 @@
};</script>
</div>
</body>
</html>

</html>
8 changes: 2 additions & 6 deletions Jellyfin.Plugin.Lastfm/Jellyfin.Plugin.Lastfm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AssemblyVersion>8.0.0.2</AssemblyVersion>
<FileVersion>8.0.0.2</FileVersion>
<AssemblyVersion>8.0.0.3</AssemblyVersion>
<FileVersion>8.0.0.3</FileVersion>
</PropertyGroup>

<ItemGroup>
<None Remove="Configuration\configPage.html" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Configuration\configPage.html" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Jellyfin.Controller" Version="10.*-*" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
</ItemGroup>

</Project>
9 changes: 4 additions & 5 deletions Jellyfin.Plugin.Lastfm/Models/MobileSession.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
namespace Jellyfin.Plugin.Lastfm.Models
{
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

[DataContract]
public class MobileSession
{
[DataMember(Name = "name")]
[JsonPropertyName("name")]
public string Name { get; set; }

[DataMember(Name = "key")]
[JsonPropertyName("key")]
public string Key { get; set; }

[DataMember(Name = "subscriber")]
[JsonPropertyName("subscriber")]
public int Subscriber { get; set; }
}
}
7 changes: 3 additions & 4 deletions Jellyfin.Plugin.Lastfm/Models/Responses/BaseResponse.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace Jellyfin.Plugin.Lastfm.Models.Responses
{
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

[DataContract]
public class BaseResponse
{
[DataMember(Name="message")]
[JsonPropertyName("message")]
public string Message { get; set; }

[DataMember(Name="error")]
[JsonPropertyName("error")]
public int ErrorCode { get; set; }

public bool IsError()
Expand Down
17 changes: 7 additions & 10 deletions Jellyfin.Plugin.Lastfm/Models/Responses/GetTracksResponse.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace Jellyfin.Plugin.Lastfm.Models.Responses
{
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

[DataContract]
public class GetTracksResponse : BaseResponse
{
[DataMember(Name="tracks")]
[JsonPropertyName("tracks")]
public GetTracksTracks Tracks { get; set; }

public bool HasTracks()
Expand All @@ -15,26 +14,24 @@ public bool HasTracks()
}
}

[DataContract]
public class GetTracksTracks
{
[DataMember(Name="track")]
[JsonPropertyName("track")]
public List<LastfmTrack> Tracks { get; set; }

[DataMember(Name = "@attr")]
[JsonPropertyName("@attr")]
public GetTracksMeta Metadata { get; set; }
}

[DataContract]
public class GetTracksMeta
{
[DataMember(Name = "totalPages")]
[JsonPropertyName("totalPages")]
public int TotalPages { get; set; }

[DataMember(Name = "total")]
[JsonPropertyName("total")]
public int TotalTracks { get; set; }

[DataMember(Name = "page")]
[JsonPropertyName("page")]
public int Page { get; set; }

public bool IsLastPage()
Expand Down
17 changes: 7 additions & 10 deletions Jellyfin.Plugin.Lastfm/Models/Responses/LovedTracksResponse.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
namespace Jellyfin.Plugin.Lastfm.Models.Responses
{
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

[DataContract]
public class LovedTracksResponse : BaseResponse
{
[DataMember(Name = "lovedtracks")]
[JsonPropertyName("lovedtracks")]
public LovedTracks LovedTracks { get; set; }

public bool HasLovedTracks()
Expand All @@ -15,28 +14,26 @@ public bool HasLovedTracks()
}
}

[DataContract]
public class LovedTracks
{
[DataMember(Name = "track")]
[JsonPropertyName("track")]
public List<LastfmLovedTrack> Tracks { get; set; }


[DataMember(Name = "@attr")]
[JsonPropertyName("@attr")]
public LovedTracksMeta Metadata { get; set; }
}


[DataContract]
public class LovedTracksMeta
{
[DataMember(Name = "totalPages")]
[JsonPropertyName("totalPages")]
public int TotalPages { get; set; }

[DataMember(Name = "total")]
[JsonPropertyName("total")]
public int TotalTracks { get; set; }

[DataMember(Name = "page")]
[JsonPropertyName("page")]
public int Page { get; set; }

public bool IsLastPage()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Jellyfin.Plugin.Lastfm.Models.Responses
{
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

[DataContract]
public class MobileSessionResponse : BaseResponse
{
[DataMember(Name="session")]
[JsonPropertyName("session")]
public MobileSession Session { get; set; }
}
}
5 changes: 2 additions & 3 deletions Jellyfin.Plugin.Lastfm/Models/Responses/ScrobbleResponse.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Jellyfin.Plugin.Lastfm.Models.Responses
{
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

[DataContract]
public class ScrobbleResponse : BaseResponse
{
[DataMember(Name = "scrobbles")]
[JsonPropertyName("scrobbles")]
public Scrobbles Scrobbles { get; set; }
}
}
11 changes: 4 additions & 7 deletions Jellyfin.Plugin.Lastfm/Models/Scrobble.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
namespace Jellyfin.Plugin.Lastfm.Models
{
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

//Wow what a bad response object!
[DataContract]
public class Scrobbles
{
[DataMember(Name = "@attr")]
[JsonPropertyName("@attr")]
public ScrobbleAttributes Attributes { get; set; }
}

[DataContract]
public class ScrobbleAttributes
{
[DataMember(Name = "accepted")]
[JsonPropertyName("accepted")]
public bool Accepted { get; set; }

[DataMember(Name = "ignored")]
[JsonPropertyName("ignored")]
public bool Ignored { get; set; }
}
}
18 changes: 8 additions & 10 deletions Jellyfin.Plugin.Lastfm/Models/Tracks.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
namespace Jellyfin.Plugin.Lastfm.Models
{
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

[DataContract]
public class BaseLastfmTrack
{
[DataMember(Name="artist")]
[JsonPropertyName("artist")]
public LastfmArtist Artist { get; set; }

[DataMember(Name = "name")]
[JsonPropertyName("name")]
public string Name { get; set; }

[DataMember(Name = "mbid")]
[JsonPropertyName("mbid")]
public string MusicBrainzId { get; set; }
}

[DataContract]
public class LastfmArtist
{
[DataMember(Name="name")]
[JsonPropertyName("name")]
public string Name { get; set; }

[DataMember(Name = "mbid")]
[JsonPropertyName("mbid")]
public string MusicBrainzId { get; set; }
}

public class LastfmLovedTrack : BaseLastfmTrack
{
}

[DataContract]

public class LastfmTrack : BaseLastfmTrack
{
[DataMember(Name="playcount")]
[JsonPropertyName("playcount")]
public int PlayCount { get; set; }
}
}
Loading

0 comments on commit ab440c7

Please sign in to comment.