using YandexMusic.API.Common;
using YandexMusic.API.Common.Providers;

namespace YandexMusic.API.Requests.Common;

internal class YRequest<T>
{
    #region Поля

    private HttpRequestMessage msg;
    private IRequestProvider provider;

    protected YandexMusicApi api;

    #endregion Поля

    public YRequest(HttpRequestMessage message, YandexMusicApi yandex, AuthStorage auth)
    {
        msg = message;
        api = yandex;
        provider = auth.Provider;
    }

    public async Task<T> GetResponseAsync()
    {
        if (msg == null)
            return default;

        HttpResponseMessage response = await provider.GetWebResponseAsync(msg);

        if (typeof(T) == typeof(HttpResponseMessage))
            return (T)(object)response;

        try
        {
            return await provider.GetDataFromResponseAsync<T>(api, response);
        }
        finally
        {
            response.Dispose();
        }
    }


}