您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

244 行
5.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using SUISS.Cloud.Boomlagoon.JSON;
  6. using UnityEngine;
  7. namespace SUISS.Cloud
  8. {
  9. public class ApiRequest : CustomYieldInstruction
  10. {
  11. private ApiRequest(string call)
  12. {
  13. this._request = new WWW(call);
  14. }
  15. private ApiRequest(string call, string json)
  16. {
  17. Dictionary<string, string> headers = new Dictionary<string, string>
  18. {
  19. {
  20. "Content-Type",
  21. "application/json"
  22. }
  23. };
  24. UTF8Encoding utf8Encoding = new UTF8Encoding();
  25. byte[] bytes = utf8Encoding.GetBytes(json);
  26. this._request = new WWW(call, bytes, headers);
  27. }
  28. public static ApiRequest Post(string call, JSONValue json)
  29. {
  30. return new ApiRequest(call, json.ToString());
  31. }
  32. public static ApiRequest Post(string call, IDictionary<string, object> json)
  33. {
  34. return new ApiRequest(call, Json.Encode(json));
  35. }
  36. public static ApiRequest Post(string call, JSONValue json, string v, string app)
  37. {
  38. call = string.Concat(new string[]
  39. {
  40. call,
  41. (!call.Contains("?")) ? "?" : "&",
  42. "v=",
  43. v,
  44. "&app=",
  45. app
  46. });
  47. return new ApiRequest(call, json.ToString());
  48. }
  49. public static ApiRequest Post(string call, Dictionary<string, object> json, string v, string app)
  50. {
  51. call = string.Concat(new string[]
  52. {
  53. call,
  54. (!call.Contains("?")) ? "?" : "&",
  55. "v=",
  56. v,
  57. "&app=",
  58. app
  59. });
  60. return new ApiRequest(call, Json.Encode(json));
  61. }
  62. public static ApiRequest Get(string call)
  63. {
  64. return new ApiRequest(call);
  65. }
  66. public static ApiRequest Get(string call, string v, string app)
  67. {
  68. call = string.Concat(new string[]
  69. {
  70. call,
  71. (!call.Contains("?")) ? "?" : "&",
  72. "v=",
  73. v,
  74. "&app=",
  75. app
  76. });
  77. return new ApiRequest(call);
  78. }
  79. public bool IsDone
  80. {
  81. get
  82. {
  83. this.ProcessResults();
  84. return this._resultsProcessed;
  85. }
  86. }
  87. public ApiResponse Result
  88. {
  89. get
  90. {
  91. this.ProcessResults();
  92. return this._result;
  93. }
  94. }
  95. public ApiError Error
  96. {
  97. get
  98. {
  99. this.ProcessResults();
  100. return this._error;
  101. }
  102. }
  103. public int HttpStatus
  104. {
  105. get
  106. {
  107. if (this._request.isDone)
  108. {
  109. if (this._request.responseHeaders.ContainsKey("STATUS"))
  110. {
  111. Match match = Regex.Match(this._request.responseHeaders["STATUS"], "HTTP\\/\\d.\\d\\s(\\d{1,3})");
  112. if (match.Success)
  113. {
  114. return int.Parse(match.Groups[1].Value);
  115. }
  116. }
  117. return 0;
  118. }
  119. return -1;
  120. }
  121. }
  122. public override bool keepWaiting
  123. {
  124. get
  125. {
  126. return !this._request.isDone;
  127. }
  128. }
  129. private void ProcessResults()
  130. {
  131. if (this._request.isDone && !this._resultsProcessed)
  132. {
  133. int httpStatus = this.HttpStatus;
  134. if (httpStatus <= 0)
  135. {
  136. this._error = new ApiError
  137. {
  138. Status = 0,
  139. Error = (this._request.error ?? "unknown error"),
  140. ErrorCode = 0,
  141. ServerError = false,
  142. NoInternet = true
  143. };
  144. }
  145. else if (200 <= httpStatus && httpStatus < 300 && !string.IsNullOrEmpty(this._request.text))
  146. {
  147. bool flag = false;
  148. object obj = Json.Decode(this._request.text, ref flag);
  149. if (!flag)
  150. {
  151. this._error = new ApiError
  152. {
  153. Status = 0,
  154. Error = "server returned invalid json",
  155. ErrorCode = 0,
  156. ServerError = true,
  157. NoInternet = false
  158. };
  159. }
  160. else
  161. {
  162. Dictionary<string, object> dictionary = obj as Dictionary<string, object>;
  163. if (dictionary != null && dictionary.ContainsKey("code") && dictionary.ContainsKey("message") && dictionary.ContainsKey("error"))
  164. {
  165. this._error = new ApiError
  166. {
  167. Status = this.GetJsonInt(obj, "code", 0),
  168. Error = this.GetJsonString(obj, "message", string.Empty),
  169. ErrorCode = this.GetJsonInt(obj, "error", 0),
  170. ServerError = false,
  171. NoInternet = false
  172. };
  173. }
  174. else
  175. {
  176. this._result = new ApiResponse(obj);
  177. }
  178. }
  179. }
  180. else
  181. {
  182. this._error = new ApiError
  183. {
  184. Status = httpStatus,
  185. Error = (string.IsNullOrEmpty(this._request.text) ? "server returned nothing" : this._request.text),
  186. ErrorCode = 0,
  187. ServerError = true,
  188. NoInternet = false
  189. };
  190. }
  191. this._resultsProcessed = true;
  192. }
  193. }
  194. private string GetJsonString(object json, string key, string defaultValue)
  195. {
  196. if (json is Dictionary<string, object>)
  197. {
  198. Dictionary<string, object> dictionary = (Dictionary<string, object>)json;
  199. if (dictionary.ContainsKey(key) && dictionary[key] is string)
  200. {
  201. return (string)dictionary[key];
  202. }
  203. }
  204. return defaultValue;
  205. }
  206. private int GetJsonInt(object json, string key, int defaultValue)
  207. {
  208. if (json is Dictionary<string, object>)
  209. {
  210. Dictionary<string, object> dictionary = (Dictionary<string, object>)json;
  211. if (dictionary.ContainsKey(key) && dictionary[key] is double)
  212. {
  213. return (int)((double)dictionary[key]);
  214. }
  215. }
  216. return defaultValue;
  217. }
  218. private WWW _request;
  219. private ApiError _error;
  220. private ApiResponse _result;
  221. private bool _resultsProcessed;
  222. }
  223. }