ApiClientService.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Alchemy.Core.Extensions;
  2. using Newtonsoft.Json.Linq;
  3. using RestSharp;
  4. namespace Alchemy.Core.Services
  5. {
  6. public class ApiClientService
  7. {
  8. /// <summary>
  9. /// 发起API请求
  10. /// </summary>
  11. /// <param name="reqMethod"></param>
  12. /// <param name="strBaseUrl"></param>
  13. /// <param name="strRouteUrl"></param>
  14. /// <param name="queryParam"></param>
  15. /// <param name="body"></param>
  16. /// <param name="headerList"></param>
  17. /// <param name="timeOut"></param>
  18. /// <returns></returns>
  19. public static ApiResponse Request(Method reqMethod, string strBaseUrl, string strRouteUrl = null, Dictionary<string, string> queryParam = null, object body = null, Dictionary<string, string> headerList = null, string strContentType = "application/json", int timeOut = 3)
  20. {
  21. ApiResponse resp = new ApiResponse()
  22. {
  23. StatusCode = -1,
  24. Content = string.Empty
  25. };
  26. RestClient client = new RestClient(strBaseUrl);
  27. if (queryParam is not null)
  28. {
  29. if (queryParam.Count > 0)
  30. strRouteUrl += "?";
  31. int index = 0;
  32. foreach (KeyValuePair<string, string> kvp in queryParam)
  33. {
  34. strRouteUrl += $"{System.Web.HttpUtility.UrlEncode(kvp.Key)}={System.Web.HttpUtility.UrlEncode(kvp.Value)}";
  35. if (index < queryParam.Count - 1)
  36. strRouteUrl += "&";
  37. index++;
  38. }
  39. }
  40. RestRequest request = new RestRequest(strRouteUrl, reqMethod);
  41. if (headerList is not null)
  42. {
  43. foreach (KeyValuePair<string, string> kvp in headerList)
  44. {
  45. request.AddHeader(kvp.Key, kvp.Value);
  46. }
  47. }
  48. request.Timeout = TimeSpan.FromSeconds(timeOut);
  49. if (!body.IsNull())
  50. {
  51. if (body is JObject && strContentType.StartsWith("application/json"))
  52. {
  53. request.AddHeader("Content-Type", strContentType);
  54. request.RequestFormat = DataFormat.Json;
  55. request.AddJsonBody(body.ToJson());
  56. }
  57. if (body is JObject && strContentType.StartsWith("multipart/form-data"))
  58. {
  59. JObject jo = (JObject)body;
  60. foreach (JProperty? jp in jo.Properties())
  61. {
  62. if (jp.IsNull())
  63. continue;
  64. if (jp.Name.Equals("size"))
  65. request.AddParameter(jp.Name, jp.Value.ToString().ToLong());
  66. if (!jp.Name.Equals("file"))
  67. request.AddParameter(jp.Name, jp.Value.ToString());
  68. else
  69. request.AddFile("file", jp.Value.ToString());
  70. }
  71. }
  72. }
  73. string strResp = string.Empty;
  74. try
  75. {
  76. RestResponse<ApiResponse> response = client.Execute<ApiResponse>(request);
  77. if (response is not null)
  78. {
  79. resp.StatusCode = (int)response.StatusCode;
  80. resp.Content = response.Content;
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. resp.Content = ex.Message;
  86. }
  87. return resp;
  88. }
  89. }
  90. public class ApiResponse
  91. {
  92. public int StatusCode { get; set; }
  93. public string Content { get; set; }
  94. }
  95. }