69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
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
|
||
}
|
||
} |