http.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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> get<T>(string url, Dictionary<string, string> data = null)
  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. if (data != null)
  92. {
  93. foreach (KeyValuePair<string, string> entry in data)
  94. {
  95. if(entry.Key == "query")
  96. {
  97. req.AddQueryParameter(entry.Key, entry.Value, false);
  98. }
  99. else
  100. {
  101. req.AddQueryParameter(entry.Key, entry.Value, true);
  102. }
  103. }
  104. }
  105. try
  106. {
  107. var res = await getClient().ExecuteGetAsync(req);
  108. if (res.StatusCode == HttpStatusCode.OK)
  109. {
  110. if (typeof(T) == typeof(string))
  111. {
  112. resolve((T)Convert.ChangeType(res.Content, typeof(T)));
  113. }
  114. else
  115. {
  116. resolve(JsonConvert.DeserializeObject<T>(res.Content));
  117. }
  118. }
  119. else
  120. {
  121. var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
  122. reject(new Exception(resData["error"]));
  123. }
  124. }
  125. catch (Exception e)
  126. {
  127. reject(e);
  128. }
  129. });
  130. }
  131. }
  132. }