Skip to content

Commit

Permalink
Playlist query can include song additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
geloczi committed Dec 19, 2021
1 parent 7102b61 commit 870d9cd
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);
}
}

Expand Down
39 changes: 31 additions & 8 deletions SynologyDotNet.AudioStation/AudioStationClient.Playlist.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -26,15 +28,36 @@ public async Task<ApiListRessponse<PlaylistList>> ListPlaylistsAsync(int limit,
/// <param name="limit">The limit.</param>
/// <param name="offset">The offset.</param>
/// <param name="id">The identifier.</param>
/// <param name="additionalFields">Additional fields to load.</param>
/// <returns></returns>
public async Task<ApiDataResponse<Playlist>> GetPlaylistAsync(int limit, int offset, string id)
public async Task<ApiDataResponse<Playlist>> GetPlaylistAsync(int limit, int offset, string id, SongQueryAdditional additionalFields)
{
var result = await Client.QueryListAsync<ApiListRessponse<PlaylistList>>(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<Playlist>(result, result.Data?.playlists?.FirstOrDefault() ?? default);
var args = new List<(string, object)>();
args.Add(GetLibraryArg());
args.Add(("id", id));

var additionalFieldNames = new List<string>();
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<ApiListRessponse<PlaylistList>>(SYNO_AudioStation_Playlist, "getinfo", limit, offset, args.ToArray());
return new ApiDataResponse<Playlist>(playlists, playlists.Data?.Playlists?.FirstOrDefault() ?? default);
}
}
}
8 changes: 4 additions & 4 deletions SynologyDotNet.AudioStation/AudioStationClient.Song.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ public partial class AudioStationClient
/// </summary>
/// <param name="limit">Maximum number of items to return</param>
/// <param name="offset">Start position in the list (use it for paging)</param>
/// <param name="additional">Additional filds to load</param>
/// <param name="additionalFields">Additional fields to load</param>
/// <param name="queryParameters">Filter parameters</param>
/// <returns></returns>
public async Task<ApiListRessponse<SongList>> ListSongsAsync(int limit, int offset, SongQueryAdditional additional, params (SongQueryParameter, object)[] queryParameters)
public async Task<ApiListRessponse<SongList>> 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<ApiListRessponse<SongList>>(SYNO_AudioStation_Song, "list", limit, offset, args.ToArray());
Expand Down
61 changes: 31 additions & 30 deletions SynologyDotNet.AudioStation/Model/Playlist.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace SynologyDotNet.AudioStation.Model
using Newtonsoft.Json;

namespace SynologyDotNet.AudioStation.Model
{
public class Playlist
{
Expand All @@ -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; }
}
}
6 changes: 4 additions & 2 deletions SynologyDotNet.AudioStation/Model/PlaylistList.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
3 changes: 3 additions & 0 deletions SynologyDotNet.AudioStation/Model/SongQueryAdditional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 870d9cd

Please sign in to comment.