.net 呼叫API並解析Json資料方法

weixin_33831673發表於2014-10-09
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Jyson.IZhanShi.Model.WebApiModel;

namespace Jyson.IZhanShi.WinDesktop.Utility
{
    public class OperateHttp
    {
        private OperateHttp() { }
        /// <summary>
        /// 單例模式
        /// </summary>
        private static OperateHttp _instance = null;
        public static OperateHttp Instance()
        {
            if (_instance == null)
            {
                _instance = new OperateHttp();
            }
            return _instance;
        }

        static HttpClient _httpClient;
        public HttpClient httpClient
        {
            get
            {
                if (_httpClient == null)
                    _httpClient = new HttpClient();
                return _httpClient;
            }
            set { _httpClient = value; }
        }
        /// <summary>
        /// 餐廳ID
        /// </summary>
        public int RestaurantID { get; set; }
        public string RestaurantName { get; set; }
        public string RestaurantSessionKey { get; set; }

        #region --Get/Post/Put/Delete 方法操作
        /// <summary>
        /// Get方式獲取資料
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public JsonData GetResult(string httpUrl)
        {
            HttpResponseMessage hrm = httpClient.GetAsync(httpUrl).Result;
            JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result;
            return data;
        }
        /// <summary>
        /// Get 非同步方式獲取資料
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        //public async Task<JsonData> GetAsyncResult(string httpUrl)
        //{
        //    HttpResponseMessage hrm = await httpClient.GetAsync(httpUrl).ConfigureAwait(false);
        //    JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result;
        //    return data;
        //}
        /// <summary>
        /// Post 方式傳遞資料
        /// </summary>
        /// <param name="postModel"></param>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public JsonData PostResult(string httpUrl, object postModel)
        {
            HttpResponseMessage hrm = httpClient.PostAsJsonAsync(httpUrl, postModel).Result;
            JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result;
            return data;
        }
        /// <summary>
        ///  Post 非同步方式傳遞資料
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <param name="postModel"></param>
        /// <returns></returns>
        //public async Task<JsonData> PostAsyncResult(string httpUrl, object postModel)
        //{
        //    var hrm = await httpClient.PostAsJsonAsync(httpUrl, postModel).ConfigureAwait(false);
        //    JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result;
        //    return data;
        //}
        /// <summary>
        /// Put 更新方式傳遞資料
        /// </summary>
        /// <param name="postModel"></param>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public JsonData PutResult(string httpUrl, object postModel)
        {
            HttpResponseMessage hrm = httpClient.PutAsJsonAsync(httpUrl, postModel).Result;
            JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result;
            return data;
        }
        /// <summary>
        /// Delete 刪除方式傳遞資料
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public JsonData DeleteResult(string httpUrl)
        {
            HttpResponseMessage hrm = httpClient.DeleteAsync(httpUrl).Result;
            JsonData data = hrm.Content.ReadAsAsync<JsonData>().Result;
            return data;
        }
        #endregion

        #region 選單操作
        /// <summary>
        /// 獲取該店鋪所有選單列表
        /// </summary>
        /// <returns></returns>
        public List<WMFoodMenu> GetFoodMenuList(string httpUrl)
        {
            JsonData prod = OperateHttp.Instance().GetResult(httpUrl);
            var model = JsonConvert.DeserializeObject<List<WMFoodMenu>>(prod.data.ToString());
            return model;
        }

        /// <summary>
        /// 獲取選單類別
        /// </summary>
        /// <returns></returns>
        public List<WMFoodMenuCategory> GetFoodMenuCategory()
        {
            string httpUrl = HttpUrl.FoodMenuCategoryUrl + OperateHttp.Instance().RestaurantID;
            JsonData prod = OperateHttp.Instance().GetResult(httpUrl);
            var model = JsonConvert.DeserializeObject<List<WMFoodMenuCategory>>(prod.data.ToString());
            return model;
        }
        /// <summary>
        /// 獲取選單單個資訊
        /// </summary>
        /// <returns></returns>
        public WMFoodMenu GetFoodMenu(int foodMenuID)
        {
            string httpUrl = HttpUrl.FoodMenuOneUrl + foodMenuID;
            JsonData prod = OperateHttp.Instance().GetResult(httpUrl);
            var model = JsonConvert.DeserializeObject<WMFoodMenu>(prod.data.ToString());
            return model;
        }
        /// <summary>
        /// 更新選單
        /// </summary>
        /// <returns></returns>
        public bool PutFoodMenu(int foodMenuID, WMFoodMenu model)
        {
            string httpUrl = HttpUrl.FoodMenuUrl + OperateHttp.Instance().RestaurantID;
            JsonData data = OperateHttp.Instance().PutResult(httpUrl, model);
            if (data.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(data.errorMsg))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新選單狀態
        /// </summary>
        /// <returns></returns>
        public bool PutIsSell(int foodMenuID, WMFoodMenuIsSell model)
        {
            string httpUrl = HttpUrl.FoodMenuOneUrl + foodMenuID.ToString();
            JsonData data = OperateHttp.Instance().PutResult(httpUrl, model);
            if (data.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(data.errorMsg))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 刪除選單
        /// </summary>
        /// <returns></returns>
        public bool DeleteFoodMenu(int foodMenuID)
        {
            string httpUrl = HttpUrl.FoodMenuOneUrl + foodMenuID.ToString();
            JsonData data = OperateHttp.Instance().DeleteResult(httpUrl);
            if (data.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(data.errorMsg))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        /// <summary>
        /// 獲取訂單列表
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public List<WMOrder> GetOrder(string httpUrl)
        {
            JsonData prod = OperateHttp.Instance().GetResult(httpUrl);
            var model = JsonConvert.DeserializeObject<List<WMOrder>>(prod.data.ToString());
            return model;
        }

        /// <summary>
        /// 獲取訂單列表
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public WMOrderDetail GetOrderDetail(int orderID)
        {
            string httpUrl = HttpUrl.OrderDetailUrl + orderID.ToString();
            JsonData prod = OperateHttp.Instance().GetResult(httpUrl);
            var model = JsonConvert.DeserializeObject<WMOrderDetail>(prod.data.ToString());
            return model;
        }
        /// <summary>
        /// 取消訂單
        /// </summary>
        /// <param name="orderID">訂單ID</param>
        /// <returns></returns>
        public bool DeleteOrder(int orderID)
        {
            string httpUrl = HttpUrl.OrderUrl + orderID.ToString();
            JsonData data = OperateHttp.Instance().DeleteResult(httpUrl);
            if (data.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(data.errorMsg))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 訂單列印
        /// </summary>
        /// <param name="httpUrl"></param>
        /// <returns></returns>
        public string PutOrderPrint(int orderID)
        {
            string httpUrl = HttpUrl.OrderPrintUrl + orderID.ToString();
            JsonData resultData = OperateHttp.Instance().PutResult(httpUrl, null);
            if (resultData.errorCode == (int)Model.Enum.ApiErrorEnum.Success && !string.IsNullOrEmpty(resultData.errorMsg))
            {
                return "Success";
            }
            else
            {
                return resultData.data.ToString();
            }
        }
    }
}

 

相關文章