|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using RestSharp;
-
- namespace jdis_import.RESTObjects
- {
- public class RESTService
- {
- private string _baseUrl = "http://servicesbrandt.rpmindustries.com:8081";
- private HttpClient _httpClient;
-
- public RESTService()
- {
-
- }
- public RESTService(string endPoint, Dictionary<string, string> parameters)
- {
- _baseUrl = _baseUrl + "/" + endPoint;
- foreach (KeyValuePair<string, string> kvp in parameters)
- {
- _baseUrl = _baseUrl + "/" + kvp.Key;
- if (!string.IsNullOrEmpty(kvp.Value))
- _baseUrl = _baseUrl + "/" + kvp.Value;
- }
-
- _httpClient = new HttpClient(new HttpClientHandler
- {
- UseCookies = true,
- CookieContainer = new System.Net.CookieContainer()
- })
- {
- BaseAddress = new Uri(_baseUrl),
- DefaultRequestHeaders = { Accept = { MediaTypeWithQualityHeaderValue.Parse("application/json") } }
- };
- }
- public string RestGet(string endpoint, Dictionary<string, string> parameters = null)
- {
- string url = _baseUrl + "/" + endpoint + '/';
-
- RestClient client = new RestClient(url);
-
- RestRequest request = new RestRequest(endpoint, Method.Get);
- request.AddHeader("Accept", "application/json");
- request.Method = Method.Get;
- RestResponse response = client.Execute(request);
- return response.Content;
- }
- public List<T> RestGet<T>(string endpoint)
- {
- RESTResponse<List<T>> rest = new RESTResponse<List<T>>();
- string url = _baseUrl + "/" + endpoint + "/";
-
- RestClient client = new RestClient(url);
-
- RestRequest request = new RestRequest();
- request.Method = Method.Get;
- request.AddHeader("Accept", "application/json");
- request.Method = Method.Get;
- RestResponse response = client.Execute(request);
- rest = Deserialize.FromJson<RESTResponse<List<T>>>(response.Content);
- List<T> list = new List<T>();
- list.AddRange(rest.data);
- return list;
- }
- }
- }
|