http.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using DeviceCenter.model;
  2. using Newtonsoft.Json;
  3. using RestSharp;
  4. using RSG;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Input;
  13. namespace DeviceCenter.utils
  14. {
  15. class http
  16. {
  17. private static readonly object _lock = new object();
  18. private static RestClient _Client;
  19. public static RestClient getClient()
  20. {
  21. if (null == _Client)
  22. {
  23. lock (_lock)
  24. {
  25. if (null == _Client)
  26. {
  27. Config config = Config.getInstance();
  28. _Client = new RestClient(config.url);
  29. }
  30. }
  31. }
  32. return _Client;
  33. }
  34. public static Promise<T> post<T>(string url, Dictionary<string, string> data = null)
  35. {
  36. return new Promise<T>(async (resolve, reject) =>
  37. {
  38. Config config = Config.getInstance();
  39. RestClient client = getClient();
  40. client.BaseUrl = new Uri(config.url);
  41. var req = new RestRequest(url);
  42. if (!string.IsNullOrEmpty(config.token))
  43. {
  44. req.AddHeader("Authorization", "Bearer " + config.token);
  45. }
  46. if (data != null)
  47. {
  48. foreach (KeyValuePair<string, string> entry in data)
  49. {
  50. req.AddParameter(entry.Key, entry.Value);
  51. }
  52. }
  53. try
  54. {
  55. var res = await getClient().ExecutePostAsync(req);
  56. if (res.StatusCode == HttpStatusCode.OK)
  57. {
  58. if (typeof(T) == typeof(string))
  59. {
  60. resolve((T)Convert.ChangeType(res.Content, typeof(T)));
  61. }
  62. else
  63. {
  64. resolve(JsonConvert.DeserializeObject<T>(res.Content));
  65. }
  66. }
  67. else
  68. {
  69. var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
  70. reject(new Exception(resData["error"]));
  71. }
  72. }
  73. catch (Exception e)
  74. {
  75. reject(e);
  76. }
  77. });
  78. }
  79. public static Promise<T> postJson<T>(string url, object body)
  80. {
  81. return new Promise<T>(async (resolve, reject) =>
  82. {
  83. Config config = Config.getInstance();
  84. RestClient client = getClient();
  85. client.BaseUrl = new Uri(config.url);
  86. var req = new RestRequest(url);
  87. req.RequestFormat = DataFormat.Json;
  88. if (!string.IsNullOrEmpty(config.token))
  89. {
  90. req.AddHeader("Authorization", "Bearer " + config.token);
  91. }
  92. if (body != null)
  93. {
  94. req.AddBody(body);
  95. }
  96. try
  97. {
  98. var res = await getClient().ExecutePostAsync(req);
  99. if (res.StatusCode == HttpStatusCode.OK)
  100. {
  101. if (typeof(T) == typeof(string))
  102. {
  103. resolve((T)Convert.ChangeType(res.Content, typeof(T)));
  104. }
  105. else
  106. {
  107. resolve(JsonConvert.DeserializeObject<T>(res.Content));
  108. }
  109. }
  110. else
  111. {
  112. var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
  113. reject(new Exception(resData["error"]));
  114. }
  115. }
  116. catch (Exception e)
  117. {
  118. reject(e);
  119. }
  120. });
  121. }
  122. public static Promise<T> upload<T>(string url, string filePath)
  123. {
  124. return new Promise<T>(async (resolve, reject) =>
  125. {
  126. Config config = Config.getInstance();
  127. RestClient client = getClient();
  128. client.BaseUrl = new Uri(config.url);
  129. var req = new RestRequest(url);
  130. if (!string.IsNullOrEmpty(config.token))
  131. {
  132. req.AddHeader("Authorization", "Bearer " + config.token);
  133. }
  134. req.AddFile("file", filePath);
  135. req.AlwaysMultipartFormData = true;
  136. req.AddHeader("Content-Type", "multipart/form-data");
  137. try
  138. {
  139. var res = await getClient().ExecutePostAsync(req);
  140. if (res.StatusCode == HttpStatusCode.OK)
  141. {
  142. if (typeof(T) == typeof(string))
  143. {
  144. resolve((T)Convert.ChangeType(res.Content, typeof(T)));
  145. }
  146. else
  147. {
  148. resolve(JsonConvert.DeserializeObject<T>(res.Content));
  149. }
  150. }
  151. else
  152. {
  153. var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
  154. reject(new Exception(resData["error"]));
  155. }
  156. }
  157. catch (Exception e)
  158. {
  159. reject(e);
  160. }
  161. });
  162. }
  163. public static Promise<T> get<T>(string url, Dictionary<string, string> data = null)
  164. {
  165. return new Promise<T>(async (resolve, reject) =>
  166. {
  167. Config config = Config.getInstance();
  168. RestClient client = getClient();
  169. client.BaseUrl = new Uri(config.url);
  170. var req = new RestRequest(url);
  171. if (!string.IsNullOrEmpty(config.token))
  172. {
  173. req.AddHeader("Authorization", "Bearer " + config.token);
  174. }
  175. if (data != null)
  176. {
  177. foreach (KeyValuePair<string, string> entry in data)
  178. {
  179. if (entry.Key == "query")
  180. {
  181. req.AddQueryParameter(entry.Key, entry.Value, false);
  182. }
  183. else
  184. {
  185. req.AddQueryParameter(entry.Key, entry.Value, true);
  186. }
  187. }
  188. }
  189. try
  190. {
  191. var res = await getClient().ExecuteGetAsync(req);
  192. if (res.StatusCode == HttpStatusCode.OK)
  193. {
  194. if (typeof(T) == typeof(string))
  195. {
  196. resolve((T)Convert.ChangeType(res.Content, typeof(T)));
  197. }
  198. else
  199. {
  200. resolve(JsonConvert.DeserializeObject<T>(res.Content));
  201. }
  202. }
  203. else
  204. {
  205. var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
  206. reject(new Exception(resData["error"]));
  207. }
  208. }
  209. catch (Exception e)
  210. {
  211. reject(e);
  212. }
  213. });
  214. }
  215. }
  216. }