using Newtonsoft.Json;
namespace Adplay.HTTP
{
    [System.Serializable]
    public struct HttpResponse<T>
    {
        public T Result { get; set; }
        public int Code { get; set; }
        public string Error { get; set; }
        public bool IsSuccess { get; set; }

        public HttpResponse(string text, int responseCode)
        {
            Code = responseCode;
            IsSuccess = Code >= 200 && Code < 300;
            if (IsSuccess)
                if (typeof(T) == typeof(string))
                {
                    Result = (T)(object)text;
                }
                else
                {
                    Result = JsonConvert.DeserializeObject<T>(text);
                }
            else
                Result = default;
            Error = IsSuccess ? "" : text;
        }

        public HttpResponse(T result, int code)
        {
            Result = result;
            Code = code;
            IsSuccess = Code >= 200 && Code < 300;
            Error = "";

        }

    }

}
