93 lines
3.8 KiB
C#
93 lines
3.8 KiB
C#
namespace LaaProduction.Http
|
|
{
|
|
using LaaProduction.Http.Interfaces;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Threading.Tasks;
|
|
|
|
public class HttpClient : IHttpClient
|
|
{
|
|
private static readonly System.Net.Http.HttpClient http = new System.Net.Http.HttpClient();
|
|
|
|
public HttpResponse<T> DELETE<T>(string pathAndQuery, string bearer = null)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Delete, pathAndQuery, bearer: bearer);
|
|
|
|
public HttpResponse<T> DELETE<T>(string pathAndQuery)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Delete, pathAndQuery);
|
|
|
|
public HttpResponse<T> GET<T>(string pathAndQuery, string bearer = null)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Get, pathAndQuery, bearer: bearer);
|
|
|
|
public HttpResponse<T> GET<T>(string pathAndQuery)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Get, pathAndQuery);
|
|
|
|
public HttpResponse<T> POST<T>(string pathAndQuery, string bearer = null, object body = null)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, pathAndQuery, bearer: bearer, body: body);
|
|
|
|
public HttpResponse<T> POST<T>(string pathAndQuery)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, pathAndQuery);
|
|
|
|
public HttpResponse<T> POST<T>(string pathAndQuery, object body)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, pathAndQuery, body: body);
|
|
|
|
public HttpResponse<T> POST<T>(string pathAndQuery, string bearer)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, pathAndQuery, bearer: bearer);
|
|
|
|
public HttpResponse<T> PUT<T>(string pathAndQuery, string bearer = null, object body = null)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Put, pathAndQuery, bearer: bearer, body: body);
|
|
|
|
public HttpResponse<T> PUT<T>(string pathAndQuery)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, pathAndQuery);
|
|
|
|
public HttpResponse<T> PUT<T>(string pathAndQuery, object body)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, pathAndQuery, body: body);
|
|
|
|
public HttpResponse<T> PUT<T>(string pathAndQuery, string bearer)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, pathAndQuery, bearer: bearer);
|
|
|
|
private HttpResponse<T> HttpRequestHandler<T>(HttpMethod method, string url, string bearer = null, object body = null)
|
|
{
|
|
var result = default(HttpResponse<T>);
|
|
|
|
using (var request = new HttpRequestMessage(method, new Uri(url)))
|
|
{
|
|
if (body != null)
|
|
{
|
|
request.Content = new StringContent(JsonConvert.SerializeObject(body));
|
|
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(bearer))
|
|
{
|
|
http.DefaultRequestHeaders
|
|
.Authorization = new AuthenticationHeaderValue("Bearer", bearer);
|
|
}
|
|
|
|
using (var response = http.SendAsync(request).Result)
|
|
{
|
|
using (var content = response.Content)
|
|
{
|
|
var text = content.ReadAsStringAsync().Result;
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
result = JsonConvert.DeserializeObject<T>(text);
|
|
}
|
|
else
|
|
{
|
|
result = JsonConvert.DeserializeObject<Dictionary<string, string>>(text);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|