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

69 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.Net;
using YandexMusic.API.Models.Common;
namespace YandexMusic.API.Common.Providers
{
/// <summary>
/// Стандартный провайдер запросов
/// </summary>
public class DefaultRequestProvider : CommonRequestProvider
{
#region Вспомогательные функции
private Exception ProcessException(Exception ex)
{
if (ex is not WebException webException)
return ex;
if (webException.Response is null)
return ex;
Stream s = webException.Response.GetResponseStream();
if (s is null)
return ex;
using StreamReader sr = new(s);
string result = sr.ReadToEnd();
YErrorResponse exception = JsonConvert.DeserializeObject<YErrorResponse>(result);
return exception ?? ex;
}
#endregion Вспомогательные функции
public DefaultRequestProvider(AuthStorage authStorage) : base(authStorage)
{
}
#region IRequestProvider
public override Task<HttpResponseMessage> GetWebResponseAsync(HttpRequestMessage message,
HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
{
try
{
HttpClient client = new(new SocketsHttpHandler
{
Proxy = storage.Context.WebProxy,
AutomaticDecompression = DecompressionMethods.GZip,
UseCookies = true,
CookieContainer = storage.Context.Cookies,
});
return client.SendAsync(message, completionOption);
}
catch (Exception ex)
{
throw ProcessException(ex);
}
}
#endregion IRequestProvider
}
}