2023-10-19 14:59:02 -05:00
|
|
|
using DamageAssesment.Api.Responses.Interfaces;
|
|
|
|
using DamageAssesment.Api.Responses.Models;
|
2023-09-04 20:31:41 -05:00
|
|
|
using System.Net.Http.Headers;
|
|
|
|
using System.Text;
|
|
|
|
|
2023-10-04 17:45:51 -05:00
|
|
|
namespace DamageAssesment.Api.Responses.Services
|
2023-09-04 20:31:41 -05:00
|
|
|
{
|
|
|
|
public class HttpUtil : IHttpUtil
|
|
|
|
{
|
|
|
|
private readonly HttpClient httpClient;
|
|
|
|
private readonly ILogger<HttpUtil> logger;
|
|
|
|
|
|
|
|
public HttpUtil(HttpClient httpClient, ILogger<HttpUtil> logger)
|
|
|
|
{
|
|
|
|
this.httpClient = httpClient;
|
|
|
|
this.logger = logger;
|
|
|
|
}
|
2023-10-19 14:59:02 -05:00
|
|
|
public async Task<string> SendAsync(HttpMethod method, string url, string JsonInput, string token)
|
2023-09-04 20:31:41 -05:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var request = new HttpRequestMessage(method, url);
|
|
|
|
request.Headers.Accept.Clear();
|
|
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
2023-10-19 14:59:02 -05:00
|
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
2023-09-04 20:31:41 -05:00
|
|
|
if (method == HttpMethod.Post)
|
|
|
|
{
|
|
|
|
request.Content = new StringContent(JsonInput, Encoding.UTF8, "application/json");
|
|
|
|
}
|
|
|
|
var response = await httpClient.SendAsync(request, CancellationToken.None);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var responseString = await response.Content.ReadAsStringAsync();
|
|
|
|
return responseString;
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
logger?.LogError($"Exception Message : {ex.Message} - Ref: HttpUtil.SendAsync()");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|