50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
namespace LaaProduction.Http
|
|
{
|
|
using LaaProduction.Http.Interfaces;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
internal class HttpMessage : IHttpMessage
|
|
{
|
|
private const string Bearer = nameof(Bearer);
|
|
|
|
private readonly HttpClient fluentHttpClient;
|
|
private readonly HttpRequestMessage httpRequestMessage;
|
|
|
|
public HttpMessage(HttpClient fluentHttpClient, HttpMethod method, string route)
|
|
{
|
|
this.fluentHttpClient = fluentHttpClient;
|
|
this.httpRequestMessage = new HttpRequestMessage(method, route);
|
|
}
|
|
|
|
public void Dispose()
|
|
=> this.httpRequestMessage?.Dispose();
|
|
|
|
public IHttpMessage AuthorizeBearer(string parameter)
|
|
{
|
|
this.httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue(Bearer, parameter);
|
|
|
|
return this;
|
|
}
|
|
|
|
public Task<IHttpResponse<T>> SendAsync<T>()
|
|
=> this.fluentHttpClient.SendAsync<T>(this);
|
|
|
|
public IHttpMessage WhitJsonBody(object model)
|
|
{
|
|
var content = JsonConvert.SerializeObject(model);
|
|
|
|
this.httpRequestMessage.Content = new StringContent(content, Encoding.UTF8, "application/json");
|
|
|
|
return this;
|
|
}
|
|
|
|
public static implicit operator HttpRequestMessage(HttpMessage message)
|
|
=> message?.httpRequestMessage;
|
|
}
|
|
} |