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.

96 lines
4.6 KiB

  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using Swift.API.Common;
  4. using System;
  5. using System.Net.Http;
  6. using System.Net.Http.Headers;
  7. using System.Text;
  8. namespace Swift.API.MapAPIData
  9. {
  10. public class DownloadAPIData
  11. {
  12. public ResponseModel ThirdPartyApiGetDataOnly<RequestModel, ResponseModel>(RequestModel model, string api_url, string objectName = "Data", string MethodType = "post")
  13. {
  14. ResponseModel _responseModel = default(ResponseModel);
  15. using (HttpClient httpClient = new HttpClient())
  16. {
  17. try
  18. {
  19. httpClient.BaseAddress = new Uri(Utility.ReadWebConfig("baseURL", ""));
  20. httpClient.DefaultRequestHeaders.Add("apiAccessKey", Utility.ReadWebConfig("apiAccessKey", ""));
  21. httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  22. httpClient.Timeout = new TimeSpan(0, 0, Convert.ToInt16(Utility.ReadWebConfig("timeOut", "")));
  23. HttpResponseMessage resp = new HttpResponseMessage();
  24. if (MethodType.ToLower().Equals("get"))
  25. resp = httpClient.GetAsync(api_url).Result;
  26. if (MethodType.ToLower().Equals("put"))
  27. {
  28. StringContent jbdContent = new StringContent(JsonConvert.SerializeObject(model).ToString(), Encoding.UTF8, "application/json");
  29. resp = httpClient.PutAsync(api_url, jbdContent).Result;
  30. }
  31. if (MethodType.ToLower().Equals("post"))
  32. {
  33. var jsonData = JsonConvert.SerializeObject(model).ToString();
  34. StringContent jbdContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
  35. resp = httpClient.PostAsync(api_url, jbdContent).Result;
  36. }
  37. if (resp.IsSuccessStatusCode)
  38. {
  39. var result = resp.Content.ReadAsStringAsync().Result;
  40. JObject a = JObject.Parse(result);
  41. //var aaaa = a["Data"].Value<JArray>();
  42. _responseModel = a.ToObject<ResponseModel>();
  43. }
  44. }
  45. catch (Exception)
  46. {
  47. _responseModel = default(ResponseModel);
  48. }
  49. };
  50. return _responseModel;
  51. }
  52. public ResponseModel ThirdPartyApiGetDataOnlyNew<RequestModel, ResponseModel>(RequestModel model, string api_url, string objectName = "Data", string MethodType = "post")
  53. {
  54. ResponseModel _responseModel = default(ResponseModel);
  55. JsonResponse jsonResponse = new JsonResponse();
  56. using (var httpClient = RestApiClient.CallJMEThirdParty())
  57. {
  58. try
  59. {
  60. HttpResponseMessage resp = new HttpResponseMessage();
  61. if (MethodType.ToLower().Equals("get"))
  62. resp = httpClient.GetAsync(api_url).Result;
  63. if (MethodType.ToLower().Equals("put"))
  64. {
  65. StringContent jbdContent = new StringContent(JsonConvert.SerializeObject(model).ToString(), Encoding.UTF8, "application/json");
  66. resp = httpClient.PutAsync(api_url, jbdContent).Result;
  67. }
  68. if (MethodType.ToLower().Equals("post"))
  69. {
  70. var jsonData = JsonConvert.SerializeObject(model).ToString();
  71. StringContent jbdContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
  72. resp = httpClient.PostAsync(api_url, jbdContent).Result;
  73. }
  74. if (resp.IsSuccessStatusCode)
  75. {
  76. var resultData = resp.Content.ReadAsStringAsync().Result;
  77. //jsonResponse = JsonConvert.DeserializeObject<JsonResponse>(resultData);
  78. JObject a = JObject.Parse(resultData);
  79. //var aaaa = a["Data"].Value<JArray>();
  80. _responseModel = a.ToObject<ResponseModel>();
  81. //_responseModel = default(ResponseModel);
  82. }
  83. }
  84. catch (Exception)
  85. {
  86. _responseModel = default(ResponseModel);
  87. }
  88. };
  89. return _responseModel;
  90. }
  91. }
  92. }