fix execute context
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
internal class Program
|
||||
using YandexMusic.API.Extensions.API;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
private async static Task Main(string[] args)
|
||||
{
|
||||
@@ -6,5 +8,9 @@
|
||||
var type = await client.Authorize("y0__xDy2budARje-AYg7rmliBc11LbYoMeUiwiO6f6mSCAMDYVIKg");
|
||||
var playlists = (await client.GetFavoritesAsync()).Where(t => t.Owner.Uid == client.Account.Uid).ToList();
|
||||
var playlist = await client.GetPlaylistAsync("97ae0768-8a40-8485-9fa4-b6c856bc6b21");
|
||||
|
||||
|
||||
var tracks = await client.GetTracksAsync(new[] { "78412759" });
|
||||
await playlist.InsertTracksAsync(tracks.ToArray());
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,6 @@ public abstract class CommonRequestProvider : IRequestProvider
|
||||
/// <summary>Хранилище данных авторизации.</summary>
|
||||
protected readonly AuthStorage storage;
|
||||
|
||||
/// <summary>Настройки сериализации JSON (регистронезависимые, поддержка enum-строк).</summary>
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = { new JsonStringEnumConverter(JsonNamingPolicy.KebabCaseLower), new IntToStringConverter() }
|
||||
};
|
||||
|
||||
/// <summary>Инициализирует новый экземпляр провайдера.</summary>
|
||||
/// <param name="authStorage">Хранилище авторизации.</param>
|
||||
protected CommonRequestProvider(AuthStorage authStorage)
|
||||
@@ -37,6 +30,16 @@ public abstract class CommonRequestProvider : IRequestProvider
|
||||
{
|
||||
var json = await response.Content.ReadAsStringAsync();
|
||||
|
||||
JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters = {
|
||||
new JsonStringEnumConverter(JsonNamingPolicy.KebabCaseLower),
|
||||
new IntToStringConverter(),
|
||||
new YExecutionContextConverter(api, storage),
|
||||
}
|
||||
};
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var error = JsonSerializer.Deserialize<YErrorResponse>(json, JsonOptions);
|
||||
|
||||
@@ -5,7 +5,7 @@ using YandexMusic.API.Common;
|
||||
namespace YandexMusic.API.Models.Common;
|
||||
|
||||
/// <summary>Конвертер для внедрения контекста выполнения (API и хранилище) в модели.</summary>
|
||||
public class YExecutionContextConverter : JsonConverter<YBaseModel>
|
||||
public class YExecutionContextConverter : JsonConverter<object>
|
||||
{
|
||||
private readonly YandexMusicApi _api;
|
||||
private readonly AuthStorage _storage;
|
||||
@@ -16,26 +16,27 @@ public class YExecutionContextConverter : JsonConverter<YBaseModel>
|
||||
_storage = storage;
|
||||
}
|
||||
|
||||
public override YBaseModel? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
public override bool CanConvert(Type typeToConvert) =>
|
||||
typeof(YBaseModel).IsAssignableFrom(typeToConvert);
|
||||
|
||||
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
// Десериализуем объект без контекста
|
||||
var obj = (YBaseModel?)JsonSerializer.Deserialize(ref reader, typeToConvert, options);
|
||||
if (obj != null)
|
||||
// Убираем этот конвертер из опций, чтобы избежать рекурсии
|
||||
var innerOptions = new JsonSerializerOptions(options);
|
||||
innerOptions.Converters.Remove(this);
|
||||
|
||||
var obj = JsonSerializer.Deserialize(ref reader, typeToConvert, innerOptions);
|
||||
if (obj is YBaseModel baseModel)
|
||||
{
|
||||
obj.Context = new YExecutionContext
|
||||
{
|
||||
API = _api,
|
||||
Storage = _storage
|
||||
};
|
||||
baseModel.Context = new YExecutionContext { API = _api, Storage = _storage };
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, YBaseModel value, JsonSerializerOptions options)
|
||||
public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
|
||||
{
|
||||
// При сериализации игнорируем контекст
|
||||
var cloneOptions = new JsonSerializerOptions(options);
|
||||
cloneOptions.Converters.Remove(this);
|
||||
JsonSerializer.Serialize(writer, value, cloneOptions);
|
||||
var innerOptions = new JsonSerializerOptions(options);
|
||||
innerOptions.Converters.Remove(this);
|
||||
JsonSerializer.Serialize(writer, value, innerOptions);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user