Полностью переписанное api
All checks were successful
Release / pack-and-publish (release) Successful in 36s
All checks were successful
Release / pack-and-publish (release) Successful in 36s
This commit is contained in:
55
YandexMusic.API/Requests/Common/YMusicRequestBuilder.cs
Normal file
55
YandexMusic.API/Requests/Common/YMusicRequestBuilder.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using YandexMusic.API.Converters;
|
||||
using YandexMusic.API.Models.Common;
|
||||
using YandexMusic.API.Requests.Common;
|
||||
|
||||
namespace YandexMusic.API.Requests;
|
||||
|
||||
/// <summary>Базовый класс для запросов к API Яндекс Музыки (api.music.yandex.net).</summary>
|
||||
public abstract class YMusicRequestBuilder<TResponse, TParams> : YJsonRequestBuilder<TResponse, TParams>
|
||||
{
|
||||
protected override string BaseUrl => YConstants.Endpoints.MusicUrl;
|
||||
|
||||
protected YMusicRequestBuilder(YandexMusicApi api) : base(api) { }
|
||||
|
||||
protected override void SetCustomHeaders(HttpRequestHeaders headers)
|
||||
{
|
||||
base.SetCustomHeaders(headers);
|
||||
headers.Add("X-Yandex-Music-Client", Storage.DeviceId);
|
||||
}
|
||||
|
||||
protected override 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
|
||||
{
|
||||
var uResponse = JsonSerializer.Deserialize<YResponse<TResponse>>(json, options);
|
||||
if (uResponse == null) return default;
|
||||
return uResponse.Result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception($"Ошибка десериализации: {ex.Message}\nJSON: {json}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user