http.cs 7.7 KB

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