From 870d9cdd5d4234670c78779f27c294a14fc77430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Gel=C3=B3czi?= Date: Sun, 19 Dec 2021 21:13:02 +0100 Subject: [PATCH] Playlist query can include song additional fields --- .../AudioStationConnector_Tests.cs | 32 ++++++---- .../AudioStationClient.Playlist.cs | 39 +++++++++--- .../AudioStationClient.Song.cs | 8 +-- SynologyDotNet.AudioStation/Model/Playlist.cs | 61 ++++++++++--------- .../Model/PlaylistList.cs | 6 +- .../Model/SongQueryAdditional.cs | 3 + 6 files changed, 94 insertions(+), 55 deletions(-) diff --git a/SynologyDotNet.AudioStation.IntegrationTest/AudioStationConnector_Tests.cs b/SynologyDotNet.AudioStation.IntegrationTest/AudioStationConnector_Tests.cs index 07878f3..13572aa 100644 --- a/SynologyDotNet.AudioStation.IntegrationTest/AudioStationConnector_Tests.cs +++ b/SynologyDotNet.AudioStation.IntegrationTest/AudioStationConnector_Tests.cs @@ -56,8 +56,8 @@ private static void UpdateTestPlaylist() var response = AudioStation.ListPlaylistsAsync(1000, 0).Result; Assert.IsTrue(response.Success); Assert.IsTrue(response.Data.Total > 0, $"Please create a playlist in Audio Station with this name: {Config.TestPlaylistName}"); - Assert.IsTrue(response.Data.playlists.Length > 0); - TestPlaylist = response.Data.playlists.First(x => x.name == Config.TestPlaylistName); + Assert.IsTrue(response.Data.Playlists.Length > 0); + TestPlaylist = response.Data.Playlists.First(x => x.Name == Config.TestPlaylistName); } [TestMethod] @@ -368,22 +368,32 @@ public async Task ListPlaylists() var response = await AudioStation.ListPlaylistsAsync(TestPageSize, 0); Assert.IsTrue(response.Success); Assert.IsTrue(response.Data.Total > 0); - Assert.IsTrue(response.Data.playlists.Length > 0); + Assert.IsTrue(response.Data.Playlists.Length > 0); } [TestMethod] public async Task GetPlaylist() { - var playlist = await AudioStation.GetPlaylistAsync(TestPageSize, 0, TestPlaylist.id); + var playlist = await AudioStation.GetPlaylistAsync(TestPageSize, 0, TestPlaylist.ID, SongQueryAdditional.None); Assert.IsTrue(playlist.Success); - Assert.IsFalse(string.IsNullOrEmpty(playlist.Data.id)); - Assert.IsTrue(playlist.Data.additional.songs.Length > 0); - foreach (var song in playlist.Data.additional.songs) + Assert.IsFalse(string.IsNullOrEmpty(playlist.Data.ID)); + Assert.IsTrue(playlist.Data.Additional.Songs.Length > 0); + foreach (var song in playlist.Data.Additional.Songs) { - Assert.IsFalse(string.IsNullOrEmpty(song.id)); - Assert.IsFalse(string.IsNullOrEmpty(song.title)); - Assert.IsFalse(string.IsNullOrEmpty(song.path)); - Assert.IsFalse(string.IsNullOrEmpty(song.type)); + AssertSong(song, SongQueryAdditional.None); + } + } + + [TestMethod] + public async Task GetPlaylist_SongDetails() + { + var playlist = await AudioStation.GetPlaylistAsync(TestPageSize, 0, TestPlaylist.ID, SongQueryAdditional.All); + Assert.IsTrue(playlist.Success); + Assert.IsFalse(string.IsNullOrEmpty(playlist.Data.ID)); + Assert.IsTrue(playlist.Data.Additional.Songs.Length > 0); + foreach (var song in playlist.Data.Additional.Songs) + { + AssertSong(song, SongQueryAdditional.All); } } diff --git a/SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs b/SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs index c3206da..d9aa31a 100644 --- a/SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs +++ b/SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs @@ -1,4 +1,6 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; +using System.Text; using System.Threading.Tasks; using SynologyDotNet.AudioStation.Model; using SynologyDotNet.Core.Responses; @@ -26,15 +28,36 @@ public async Task> ListPlaylistsAsync(int limit, /// The limit. /// The offset. /// The identifier. + /// Additional fields to load. /// - public async Task> GetPlaylistAsync(int limit, int offset, string id) + public async Task> GetPlaylistAsync(int limit, int offset, string id, SongQueryAdditional additionalFields) { - var result = await Client.QueryListAsync>(SYNO_AudioStation_Playlist, "getinfo", limit, offset, - GetLibraryArg(), - ("id", id), - ("additional", "songs") //("additional", "songs_song_tag,songs_song_audio,songs_song_rating,sharing_info") - ); - return new ApiDataResponse(result, result.Data?.playlists?.FirstOrDefault() ?? default); + var args = new List<(string, object)>(); + args.Add(GetLibraryArg()); + args.Add(("id", id)); + + var additionalFieldNames = new List(); + if (additionalFields != SongQueryAdditional.None) + { + additionalFieldNames.AddRange((new[] { + SongQueryAdditional.song_audio, + SongQueryAdditional.song_rating, + SongQueryAdditional.song_tag + }) + .Where(x => additionalFields.HasFlag(x)) + .Select(x => "songs_" + x.ToString())); + } + else + { + additionalFieldNames.Add("songs"); + } + args.Add(("additional", string.Join(",", additionalFieldNames))); + + var test = await Client.QueryByteArrayAsync(SYNO_AudioStation_Playlist, "getinfo", args.ToArray()); + string json = Encoding.UTF8.GetString(test.Data); + + var playlists = await Client.QueryListAsync>(SYNO_AudioStation_Playlist, "getinfo", limit, offset, args.ToArray()); + return new ApiDataResponse(playlists, playlists.Data?.Playlists?.FirstOrDefault() ?? default); } } } diff --git a/SynologyDotNet.AudioStation/AudioStationClient.Song.cs b/SynologyDotNet.AudioStation/AudioStationClient.Song.cs index 20aa039..e7dd04f 100644 --- a/SynologyDotNet.AudioStation/AudioStationClient.Song.cs +++ b/SynologyDotNet.AudioStation/AudioStationClient.Song.cs @@ -18,21 +18,21 @@ public partial class AudioStationClient /// /// Maximum number of items to return /// Start position in the list (use it for paging) - /// Additional filds to load + /// Additional fields to load /// Filter parameters /// - public async Task> ListSongsAsync(int limit, int offset, SongQueryAdditional additional, params (SongQueryParameter, object)[] queryParameters) + public async Task> ListSongsAsync(int limit, int offset, SongQueryAdditional additionalFields, params (SongQueryParameter, object)[] queryParameters) { var args = new List<(string, object)>(queryParameters.Select(f => (f.Item1.ToString(), f.Item2))); args.Add(GetLibraryArg()); - if (additional != SongQueryAdditional.None) + if (additionalFields != SongQueryAdditional.None) { args.Add(("additional", string.Join(",", (new[] { SongQueryAdditional.song_audio, SongQueryAdditional.song_rating, SongQueryAdditional.song_tag }) - .Where(x => additional.HasFlag(x)) + .Where(x => additionalFields.HasFlag(x)) .Select(x => x.ToString())))); } var result = await Client.QueryListAsync>(SYNO_AudioStation_Song, "list", limit, offset, args.ToArray()); diff --git a/SynologyDotNet.AudioStation/Model/Playlist.cs b/SynologyDotNet.AudioStation/Model/Playlist.cs index 3fe0412..d32101c 100644 --- a/SynologyDotNet.AudioStation/Model/Playlist.cs +++ b/SynologyDotNet.AudioStation/Model/Playlist.cs @@ -1,4 +1,6 @@ -namespace SynologyDotNet.AudioStation.Model +using Newtonsoft.Json; + +namespace SynologyDotNet.AudioStation.Model { public class Playlist { @@ -7,40 +9,39 @@ public static class ReservedNames public const string SharedSongs = "__SYNO_AUDIO_SHARED_SONGS__"; } - public string id { get; set; } - public string library { get; set; } - public string name { get; set; } - public string path { get; set; } - public string sharing_status { get; set; } - public string type { get; set; } - public PlaylistAdditional additional { get; set; } + [JsonProperty("id")] + public string ID { get; set; } + + [JsonProperty("library")] + public string Library { get; set; } + + [JsonProperty("name")] + public string Name { get; set; } + + [JsonProperty("path")] + public string Path { get; set; } + + [JsonProperty("sharing_status")] + public string SharingStatus { get; set; } - public override string ToString() => id ?? base.ToString(); + [JsonProperty("type")] + public string Type { get; set; } + + [JsonProperty("additional")] + public PlaylistAdditional Additional { get; set; } + + public override string ToString() => ID ?? base.ToString(); } public struct PlaylistAdditional { - public PlaylistSong[] songs { get; set; } - public int songs_offset { get; set; } - public int songs_total { get; set; } - //public PlaylistSharingInfo sharing_info { get; set; } - } + [JsonProperty("songs")] + public Song[] Songs { get; set; } - //public struct PlaylistSharingInfo - //{ - // public string date_available { get; set; } - // public string date_expired { get; set; } - // public string id { get; set; } - // public string status { get; set; } - // public string url { get; set; } - //} - - public struct PlaylistSong - { - public string id { get; set; } - public string path { get; set; } - public string title { get; set; } - public string type { get; set; } - public override string ToString() => title ?? base.ToString(); + [JsonProperty("songs_offset")] + public int Offset { get; set; } + + [JsonProperty("songs_total")] + public int Total { get; set; } } } diff --git a/SynologyDotNet.AudioStation/Model/PlaylistList.cs b/SynologyDotNet.AudioStation/Model/PlaylistList.cs index 6c04646..2638b4e 100644 --- a/SynologyDotNet.AudioStation/Model/PlaylistList.cs +++ b/SynologyDotNet.AudioStation/Model/PlaylistList.cs @@ -1,9 +1,11 @@ -using SynologyDotNet.Core.Responses; +using Newtonsoft.Json; +using SynologyDotNet.Core.Responses; namespace SynologyDotNet.AudioStation.Model { public class PlaylistList : ListResponseBase { - public Playlist[] playlists { get; set; } + [JsonProperty("playlists")] + public Playlist[] Playlists { get; set; } } } diff --git a/SynologyDotNet.AudioStation/Model/SongQueryAdditional.cs b/SynologyDotNet.AudioStation/Model/SongQueryAdditional.cs index 9758733..ce6ece0 100644 --- a/SynologyDotNet.AudioStation/Model/SongQueryAdditional.cs +++ b/SynologyDotNet.AudioStation/Model/SongQueryAdditional.cs @@ -6,9 +6,12 @@ namespace SynologyDotNet.AudioStation.Model public enum SongQueryAdditional : short { None = 0, + + // Do not rename! song_tag = 1, song_audio = 2, song_rating = 4, + All = ~None } }