121 lines
4.3 KiB
C#
121 lines
4.3 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;
|
|
|
|
public class HttpClient : IHttpClient
|
|
{
|
|
private static readonly System.Net.Http.HttpClient http = new System.Net.Http.HttpClient();
|
|
|
|
private Uri baseURL;
|
|
|
|
public HttpClient()
|
|
{
|
|
}
|
|
|
|
public HttpClient(String baseURL)
|
|
=> this.BaseURL(baseURL);
|
|
|
|
public void BaseURL(String baseURL)
|
|
=> this.baseURL = new Uri(baseURL, UriKind.Absolute);
|
|
|
|
public HttpResponse<T> DELETE<T>(String url, String bearer = null)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Delete, url, bearer: bearer);
|
|
|
|
public HttpResponse<T> DELETE<T>(String url)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Delete, url);
|
|
|
|
public HttpResponse<T> GET<T>(String url, String bearer = null)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Get, url, bearer: bearer);
|
|
|
|
public HttpResponse<T> GET<T>(String url)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Get, url);
|
|
|
|
public HttpResponse<T> POST<T>(String url, String bearer = null, Object body = null)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, url, bearer: bearer, body: body);
|
|
|
|
public HttpResponse<T> POST<T>(String url)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, url);
|
|
|
|
public HttpResponse<T> POST<T>(String url, Object body)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, url, body: body);
|
|
|
|
public HttpResponse<T> POST<T>(String url, String bearer)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, url, bearer: bearer);
|
|
|
|
public HttpResponse<T> PUT<T>(String url, String bearer = null, Object body = null)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Put, url, bearer: bearer, body: body);
|
|
|
|
public HttpResponse<T> PUT<T>(String url)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Post, url);
|
|
|
|
public HttpResponse<T> PUT<T>(String url, Object body)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Put, url, body: body);
|
|
|
|
public HttpResponse<T> PUT<T>(String url, String bearer)
|
|
=> this.HttpRequestHandler<T>(HttpMethod.Put, url, bearer: bearer);
|
|
|
|
private HttpResponse<T> HttpRequestHandler<T>(HttpMethod method, String url, String bearer = null, Object body = null)
|
|
{
|
|
var result = default(HttpResponse<T>);
|
|
var uri = default(Uri);
|
|
|
|
try
|
|
{
|
|
uri = new Uri(this.baseURL, new Uri(url, UriKind.Relative));
|
|
}
|
|
catch (Exception)
|
|
{
|
|
uri = new Uri(url);
|
|
}
|
|
|
|
try
|
|
{
|
|
using (var request = new HttpRequestMessage(method, uri))
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
result = e;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|