Files
YandexMusic/YandexMusic.API/Requests/Common/YJsonRequestBuilder.cs
FrigaT add7f08215
All checks were successful
Release / pack-and-publish (release) Successful in 32s
Добавлен вывод AuthStorage. Спрятаны внутренние api запросы
2026-04-19 17:41:30 +03:00

54 lines
1.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text.Json;
using System.Text.Json.Serialization;
using YandexMusic.API.Converters;
using YandexMusic.API.Models.Common;
namespace YandexMusic.API.Requests.Common;
/// <summary>
/// Строитель запросов с десериализацией JSON-ответа в TResponse.
/// </summary>
internal abstract class YJsonRequestBuilder<TResponse, TParams> : YRequestBuilder<TParams>
{
protected YJsonRequestBuilder(YandexMusicApi api) : base(api) { }
protected virtual async Task<TResponse?> DeserializeAsync(HttpResponseMessage response)
{
var json = await response.Content.ReadAsStringAsync();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Converters = {
new JsonStringEnumConverter(JsonNamingPolicy.KebabCaseLower),
new IntToStringConverter(),
new StringToIntConverter(),
new YExecutionContextConverter(Api, Storage),
}
};
if (!response.IsSuccessStatusCode)
{
var error = JsonSerializer.Deserialize<YErrorResponse>(json, options);
throw error ?? new Exception($"Ошибка HTTP {response.StatusCode}: {json}");
}
try
{
return JsonSerializer.Deserialize<TResponse>(json, options);
}
catch (Exception ex)
{
throw new Exception($"Ошибка десериализации: {ex.Message}\nJSON: {json}", ex);
}
}
/// <summary>
/// Выполняет запрос и возвращает десериализованный объект типа TResponse.
/// </summary>
public async Task<TResponse?> ExecuteAsync(TParams parameters)
{
using var response = await ExecuteRawAsync(parameters);
return await DeserializeAsync(response);
}
}