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.

181 lines
6.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Apewer.Network;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. namespace Apewer.Web
  6. {
  7. /// <summary></summary>
  8. public class ApiClient
  9. {
  10. private const int DefaultTimeout = 30000;
  11. /// <summary></summary>
  12. public string ApiUrl { get; set; }
  13. /// <summary></summary>
  14. public string Random { get; set; }
  15. /// <summary></summary>
  16. public string Ticket { get; set; }
  17. /// <summary></summary>
  18. public string Session { get; set; }
  19. /// <summary></summary>
  20. public string Page { get; set; }
  21. /// <summary></summary>
  22. public string UserAgent { get; set; }
  23. /// <summary></summary>
  24. public int Timeout { get; set; } = DefaultTimeout;
  25. /// <summary></summary>
  26. public static Result<Json> Return(byte[] bytes, Action<Json> succeed = null, Action<string> failed = null)
  27. {
  28. var text = TextUtility.FromBytes(bytes);
  29. var json = Json.From(text);
  30. return Return(json, succeed, failed);
  31. }
  32. /// <summary>解析响应并返回。</summary>
  33. public static Result<Json> Return(Json json, Action<Json> succeed = null, Action<string> failed = null)
  34. {
  35. if (json != null && json.Available)
  36. {
  37. var status = json["status"];
  38. if (status == "ok")
  39. {
  40. var data = json.GetProperty("data") ?? Json.NewObject();
  41. succeed?.Invoke(data);
  42. return new Result<Json>(data);
  43. }
  44. else
  45. {
  46. var message = json["message"];
  47. return Return(message, failed);
  48. }
  49. }
  50. return Return("响应为空。", failed);
  51. }
  52. /// <summary>返回错误。</summary>
  53. public static Result<Json> Return(string error, Action<string> failed = null)
  54. {
  55. var error2 = error.IsEmpty() ? "未知错误。" : error;
  56. failed?.Invoke(error2);
  57. return new Result<Json>(error2);
  58. }
  59. /// <summary>发起请求。</summary>
  60. public Result<Json> Send(string url, byte[] body = null, Action<Json> succeed = null, Action<string> failed = null, int timeout = DefaultTimeout)
  61. {
  62. if (url.IsEmpty()) return Return("参数 URL 无效。", failed);
  63. var client = new HttpClient();
  64. client.Request.Url = url;
  65. if (UserAgent.NotEmpty()) client.Request.UserAgent = UserAgent;
  66. if (body == null || body.Length < 1)
  67. {
  68. client.Request.Method = HttpMethod.GET;
  69. }
  70. else
  71. {
  72. client.Request.Method = HttpMethod.POST;
  73. client.Request.Data = body;
  74. }
  75. var ex = client.Send();
  76. return ex == null ? Return(client.Response.Data, succeed, failed) : Return(ex.Message);
  77. }
  78. private string MergeUrl(string application = null, string function = null, IEnumerable<KeyValuePair<string, string>> args = null)
  79. {
  80. var ts = new TextSet(true);
  81. ts["application"] = application;
  82. ts["function"] = function;
  83. if (Random.NotEmpty()) ts["random"] = Random;
  84. if (Page.NotEmpty()) ts["page"] = Random;
  85. if (Session.NotEmpty()) ts["session"] = Random;
  86. if (Ticket.NotEmpty()) ts["ticket"] = Random;
  87. if (args != null)
  88. {
  89. foreach (var i in args)
  90. {
  91. if (i.Key.IsEmpty()) continue;
  92. if (i.Key == "application") continue;
  93. if (i.Key == "function") continue;
  94. if (i.Value.IsEmpty()) continue;
  95. ts[i.Key] = i.Value;
  96. }
  97. }
  98. var url = TextUtility.AssureEnds(ApiUrl, "/");
  99. var query = HttpClient.MergeForm(ts);
  100. if (query.NotEmpty()) url = url.Append("?", query);
  101. return url;
  102. }
  103. private Json MergePost(string application = null, string function = null, Json data = null)
  104. {
  105. var json = Json.NewObject();
  106. json.SetProperty("application", application);
  107. json.SetProperty("function", function);
  108. json.SetProperty("random", Random);
  109. json.SetProperty("page", Page);
  110. json.SetProperty("session", Session);
  111. json.SetProperty("ticket", Ticket);
  112. json.SetProperty("data", data);
  113. return json;
  114. }
  115. // GET
  116. /// <summary></summary>
  117. public Result<Json> Get(string application = null, string function = null, IEnumerable<KeyValuePair<string, string>> args = null, Action<Json> succeed = null, Action<string> failed = null)
  118. {
  119. var url = MergeUrl(application, function, args);
  120. return Send(url, null, succeed, failed, Timeout);
  121. }
  122. /// <summary></summary>
  123. public Result<Json> Get(string application = null, string function = null, TextSet args = null, Action<Json> succeed = null, Action<string> failed = null)
  124. {
  125. var url = MergeUrl(application, function, args == null ? null : args.Origin);
  126. return Send(url, null, succeed, failed, Timeout);
  127. }
  128. // POST
  129. /// <summary></summary>
  130. public Result<Json> Post(string application = null, string function = null, IEnumerable<KeyValuePair<string, string>> args = null, byte[] body = null, Action<Json> succeed = null, Action<string> failed = null)
  131. {
  132. var url = MergeUrl(application, function, args);
  133. return Send(url, body, succeed, failed, Timeout);
  134. }
  135. /// <summary></summary>
  136. public Result<Json> Post(string application = null, string function = null, TextSet args = null, byte[] body = null, Action<Json> succeed = null, Action<string> failed = null)
  137. {
  138. var url = MergeUrl(application, function, args == null ? null : args.Origin);
  139. return Send(url, body, succeed, failed, Timeout);
  140. }
  141. /// <summary></summary>
  142. public Result<Json> Post(string application = null, string function = null, Json data = null, Action<Json> succeed = null, Action<string> failed = null)
  143. {
  144. var json = MergePost(application, function, data);
  145. var bytes = json.ToString().Bytes();
  146. return Send(ApiUrl, bytes, succeed, failed, Timeout);
  147. }
  148. }
  149. }