60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
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
|
||
}
|
||
} |