using CsharpHttpHelper;
using DeviceRepair.Models;
using Newtonsoft.Json;
using System;
using System.Net;

namespace DeviceRepair.Utils
{
    public static class JsonHelper
    {
        /// <summary>
        /// 接口调用返回转对象
        /// </summary>
        /// <param name="http"></param>
        /// <returns></returns>
        public static APIResponseData GetApiResponseData(this HttpResult http)
        {
            APIResponseData apiResponseData = new APIResponseData { Code = -1, Message = "网络连接出错!" };
            try
            {
                if (http.StatusCode == HttpStatusCode.OK)
                    apiResponseData = JsonConvert.DeserializeObject<APIResponseData>(http.Html);
                else if (http.StatusCode == 0)
                    return apiResponseData;
                else
                    throw new Exception(http.StatusDescription);
            }
            catch (Exception ex)
            {
                apiResponseData.Message = ex.Message;
                apiResponseData.Code = -1;
            }
            return apiResponseData;
        }


    }
}
