Files
YandexMusic/YandexMusic.API/Common/Providers/CommonRequestProvider.cs
2026-04-10 12:12:33 +03:00

60 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 YandexMusic.API.Models.Common;
namespace YandexMusic.API.Common.Providers
{
public class CommonRequestProvider : IRequestProvider
{
#region Поля
protected AuthStorage storage;
#endregion Поля
public CommonRequestProvider(AuthStorage authStorage)
{
storage = authStorage;
}
#region IRequestProvider
public virtual Task<HttpResponseMessage> GetWebResponseAsync(HttpRequestMessage message, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
{
throw new NotImplementedException();
}
public virtual async Task<T> GetDataFromResponseAsync<T>(YandexMusicApi api, HttpResponseMessage response)
{
string result = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
YErrorResponse exception = JsonConvert.DeserializeObject<YErrorResponse>(result);
throw exception ?? new Exception("Ошибка десериализации ответа с ошибкой.");
}
try
{
JsonSerializerSettings settings = new()
{
Converters = new List<JsonConverter> {
new YExecutionContextConverter(api, storage)
}
};
return storage.Debug != null
? storage.Debug.Deserialize<T>(response.RequestMessage?.RequestUri?.AbsolutePath, result, settings)
: JsonConvert.DeserializeObject<T>(result, settings);
}
catch (Exception ex)
{
throw new Exception($"Ошибка десериализации {ex}");
}
}
#endregion IRequestProvider
}
}