Replacement for JDIS Importer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

70 строки
2.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Net.Http;
  6. using System.Net.Http.Headers;
  7. using RestSharp;
  8. namespace jdis_import.RESTObjects
  9. {
  10. public class RESTService
  11. {
  12. private string _baseUrl = "http://servicesbrandt.rpmindustries.com:8081";
  13. private HttpClient _httpClient;
  14. public RESTService()
  15. {
  16. }
  17. public RESTService(string endPoint, Dictionary<string, string> parameters)
  18. {
  19. _baseUrl = _baseUrl + "/" + endPoint;
  20. foreach (KeyValuePair<string, string> kvp in parameters)
  21. {
  22. _baseUrl = _baseUrl + "/" + kvp.Key;
  23. if (!string.IsNullOrEmpty(kvp.Value))
  24. _baseUrl = _baseUrl + "/" + kvp.Value;
  25. }
  26. _httpClient = new HttpClient(new HttpClientHandler
  27. {
  28. UseCookies = true,
  29. CookieContainer = new System.Net.CookieContainer()
  30. })
  31. {
  32. BaseAddress = new Uri(_baseUrl),
  33. DefaultRequestHeaders = { Accept = { MediaTypeWithQualityHeaderValue.Parse("application/json") } }
  34. };
  35. }
  36. public string RestGet(string endpoint, Dictionary<string, string> parameters = null)
  37. {
  38. string url = _baseUrl + "/" + endpoint + '/';
  39. RestClient client = new RestClient(url);
  40. RestRequest request = new RestRequest(endpoint, Method.Get);
  41. request.AddHeader("Accept", "application/json");
  42. request.Method = Method.Get;
  43. RestResponse response = client.Execute(request);
  44. return response.Content;
  45. }
  46. public List<T> RestGet<T>(string endpoint)
  47. {
  48. RESTResponse<List<T>> rest = new RESTResponse<List<T>>();
  49. string url = _baseUrl + "/" + endpoint + "/";
  50. RestClient client = new RestClient(url);
  51. RestRequest request = new RestRequest();
  52. request.Method = Method.Get;
  53. request.AddHeader("Accept", "application/json");
  54. request.Method = Method.Get;
  55. RestResponse response = client.Execute(request);
  56. rest = Deserialize.FromJson<RESTResponse<List<T>>>(response.Content);
  57. List<T> list = new List<T>();
  58. list.AddRange(rest.data);
  59. return list;
  60. }
  61. }
  62. }