using BuddyFinance.Integration.Models.Enums; using Newtonsoft.Json; using System; using System.Net.Http; namespace BuddyFinance.Common.Integration { public static class WebApiHelper { public static WebApiResponse Get(string url, string action, string token = "") //public static T Get(string url, string action, string token = "") where T : class // where T : BaseResult//, new() { try { using (var client = new HttpClient()) { client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Add("X-Auth-Token", token); var response = client.GetStringAsync(action).Result; var resultObject = JsonConvert.DeserializeObject(response); //return resultObject; return new WebApiResponse { ErrorCode = 0, ErrorMessage = "Successful", Result = resultObject }; } } catch (Exception ex) { return new WebApiResponse()// new T() { ErrorCode = -1, ErrorMessage = ex.ToString(), Result = null }; //return (T)new BaseResult //{ // ErrorCode = (int)ResponseCodes.Exception, // ErrorMessage = ex.Message, //}; } } public static WebApiResponse Post(string url, string action, object data, string token = "") // public static T Post(string url, string action, object data, string token = "") where T : class // where T : BaseResult//, new() { using (var client = new HttpClient()) { client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Add("X-Auth-Token", token); var result = client.PostAsJsonAsync(action, data).Result; var response = result.Content.ReadAsStringAsync().Result; try { var resultObject = JsonConvert.DeserializeObject(response); //return resultObject; return new WebApiResponse { ErrorCode = 0, ErrorMessage = "Successful", Result = resultObject }; } catch (Exception ex) { return new WebApiResponse { ErrorCode = (int)ResponseCodes.Exception, ErrorMessage = ex.Message, Result = null }; //return (T)new BaseResult //{ // ErrorCode = (int)ResponseCodes.Exception, // ErrorMessage = ex.Message, //}; } } } } }