http.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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> upload<T>(string url, string filePath)
  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. if (!string.IsNullOrEmpty(config.token))
  88. {
  89. req.AddHeader("Authorization", "Bearer " + config.token);
  90. }
  91. req.AddFile("file", filePath);
  92. req.AlwaysMultipartFormData = true;
  93. req.AddHeader("Content-Type", "multipart/form-data");
  94. try
  95. {
  96. var res = await getClient().ExecutePostAsync(req);
  97. if (res.StatusCode == HttpStatusCode.OK)
  98. {
  99. if (typeof(T) == typeof(string))
  100. {
  101. resolve((T)Convert.ChangeType(res.Content, typeof(T)));
  102. }
  103. else
  104. {
  105. resolve(JsonConvert.DeserializeObject<T>(res.Content));
  106. }
  107. }
  108. else
  109. {
  110. var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
  111. reject(new Exception(resData["error"]));
  112. }
  113. }
  114. catch (Exception e)
  115. {
  116. reject(e);
  117. }
  118. });
  119. }
  120. public static Promise<T> get<T>(string url, Dictionary<string, string> data = null)
  121. {
  122. return new Promise<T>(async (resolve, reject) =>
  123. {
  124. Config config = Config.getInstance();
  125. RestClient client = getClient();
  126. client.BaseUrl = new Uri(config.url);
  127. var req = new RestRequest(url);
  128. if (!string.IsNullOrEmpty(config.token))
  129. {
  130. req.AddHeader("Authorization", "Bearer " + config.token);
  131. }
  132. if (data != null)
  133. {
  134. foreach (KeyValuePair<string, string> entry in data)
  135. {
  136. if(entry.Key == "query")
  137. {
  138. req.AddQueryParameter(entry.Key, entry.Value, false);
  139. }
  140. else
  141. {
  142. req.AddQueryParameter(entry.Key, entry.Value, true);
  143. }
  144. }
  145. }
  146. try
  147. {
  148. var res = await getClient().ExecuteGetAsync(req);
  149. if (res.StatusCode == HttpStatusCode.OK)
  150. {
  151. if (typeof(T) == typeof(string))
  152. {
  153. resolve((T)Convert.ChangeType(res.Content, typeof(T)));
  154. }
  155. else
  156. {
  157. resolve(JsonConvert.DeserializeObject<T>(res.Content));
  158. }
  159. }
  160. else
  161. {
  162. var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
  163. reject(new Exception(resData["error"]));
  164. }
  165. }
  166. catch (Exception e)
  167. {
  168. reject(e);
  169. }
  170. });
  171. }
  172. }
  173. }