From fcd76c421bf088cf6f99da2fb9a0a33f27521923 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Thu, 25 May 2023 18:57:42 +0300 Subject: [PATCH 1/5] Move ctors with HttpClient to factory methods --- .../YandexMusicSearchResultLoaderTest.cs | 2 +- .../YandexMusicTrackLoaderTest.cs | 4 ++-- .../Loaders/YandexMusicDirectUrlLoader.cs | 12 +++++++----- .../Loaders/YandexMusicPlaylistLoader.cs | 13 +++++++------ .../Loaders/YandexMusicSearchResultLoader.cs | 13 +++++++------ .../Loaders/YandexMusicTrackLoader.cs | 16 +++++++++------- YandexMusicResolver/YandexMusicMainResolver.cs | 8 ++++---- YandexMusicResolver/YandexMusicResolver.csproj | 4 ++-- 8 files changed, 39 insertions(+), 33 deletions(-) diff --git a/YandexMusicResolver.Tests/YandexMusicSearchResultLoaderTest.cs b/YandexMusicResolver.Tests/YandexMusicSearchResultLoaderTest.cs index a18a82f..289ae87 100644 --- a/YandexMusicResolver.Tests/YandexMusicSearchResultLoaderTest.cs +++ b/YandexMusicResolver.Tests/YandexMusicSearchResultLoaderTest.cs @@ -42,7 +42,7 @@ public void DoPlaylistSearch() { [InlineData("myprefix")] public void TestPrefixes(string? prefix) { #pragma warning disable 8625 - var yandexMusicSearchResultLoader = new YandexMusicSearchResultLoader(YandexCredentialsProviderMock.Object, new HttpClient(), AutoMocker.Get()); + var yandexMusicSearchResultLoader = YandexMusicSearchResultLoader.CreateWithHttpClient(YandexCredentialsProviderMock.Object, new HttpClient(), AutoMocker.Get()); yandexMusicSearchResultLoader.SetSearchPrefix(prefix); #pragma warning restore 8625 prefix ??= "ymsearch"; diff --git a/YandexMusicResolver.Tests/YandexMusicTrackLoaderTest.cs b/YandexMusicResolver.Tests/YandexMusicTrackLoaderTest.cs index ae32321..1451931 100644 --- a/YandexMusicResolver.Tests/YandexMusicTrackLoaderTest.cs +++ b/YandexMusicResolver.Tests/YandexMusicTrackLoaderTest.cs @@ -9,7 +9,7 @@ public class YandexMusicTrackLoaderTest : YandexTestBase { [InlineData(9425747, 55561798)] [InlineData(12033669, 70937156)] public void GetTrackInfo(long albumId, long trackId) { - var yandexMusicTrackLoader = new YandexMusicTrackLoader(YandexCredentialsProviderMock.Object, new HttpClient()); + var yandexMusicTrackLoader = YandexMusicTrackLoader.CreateWithHttpClient(YandexCredentialsProviderMock.Object, new HttpClient()); var trackInfo = yandexMusicTrackLoader.LoadTrack(trackId).GetAwaiter().GetResult(); Assert.NotNull(trackInfo); Assert.Equal($"https://music.yandex.ru/album/{albumId}/track/{trackId}", trackInfo.Uri); @@ -17,7 +17,7 @@ public void GetTrackInfo(long albumId, long trackId) { [Fact] public void GetTracksInfo() { - var yandexMusicTrackLoader = new YandexMusicTrackLoader(YandexCredentialsProviderMock.Object, new HttpClient()); + var yandexMusicTrackLoader = YandexMusicTrackLoader.CreateWithHttpClient(YandexCredentialsProviderMock.Object, new HttpClient()); var trackIds = new List() { 55561798, 70937156 }; var trackInfo = yandexMusicTrackLoader.LoadTracks(trackIds).GetAwaiter().GetResult(); Assert.NotNull(trackInfo); diff --git a/YandexMusicResolver/Loaders/YandexMusicDirectUrlLoader.cs b/YandexMusicResolver/Loaders/YandexMusicDirectUrlLoader.cs index fce2a46..ae1a8ba 100644 --- a/YandexMusicResolver/Loaders/YandexMusicDirectUrlLoader.cs +++ b/YandexMusicResolver/Loaders/YandexMusicDirectUrlLoader.cs @@ -22,8 +22,11 @@ public class YandexMusicDirectUrlLoader : IYandexMusicDirectUrlLoader { /// /// Config instance for performing requests /// Factory for resolving HttpClient. Client name is - public YandexMusicDirectUrlLoader(IYandexCredentialsProvider credentialsProvider, IHttpClientFactory httpClientFactory) { - _httpClient = httpClientFactory.GetYMusicHttpClient(); + public YandexMusicDirectUrlLoader(IYandexCredentialsProvider credentialsProvider, IHttpClientFactory httpClientFactory) + : this(credentialsProvider, httpClientFactory.GetYMusicHttpClient()) { } + + private YandexMusicDirectUrlLoader(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient) { + _httpClient = httpClient; _credentialsProvider = credentialsProvider; } @@ -32,9 +35,8 @@ public YandexMusicDirectUrlLoader(IYandexCredentialsProvider credentialsProvider /// /// Config instance for performing requests /// HttpClient for performing requests. But preferred way is use another ctor and pass - public YandexMusicDirectUrlLoader(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient) { - _httpClient = httpClient; - _credentialsProvider = credentialsProvider; + public static YandexMusicDirectUrlLoader CreateWithHttpClient(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient) { + return new YandexMusicDirectUrlLoader(credentialsProvider, httpClient); } /// diff --git a/YandexMusicResolver/Loaders/YandexMusicPlaylistLoader.cs b/YandexMusicResolver/Loaders/YandexMusicPlaylistLoader.cs index ce5f270..35c763a 100644 --- a/YandexMusicResolver/Loaders/YandexMusicPlaylistLoader.cs +++ b/YandexMusicResolver/Loaders/YandexMusicPlaylistLoader.cs @@ -21,8 +21,11 @@ public class YandexMusicPlaylistLoader : IYandexMusicPlaylistLoader { /// Config instance for performing requests /// Factory for resolving HttpClient. Client name is /// Instance of for resolving some strange playlists - public YandexMusicPlaylistLoader(IYandexCredentialsProvider yandexCredentialsProvider, IHttpClientFactory httpClientFactory, IYandexMusicTrackLoader? trackLoader = null) { - _httpClient = httpClientFactory.GetYMusicHttpClient(); + public YandexMusicPlaylistLoader(IYandexCredentialsProvider yandexCredentialsProvider, IHttpClientFactory httpClientFactory, IYandexMusicTrackLoader? trackLoader = null) + : this(yandexCredentialsProvider, httpClientFactory.GetYMusicHttpClient(), trackLoader) { } + + private YandexMusicPlaylistLoader(IYandexCredentialsProvider yandexCredentialsProvider, HttpClient httpClient, IYandexMusicTrackLoader? trackLoader = null) { + _httpClient = httpClient; _trackLoader = trackLoader; _yandexCredentialsProvider = yandexCredentialsProvider; } @@ -33,10 +36,8 @@ public YandexMusicPlaylistLoader(IYandexCredentialsProvider yandexCredentialsPro /// Config instance for performing requests /// HttpClient for performing requests. But preferred way is use another ctor and pass /// Instance of for resolving some strange playlists - public YandexMusicPlaylistLoader(IYandexCredentialsProvider yandexCredentialsProvider, HttpClient httpClient, IYandexMusicTrackLoader? trackLoader = null) { - _httpClient = httpClient; - _trackLoader = trackLoader; - _yandexCredentialsProvider = yandexCredentialsProvider; + public static YandexMusicPlaylistLoader CreateWithHttpClient(IYandexCredentialsProvider yandexCredentialsProvider, HttpClient httpClient, IYandexMusicTrackLoader? trackLoader = null) { + return new YandexMusicPlaylistLoader(yandexCredentialsProvider, httpClient, trackLoader); } /// diff --git a/YandexMusicResolver/Loaders/YandexMusicSearchResultLoader.cs b/YandexMusicResolver/Loaders/YandexMusicSearchResultLoader.cs index d555b60..d7a64e1 100644 --- a/YandexMusicResolver/Loaders/YandexMusicSearchResultLoader.cs +++ b/YandexMusicResolver/Loaders/YandexMusicSearchResultLoader.cs @@ -31,8 +31,11 @@ public class YandexMusicSearchResultLoader : IYandexMusicSearchResultLoader { /// Config instance for performing requests /// Factory for resolving HttpClient. Client name is /// Playlist loader instance for resolving albums and playlists - public YandexMusicSearchResultLoader(IYandexCredentialsProvider credentialsProvider, IHttpClientFactory httpClientFactory, IYandexMusicPlaylistLoader playlistLoader) { - _httpClient = httpClientFactory.GetYMusicHttpClient(); + public YandexMusicSearchResultLoader(IYandexCredentialsProvider credentialsProvider, IHttpClientFactory httpClientFactory, IYandexMusicPlaylistLoader playlistLoader) + : this(credentialsProvider, httpClientFactory.GetYMusicHttpClient(), playlistLoader) { } + + private YandexMusicSearchResultLoader(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient, IYandexMusicPlaylistLoader playlistLoader) { + _httpClient = httpClient; _playlistLoader = playlistLoader; _credentialsProvider = credentialsProvider; } @@ -43,10 +46,8 @@ public YandexMusicSearchResultLoader(IYandexCredentialsProvider credentialsProvi /// Config instance for performing requests /// HttpClient for performing requests. But preferred way is use another ctor and pass /// Playlist loader instance for resolving albums and playlists - public YandexMusicSearchResultLoader(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient, IYandexMusicPlaylistLoader playlistLoader) { - _httpClient = httpClient; - _playlistLoader = playlistLoader; - _credentialsProvider = credentialsProvider; + public static YandexMusicSearchResultLoader CreateWithHttpClient(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient, IYandexMusicPlaylistLoader playlistLoader) { + return new YandexMusicSearchResultLoader(credentialsProvider, httpClient, playlistLoader); } /// diff --git a/YandexMusicResolver/Loaders/YandexMusicTrackLoader.cs b/YandexMusicResolver/Loaders/YandexMusicTrackLoader.cs index ae1e781..7d6f2ba 100644 --- a/YandexMusicResolver/Loaders/YandexMusicTrackLoader.cs +++ b/YandexMusicResolver/Loaders/YandexMusicTrackLoader.cs @@ -22,19 +22,21 @@ public class YandexMusicTrackLoader : IYandexMusicTrackLoader { /// /// Config instance for performing requests /// Factory for resolving HttpClient. Client name is - public YandexMusicTrackLoader(IYandexCredentialsProvider credentialsProvider, IHttpClientFactory httpClientFactory) { + public YandexMusicTrackLoader(IYandexCredentialsProvider credentialsProvider, IHttpClientFactory httpClientFactory) + : this(credentialsProvider, httpClientFactory.GetYMusicHttpClient()) { } + + private YandexMusicTrackLoader(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient) { _credentialsProvider = credentialsProvider; - _httpClient = httpClientFactory.GetYMusicHttpClient(); - } - + _httpClient = httpClient; + } + /// /// Initializes a new instance of the class. /// /// Config instance for performing requests /// HttpClient for performing requests. But preferred way is use another ctor and pass - public YandexMusicTrackLoader(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient) { - _credentialsProvider = credentialsProvider; - _httpClient = httpClient; + public static YandexMusicTrackLoader CreateWithHttpClient(IYandexCredentialsProvider credentialsProvider, HttpClient httpClient) { + return new YandexMusicTrackLoader(credentialsProvider, httpClient); } /// diff --git a/YandexMusicResolver/YandexMusicMainResolver.cs b/YandexMusicResolver/YandexMusicMainResolver.cs index cf31507..4af9ae5 100644 --- a/YandexMusicResolver/YandexMusicMainResolver.cs +++ b/YandexMusicResolver/YandexMusicMainResolver.cs @@ -54,10 +54,10 @@ public YandexMusicMainResolver(IYandexCredentialsProvider credentialsProvider, IYandexMusicTrackLoader? trackLoader = null, IYandexMusicDirectUrlLoader? directUrlLoader = null, IYandexMusicSearchResultLoader? searchResultLoader = null) { - TrackLoader = trackLoader ??= new YandexMusicTrackLoader(credentialsProvider, client); - DirectUrlLoader = directUrlLoader ??= new YandexMusicDirectUrlLoader(credentialsProvider, client); - PlaylistLoader = playlistLoader ??= new YandexMusicPlaylistLoader(credentialsProvider, client, trackLoader); - SearchResultLoader = searchResultLoader ??= new YandexMusicSearchResultLoader(credentialsProvider, client, playlistLoader); + TrackLoader = trackLoader ??= YandexMusicTrackLoader.CreateWithHttpClient(credentialsProvider, client); + DirectUrlLoader = directUrlLoader ??= YandexMusicDirectUrlLoader.CreateWithHttpClient(credentialsProvider, client); + PlaylistLoader = playlistLoader ??= YandexMusicPlaylistLoader.CreateWithHttpClient(credentialsProvider, client, trackLoader); + SearchResultLoader = searchResultLoader ??= YandexMusicSearchResultLoader.CreateWithHttpClient(credentialsProvider, client, playlistLoader); } /// diff --git a/YandexMusicResolver/YandexMusicResolver.csproj b/YandexMusicResolver/YandexMusicResolver.csproj index 6355289..cc9e04f 100644 --- a/YandexMusicResolver/YandexMusicResolver.csproj +++ b/YandexMusicResolver/YandexMusicResolver.csproj @@ -12,10 +12,10 @@ A library aimed at searching, resolving and getting direct links to tracks, playlists or albums in Yandex.Music. Can work without authorization. Git https://github.com/SKProCH/YandexMusicResolver - 5.1.0 + 6.0.0-preview1 MIT -- Add YandexCredentials class for YandexCredentialsProvider +- Move ctors with HttpClient to factory methods README.md icon.png From 1d1ed728c4e35d89e9602d7687d3286b678d5ea9 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Mon, 15 Jan 2024 02:59:45 +0300 Subject: [PATCH 2/5] Add YandexId type to handle cursed Yandex ids (long/guid) --- .../AudioItems/YandexMusicTrack.cs | 5 +- YandexMusicResolver/Ids/YandexId.cs | 128 ++++++++++++++++++ YandexMusicResolver/Ids/YandexIdConverter.cs | 36 +++++ YandexMusicResolver/Responses/MetaTrack.cs | 5 +- 4 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 YandexMusicResolver/Ids/YandexId.cs create mode 100644 YandexMusicResolver/Ids/YandexIdConverter.cs diff --git a/YandexMusicResolver/AudioItems/YandexMusicTrack.cs b/YandexMusicResolver/AudioItems/YandexMusicTrack.cs index f84213f..34a7f40 100644 --- a/YandexMusicResolver/AudioItems/YandexMusicTrack.cs +++ b/YandexMusicResolver/AudioItems/YandexMusicTrack.cs @@ -1,13 +1,14 @@ using System; using System.Collections.Generic; using System.Linq; +using YandexMusicResolver.Ids; namespace YandexMusicResolver.AudioItems { /// /// AudioTrackInfo wrapper to resolve track direct url /// public class YandexMusicTrack { - internal YandexMusicTrack(string title, List authors, TimeSpan length, long id, string? uri, bool isAvailable, string? artworkUrl = null) { + internal YandexMusicTrack(string title, List authors, TimeSpan length, YandexId id, string? uri, bool isAvailable, string? artworkUrl = null) { Title = title; Authors = authors; Length = length; @@ -40,7 +41,7 @@ internal YandexMusicTrack(string title, List authors, TimeSpa /// /// Track id /// - public long Id { get; } + public YandexId Id { get; } /// /// Track link diff --git a/YandexMusicResolver/Ids/YandexId.cs b/YandexMusicResolver/Ids/YandexId.cs new file mode 100644 index 0000000..f073233 --- /dev/null +++ b/YandexMusicResolver/Ids/YandexId.cs @@ -0,0 +1,128 @@ +using System; + +namespace YandexMusicResolver.Ids; + +/// +/// Holds the yandex track id +/// +/// +/// Since Yandex fucked up as usual, it will return an ids for yandex's track, but will return ids for user uploaded tracks +/// +public struct YandexId { + private long? _longId; + private Guid? _guidId; + + /// + /// Gets the id + /// + /// Current Id is not an Guid type. You can look at to determine proper id type + public Guid GuidId { + get => _guidId ?? throw new InvalidOperationException($"Current Id is not an Guid type. Current type is {IdType}"); + set => _guidId = value; + } + + /// + /// Gets the id + /// + /// Current Id is not an long type. You can look at to determine proper id type + public long LongId { + get => _longId ?? throw new InvalidOperationException($"Current Id is not an long type. Current type is {IdType}"); + set => _longId = value; + } + + /// + /// Gets the current id type + /// + public YandexIdType IdType { get; } + + /// + /// Creates with id + /// + /// The id + public YandexId(Guid id) { + IdType = YandexIdType.Guid; + _guidId = id; + } + + /// + /// Creates with id + /// + /// The id + public YandexId(long id) { + IdType = YandexIdType.Long; + _longId = id; + } + + /// + /// Allows to implicitly cast to + /// + /// Instance of + /// The value + public static implicit operator Guid(YandexId yandexId) => yandexId.GuidId; + + /// + /// Allows to implicitly cast to + /// + /// Instance of + /// The value + public static implicit operator long(YandexId yandexId) => yandexId.LongId; + + /// + /// Allows to implicitly cast to + /// + /// value + /// The intance + public static implicit operator YandexId(long id) => new(id); + + /// + /// Allows to implicitly cast to + /// + /// value + /// The intance + public static implicit operator YandexId(Guid id) => new(id); + + /// + public override string ToString() { + if (_longId is not null) { + return _longId.ToString(); + } + + if (_guidId is not null) { + return _guidId.ToString(); + } + + throw new NotSupportedException("Current Id type doesn't support to string conversion. Report this to the library author"); + } + + /// + /// Tries to parse from the string + /// + /// to parse the Id + /// Instance of + /// Current string cannot be converted to any known Id type. Check the string + public static YandexId Parse(string s) { + if (Guid.TryParse(s, out var guid)) { + return new YandexId(guid); + } + + if (long.TryParse(s, out var l)) { + return new YandexId(l); + } + + throw new NotSupportedException("Current string cannot be converted to any known Id type. Check the string"); + } + + /// + /// Available types of Yandex ids + /// + public enum YandexIdType { + /// + /// Current id is long + /// + Long, + /// + /// Current id is Guid + /// + Guid + } +} diff --git a/YandexMusicResolver/Ids/YandexIdConverter.cs b/YandexMusicResolver/Ids/YandexIdConverter.cs new file mode 100644 index 0000000..e27d333 --- /dev/null +++ b/YandexMusicResolver/Ids/YandexIdConverter.cs @@ -0,0 +1,36 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace YandexMusicResolver.Ids; + +/// +/// Allows to properly serialize/deserialize the from JSON +/// +public class YandexIdConverter : JsonConverter { + /// + public override YandexId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + if (reader.TryGetInt64(out var l)) { + return new YandexId(l); + } + + if (reader.TryGetGuid(out var guid)) { + return new YandexId(guid); + } + + throw new NotSupportedException("Current value seems doesn't look like any known YandexId type"); + } + /// + public override void Write(Utf8JsonWriter writer, YandexId value, JsonSerializerOptions options) { + switch (value.IdType) { + case YandexId.YandexIdType.Long: + writer.WriteNumberValue(value.LongId); + break; + case YandexId.YandexIdType.Guid: + writer.WriteStringValue(value.GuidId); + break; + default: + throw new ArgumentOutOfRangeException(nameof(value.IdType), "Current YandexId type can't be wrote to JSON"); + } + } +} diff --git a/YandexMusicResolver/Responses/MetaTrack.cs b/YandexMusicResolver/Responses/MetaTrack.cs index 0670668..ce29e82 100644 --- a/YandexMusicResolver/Responses/MetaTrack.cs +++ b/YandexMusicResolver/Responses/MetaTrack.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text.Json.Serialization; using YandexMusicResolver.AudioItems; +using YandexMusicResolver.Ids; namespace YandexMusicResolver.Responses { /// @@ -11,8 +12,8 @@ namespace YandexMusicResolver.Responses { internal class MetaTrack { private const string TrackUrlFormat = "https://music.yandex.ru/album/{0}/track/{1}"; - [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] - public long Id { get; set; } + [JsonConverter(typeof(YandexIdConverter))] + public YandexId Id { get; set; } public string Title { get; set; } = null!; From 62995c3cf252b706c57693c82062f1e0f2b15837 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Tue, 16 Jan 2024 01:50:29 +0300 Subject: [PATCH 3/5] Add new overloads to IYandexMusicTrackLoader --- .../Loaders/IYandexMusicTrackLoader.cs | 46 ++++++++++++++++++- .../Loaders/YandexMusicTrackLoader.cs | 23 +++++++++- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/YandexMusicResolver/Loaders/IYandexMusicTrackLoader.cs b/YandexMusicResolver/Loaders/IYandexMusicTrackLoader.cs index c8c44b4..d5613c1 100644 --- a/YandexMusicResolver/Loaders/IYandexMusicTrackLoader.cs +++ b/YandexMusicResolver/Loaders/IYandexMusicTrackLoader.cs @@ -1,6 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using YandexMusicResolver.AudioItems; +using YandexMusicResolver.Ids; namespace YandexMusicResolver.Loaders { /// @@ -13,6 +15,27 @@ public interface IYandexMusicTrackLoader { /// Target track id /// Instance of Task LoadTrack(long trackId); + + /// + /// Load track info + /// + /// Target track id + /// Instance of + Task LoadTrack(Guid trackId); + + /// + /// Load track info + /// + /// Target track id + /// Instance of + Task LoadTrack(YandexId trackId); + + /// + /// Load track info + /// + /// Target track id + /// Instance of + Task LoadTrack(string trackId); /// /// Load track infos @@ -20,5 +43,26 @@ public interface IYandexMusicTrackLoader { /// Target track ids /// List of instances of Task> LoadTracks(IEnumerable trackIds); + + /// + /// Load track infos + /// + /// Target track ids + /// List of instances of + Task> LoadTracks(IEnumerable trackIds); + + /// + /// Load track infos + /// + /// Target track ids + /// List of instances of + Task> LoadTracks(IEnumerable trackIds); + + /// + /// Load track infos + /// + /// Target track ids + /// List of instances of + Task> LoadTracks(IEnumerable trackIds); } } diff --git a/YandexMusicResolver/Loaders/YandexMusicTrackLoader.cs b/YandexMusicResolver/Loaders/YandexMusicTrackLoader.cs index 7d6f2ba..939a081 100644 --- a/YandexMusicResolver/Loaders/YandexMusicTrackLoader.cs +++ b/YandexMusicResolver/Loaders/YandexMusicTrackLoader.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using YandexMusicResolver.AudioItems; using YandexMusicResolver.Config; +using YandexMusicResolver.Ids; using YandexMusicResolver.Responses; namespace YandexMusicResolver.Loaders { @@ -40,7 +41,16 @@ public static YandexMusicTrackLoader CreateWithHttpClient(IYandexCredentialsProv } /// - public async Task LoadTrack(long trackId) { + public Task LoadTrack(long trackId) => LoadTrack(trackId.ToString()); + + /// + public Task LoadTrack(Guid trackId) => LoadTrack(trackId.ToString()); + + /// + public Task LoadTrack(YandexId trackId) => LoadTrack(trackId.ToString()); + + /// + public async Task LoadTrack(string trackId) { try { var url = TracksInfoFormat + trackId; var response = await _httpClient.PerformYMusicRequestAsync>(_credentialsProvider, url); @@ -52,7 +62,16 @@ public static YandexMusicTrackLoader CreateWithHttpClient(IYandexCredentialsProv } /// - public async Task> LoadTracks(IEnumerable trackIds) { + public Task> LoadTracks(IEnumerable trackIds) => LoadTracks(trackIds.Select(l => l.ToString())); + + /// + public Task> LoadTracks(IEnumerable trackIds) => LoadTracks(trackIds.Select(l => l.ToString())); + + /// + public Task> LoadTracks(IEnumerable trackIds) => LoadTracks(trackIds.Select(l => l.ToString())); + + /// + public async Task> LoadTracks(IEnumerable trackIds) { try { var trackIdsString = string.Join(",", trackIds); var url = TracksInfoFormat + trackIdsString; From af3892396bfb26ccf84d1766fb73239afc2c2c51 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Tue, 16 Jan 2024 01:51:26 +0300 Subject: [PATCH 4/5] Fix YandexIdConverter --- YandexMusicResolver/Ids/YandexIdConverter.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/YandexMusicResolver/Ids/YandexIdConverter.cs b/YandexMusicResolver/Ids/YandexIdConverter.cs index e27d333..670f365 100644 --- a/YandexMusicResolver/Ids/YandexIdConverter.cs +++ b/YandexMusicResolver/Ids/YandexIdConverter.cs @@ -10,14 +10,20 @@ namespace YandexMusicResolver.Ids; public class YandexIdConverter : JsonConverter { /// public override YandexId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - if (reader.TryGetInt64(out var l)) { - return new YandexId(l); - } + if (reader.TokenType == JsonTokenType.String) { + var s = reader.GetString(); + if (long.TryParse(s, out var l)) { + return new YandexId(l); + } - if (reader.TryGetGuid(out var guid)) { - return new YandexId(guid); + if (Guid.TryParse(s, out var guid)) { + return new YandexId(guid); + } + } + else if (reader.TokenType == JsonTokenType.Number) { + var int64 = reader.GetInt64(); + return new YandexId(int64); } - throw new NotSupportedException("Current value seems doesn't look like any known YandexId type"); } /// From ead9bf4ed95fea8c99a7439f9c0bad1ec9486524 Mon Sep 17 00:00:00 2001 From: SKProCH Date: Tue, 16 Jan 2024 01:51:47 +0300 Subject: [PATCH 5/5] Replace ids to YandexId is some internal classes to fix serialization --- YandexMusicResolver/AudioItems/YandexMusicArtist.cs | 8 ++++++-- .../Responses/MetaPlaylistTrackContainer.cs | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/YandexMusicResolver/AudioItems/YandexMusicArtist.cs b/YandexMusicResolver/AudioItems/YandexMusicArtist.cs index d620650..2cb96fd 100644 --- a/YandexMusicResolver/AudioItems/YandexMusicArtist.cs +++ b/YandexMusicResolver/AudioItems/YandexMusicArtist.cs @@ -1,4 +1,7 @@ -namespace YandexMusicResolver.AudioItems { +using System.Text.Json.Serialization; +using YandexMusicResolver.Ids; + +namespace YandexMusicResolver.AudioItems { /// /// Represent a artist in Yandex Music /// @@ -6,7 +9,8 @@ public class YandexMusicArtist { /// /// Artist ID /// - public long Id { get; set; } + [JsonConverter(typeof(YandexIdConverter))] + public YandexId Id { get; set; } /// /// Artist name diff --git a/YandexMusicResolver/Responses/MetaPlaylistTrackContainer.cs b/YandexMusicResolver/Responses/MetaPlaylistTrackContainer.cs index 2da23c0..da66af7 100644 --- a/YandexMusicResolver/Responses/MetaPlaylistTrackContainer.cs +++ b/YandexMusicResolver/Responses/MetaPlaylistTrackContainer.cs @@ -1,6 +1,10 @@ -namespace YandexMusicResolver.Responses { +using System.Text.Json.Serialization; +using YandexMusicResolver.Ids; + +namespace YandexMusicResolver.Responses { internal class MetaPlaylistTrackContainer { - public long Id { get; set; } + [JsonConverter(typeof(YandexIdConverter))] + public YandexId Id { get; set; } public MetaTrack? Track { get; set; } }