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.

88 lines
4.1 KiB

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