Добавьте файлы проекта.
This commit is contained in:
60
YandexMusic.API/Common/Providers/CommonRequestProvider.cs
Normal file
60
YandexMusic.API/Common/Providers/CommonRequestProvider.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
69
YandexMusic.API/Common/Providers/DefaultRequestProvider.cs
Normal file
69
YandexMusic.API/Common/Providers/DefaultRequestProvider.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
25
YandexMusic.API/Common/Providers/IRequestProvider.cs
Normal file
25
YandexMusic.API/Common/Providers/IRequestProvider.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace YandexMusic.API.Common.Providers
|
||||
{
|
||||
/// <summary>
|
||||
/// Интерфейс для провайдеров обработки запросов
|
||||
/// </summary>
|
||||
public interface IRequestProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Функция получения ответа
|
||||
/// </summary>
|
||||
/// <param name="message">Запрос</param>
|
||||
/// <param name="completionOption">Опция завершения запроса</param>
|
||||
/// <returns></returns>
|
||||
Task<HttpResponseMessage> GetWebResponseAsync(HttpRequestMessage message, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead);
|
||||
|
||||
/// <summary>
|
||||
/// Функция формирования ответа
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Тип объекта с ответом</typeparam>
|
||||
/// <param name="api">API</param>
|
||||
/// <param name="response">Ответ</param>
|
||||
/// <returns></returns>
|
||||
Task<T> GetDataFromResponseAsync<T>(YandexMusicApi api, HttpResponseMessage response);
|
||||
}
|
||||
}
|
||||
27
YandexMusic.API/Common/Providers/MockRequestProvider.cs
Normal file
27
YandexMusic.API/Common/Providers/MockRequestProvider.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
namespace YandexMusic.API.Common.Providers
|
||||
{
|
||||
/// <summary>
|
||||
/// Провайдер запросов данными из файла
|
||||
/// </summary>
|
||||
public class MockRequestProvider : CommonRequestProvider
|
||||
{
|
||||
|
||||
|
||||
public MockRequestProvider(AuthStorage authStorage) : base(authStorage)
|
||||
{
|
||||
storage = authStorage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#region IRequestProvider
|
||||
|
||||
public override Task<HttpResponseMessage> GetWebResponseAsync(HttpRequestMessage message,
|
||||
HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
#endregion IRequestProvider
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user