Files
YandexMusic/YandexMusic.API/Requests/Common/YXmlRequestBuilder.cs
FrigaT 526353d679
All checks were successful
Release / pack-and-publish (release) Successful in 36s
Переделано воспроизведение аудио
2026-04-21 11:14:36 +03:00

47 lines
1.8 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.Xml;
using System.Xml.Serialization;
namespace YandexMusic.API.Requests.Common;
/// <summary>
/// Строитель запросов с десериализацией XML-ответа в TResponse.
/// </summary>
internal abstract class YXmlRequestBuilder<TResponse, TParams> : YRequestBuilder<TParams>
{
protected YXmlRequestBuilder(YandexMusicApi api) : base(api) { }
/// <summary>
/// Десериализует XML-ответ в объект типа TResponse.
/// </summary>
protected virtual async Task<TResponse?> DeserializeAsync(HttpResponseMessage response)
{
var xml = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
// Для XML-ошибок можно создать отдельную модель, но для простоты выбрасываем исключение
throw new Exception($"Ошибка HTTP {response.StatusCode}: {xml}");
}
try
{
using var stringReader = new StringReader(xml);
using var xmlReader = XmlReader.Create(stringReader, new XmlReaderSettings { Async = true });
var serializer = new XmlSerializer(typeof(TResponse));
return (TResponse?)serializer.Deserialize(xmlReader);
}
catch (Exception ex)
{
throw new Exception($"Ошибка десериализации XML: {ex.Message}\nXML: {xml}", ex);
}
}
/// <summary>
/// Выполняет запрос и возвращает десериализованный объект типа TResponse.
/// </summary>
public async Task<TResponse?> ExecuteAsync(TParams parameters)
{
using var response = await ExecuteRawAsync(parameters);
return await DeserializeAsync(response);
}
}