| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using DeviceCenter.model;
- using Newtonsoft.Json;
- using RestSharp;
- using RSG;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace DeviceCenter.utils
- {
- class http
- {
- private static readonly object _lock = new object();
- private static RestClient _Client;
- public static RestClient getClient()
- {
- if (null == _Client)
- {
- lock (_lock)
- {
- if (null == _Client)
- {
- Config config = Config.getInstance();
- _Client = new RestClient(config.url);
- }
- }
- }
- return _Client;
- }
- public static Promise<T> post<T>(string url, Dictionary<string, string> data = null)
- {
- return new Promise<T>(async (resolve, reject) =>
- {
- Config config = Config.getInstance();
- RestClient client = getClient();
- client.BaseUrl = new Uri(config.url);
- var req = new RestRequest(url);
- if (!string.IsNullOrEmpty(config.token))
- {
- req.AddHeader("Authorization", "Bearer " + config.token);
- }
- if (data != null)
- {
- foreach (KeyValuePair<string, string> entry in data)
- {
- req.AddParameter(entry.Key, entry.Value);
- }
- }
- try
- {
- var res = await getClient().ExecutePostAsync(req);
- if (res.StatusCode == HttpStatusCode.OK)
- {
- if (typeof(T) == typeof(string))
- {
- resolve((T)Convert.ChangeType(res.Content, typeof(T)));
- }
- else
- {
- resolve(JsonConvert.DeserializeObject<T>(res.Content));
- }
- }
- else
- {
- var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
- reject(new Exception(resData["error"]));
- }
- }
- catch (Exception e)
- {
- reject(e);
- }
- });
- }
- public static Promise<T> upload<T>(string url, string filePath)
- {
- return new Promise<T>(async (resolve, reject) =>
- {
- Config config = Config.getInstance();
- RestClient client = getClient();
- client.BaseUrl = new Uri(config.url);
- var req = new RestRequest(url);
- if (!string.IsNullOrEmpty(config.token))
- {
- req.AddHeader("Authorization", "Bearer " + config.token);
- }
- req.AddFile("file", filePath);
- req.AlwaysMultipartFormData = true;
- req.AddHeader("Content-Type", "multipart/form-data");
- try
- {
- var res = await getClient().ExecutePostAsync(req);
- if (res.StatusCode == HttpStatusCode.OK)
- {
- if (typeof(T) == typeof(string))
- {
- resolve((T)Convert.ChangeType(res.Content, typeof(T)));
- }
- else
- {
- resolve(JsonConvert.DeserializeObject<T>(res.Content));
- }
- }
- else
- {
- var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
- reject(new Exception(resData["error"]));
- }
- }
- catch (Exception e)
- {
- reject(e);
- }
- });
- }
- public static Promise<T> get<T>(string url, Dictionary<string, string> data = null)
- {
- return new Promise<T>(async (resolve, reject) =>
- {
- Config config = Config.getInstance();
- RestClient client = getClient();
- client.BaseUrl = new Uri(config.url);
- var req = new RestRequest(url);
- if (!string.IsNullOrEmpty(config.token))
- {
- req.AddHeader("Authorization", "Bearer " + config.token);
- }
- if (data != null)
- {
- foreach (KeyValuePair<string, string> entry in data)
- {
- if(entry.Key == "query")
- {
- req.AddQueryParameter(entry.Key, entry.Value, false);
- }
- else
- {
- req.AddQueryParameter(entry.Key, entry.Value, true);
- }
- }
- }
- try
- {
- var res = await getClient().ExecuteGetAsync(req);
- if (res.StatusCode == HttpStatusCode.OK)
- {
- if (typeof(T) == typeof(string))
- {
- resolve((T)Convert.ChangeType(res.Content, typeof(T)));
- }
- else
- {
- resolve(JsonConvert.DeserializeObject<T>(res.Content));
- }
- }
- else
- {
- var resData = JsonConvert.DeserializeObject<Dictionary<string, string>>(res.Content);
- reject(new Exception(resData["error"]));
- }
- }
- catch (Exception e)
- {
- reject(e);
- }
- });
- }
- }
- }
|