using DamageAssesment.Api.Responses.Interfaces; using DamageAssesment.Api.Responses.Models; using System.Net.Http.Headers; using System.Text; namespace DamageAssesment.Api.Responses.Services { public class HttpUtil : IHttpUtil { private readonly HttpClient httpClient; private readonly ILogger logger; public HttpUtil(HttpClient httpClient, ILogger logger) { this.httpClient = httpClient; this.logger = logger; } public async Task SendAsync(HttpMethod method, string url, string JsonInput, string token) { try { var request = new HttpRequestMessage(method, url); request.Headers.Accept.Clear(); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); 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; } } } }