using Common; using JMETxnPushScheduler.Business; using JMETxnPushScheduler.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; namespace JMETxnPushScheduler.API { public class ThirdPartyAPI { private static int timeOut = 100; private string tp_base_url = GetStatic.ReadWebConfig("ThirdParty_Base_Url", ""); private string apiAccessKey = GetStatic.ReadWebConfig("apiAccessKey", ""); //private readonly ILog _log = LogManager.GetLogger(typeof(ThirdPartyApi)); public ResponseModel ThirdPartyApiGetDataOnly(RequestModel model, string api_url, out APIJsonResponse jsonResponse, string MethodType = "post") { ResponseModel _responseModel = default(ResponseModel); using (HttpClient httpClient = new HttpClient()) { try { httpClient.BaseAddress = new Uri(tp_base_url); httpClient.DefaultRequestHeaders.Add("apiAccessKey", apiAccessKey); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.Timeout = new TimeSpan(0, 0, timeOut); HttpResponseMessage resp = new HttpResponseMessage(); if (MethodType.ToLower().Equals("get")) resp = httpClient.GetAsync(api_url).Result; if (MethodType.ToLower().Equals("put")) { StringContent jbdContent = new StringContent(JsonConvert.SerializeObject(model).ToString(), Encoding.UTF8, "application/json"); resp = httpClient.PutAsync(api_url, jbdContent).Result; } if (MethodType.ToLower().Equals("post")) { var jsonData = JsonConvert.SerializeObject(model).ToString(); StringContent jbdContent = new StringContent(jsonData, Encoding.UTF8, "application/json"); resp = httpClient.PostAsync(api_url, jbdContent).Result; } string resultData = resp.Content.ReadAsStringAsync().Result; if (resp.IsSuccessStatusCode) { var result = resp.Content.ReadAsStringAsync().Result; JObject a = JObject.Parse(result); _responseModel = a.ToObject(); } else { //_log.Debug("ThirdPartyApiGetDataOnly | RESPONSE: " + resp.ToString()); ErrorJosn errorJson = JsonConvert.DeserializeObject(resultData); APIJsonResponse jsonResponseData = JsonConvert.DeserializeObject(errorJson.Message); List data = JsonConvert.DeserializeObject>(jsonResponseData.Data.ToString()); jsonResponse = new APIJsonResponse() { Id = jsonResponseData.Id, Msg = jsonResponseData.Msg, Data = data, Extra = jsonResponseData.Extra, Extra1 = jsonResponseData.Extra1 }; } } catch (HttpRequestException ex) { jsonResponse = new APIJsonResponse() { ResponseCode = "999", Msg = ex.Message, Data = ex.Data, }; _responseModel = default(ResponseModel); } }; jsonResponse = new APIJsonResponse() { ResponseCode = "0", Msg = "Api Calling process successfully!" }; return _responseModel; } } }