You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
4.2 KiB

  1. using Common;
  2. using JMETxnPushScheduler.Business;
  3. using JMETxnPushScheduler.Common;
  4. using log4net;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net.Http;
  11. using System.Net.Http.Headers;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace JMETxnPushScheduler.API
  15. {
  16. public class ThirdPartyAPI
  17. {
  18. private static int timeOut = 100;
  19. private string tp_base_url = GetStatic.ReadWebConfig("ThirdParty_Base_Url", "");
  20. private string apiAccessKey = GetStatic.ReadWebConfig("apiAccessKey", "");
  21. //private readonly ILog _log = LogManager.GetLogger(typeof(ThirdPartyApi));
  22. public ResponseModel ThirdPartyApiGetDataOnly<RequestModel, ResponseModel>(RequestModel model, string api_url, out APIJsonResponse jsonResponse, string MethodType = "post")
  23. {
  24. ResponseModel _responseModel = default(ResponseModel);
  25. using (HttpClient httpClient = new HttpClient())
  26. {
  27. try
  28. {
  29. httpClient.BaseAddress = new Uri(tp_base_url);
  30. httpClient.DefaultRequestHeaders.Add("apiAccessKey", apiAccessKey);
  31. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  32. httpClient.Timeout = new TimeSpan(0, 0, timeOut);
  33. HttpResponseMessage resp = new HttpResponseMessage();
  34. if (MethodType.ToLower().Equals("get"))
  35. resp = httpClient.GetAsync(api_url).Result;
  36. if (MethodType.ToLower().Equals("put"))
  37. {
  38. StringContent jbdContent = new StringContent(JsonConvert.SerializeObject(model).ToString(), Encoding.UTF8, "application/json");
  39. resp = httpClient.PutAsync(api_url, jbdContent).Result;
  40. }
  41. if (MethodType.ToLower().Equals("post"))
  42. {
  43. var jsonData = JsonConvert.SerializeObject(model).ToString();
  44. StringContent jbdContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
  45. resp = httpClient.PostAsync(api_url, jbdContent).Result;
  46. }
  47. string resultData = resp.Content.ReadAsStringAsync().Result;
  48. if (resp.IsSuccessStatusCode)
  49. {
  50. var result = resp.Content.ReadAsStringAsync().Result;
  51. JObject a = JObject.Parse(result);
  52. _responseModel = a.ToObject<ResponseModel>();
  53. }
  54. else
  55. {
  56. //_log.Debug("ThirdPartyApiGetDataOnly | RESPONSE: " + resp.ToString());
  57. ErrorJosn errorJson = JsonConvert.DeserializeObject<ErrorJosn>(resultData);
  58. APIJsonResponse jsonResponseData = JsonConvert.DeserializeObject<APIJsonResponse>(errorJson.Message);
  59. List<Data> data = JsonConvert.DeserializeObject<List<Data>>(jsonResponseData.Data.ToString());
  60. jsonResponse = new APIJsonResponse()
  61. {
  62. Id = jsonResponseData.Id,
  63. Msg = jsonResponseData.Msg,
  64. Data = data,
  65. Extra = jsonResponseData.Extra,
  66. Extra1 = jsonResponseData.Extra1
  67. };
  68. }
  69. }
  70. catch (HttpRequestException ex)
  71. {
  72. jsonResponse = new APIJsonResponse()
  73. {
  74. ResponseCode = "999",
  75. Msg = ex.Message,
  76. Data = ex.Data,
  77. };
  78. _responseModel = default(ResponseModel);
  79. }
  80. };
  81. jsonResponse = new APIJsonResponse()
  82. {
  83. ResponseCode = "0",
  84. Msg = "Api Calling process successfully!"
  85. };
  86. return _responseModel;
  87. }
  88. }
  89. }