1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using RestSharp;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace WCS.API.WebApiClient
- {
- public class RestApi<T> where T : class, new()
- {
- public T Get(string url, object pars, object body, out string content)
- {
- IRestResponse<T> reval = GetApiInfo(url, pars, body, Method.GET);
- content = reval.Content;
- return reval.Data;
- }
- public T Post(string url, object pars, object body, out string content)
- {
- IRestResponse<T> reval = GetApiInfo(url, pars, body, Method.POST);
- content = reval.Content;
- return reval.Data;
- }
- public T Delete(string url, object pars, object body, out string content)
- {
- IRestResponse<T> reval = GetApiInfo(url, pars, body, Method.DELETE);
- content = reval.Content;
- return reval.Data;
- }
- public T Put(string url, object pars, object body, out string content)
- {
- IRestResponse<T> reval = GetApiInfo(url, pars, body, Method.PUT);
- content = reval.Content;
- return reval.Data;
- }
- public IRestResponse<T> GetApiInfo(string url, object pars, object body, Method type)
- {
- var request = new RestRequest(type);
- if (pars!=null)
- {
- request.AddObject(pars);
- }
- if (body!=null)
- {
- request.AddJsonBody(body);
- }
- var client = new RestClient(url);
- client.AddHandler("text/plain", () => new RestSharp.Serialization.Json.JsonDeserializer());
- client.AddHandler("text/html", () => new RestSharp.Serialization.Json.JsonDeserializer());
- IRestResponse<T> reval = client.Execute<T>(request);
- if (reval.ErrorException!=null)
- {
- throw new Exception($"ÇëÇó³ö´í{url} {reval.ErrorMessage} {reval.Content}");
- }
- return reval;
- }
-
- }
- }
|