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.1 KiB

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