using System.Net; using System.Text; using Functions.Services.Interfaces; namespace Functions.Services; public class ExternalEndpointManager : IExternalEndpointManager { private readonly ILogger _logger; private readonly HttpClient _httpClient; public ExternalEndpointManager(ILogger logger, HttpClient httpClient) { _logger = logger; _httpClient = httpClient; } public async Task Get(string hostname) { try { // Send GET request to the API HttpResponseMessage response = await _httpClient.GetAsync($"http://{hostname}"); // Ensure the response was successful response.EnsureSuccessStatusCode(); // Display the response content return response; } catch (HttpRequestException ex) { _logger.LogError(ex.Message); // Handle any errors that occurred during the request return new HttpResponseMessage(HttpStatusCode.BadRequest); } } public async Task Post(string hostname, string body) { try { var content = new StringContent(body, Encoding.UTF8, "application/json"); // Send GET request to the API HttpResponseMessage response = await _httpClient.PostAsync($"http://{hostname}",content); // Ensure the response was successful response.EnsureSuccessStatusCode(); // Display the response content return response; } catch (HttpRequestException ex) { _logger.LogError(ex.Message); // Handle any errors that occurred during the request return new HttpResponseMessage(HttpStatusCode.BadRequest); } } public async Task Delete(string hostname) { try { // Send GET request to the API HttpResponseMessage response = await _httpClient.DeleteAsync($"http://{hostname}"); // Ensure the response was successful response.EnsureSuccessStatusCode(); // Display the response content return response; } catch (HttpRequestException ex) { _logger.LogError(ex.Message); // Handle any errors that occurred during the request return new HttpResponseMessage(HttpStatusCode.BadRequest); } } public async Task Patch(string hostname, string body) { try { var content = new StringContent(body, Encoding.UTF8, "application/json"); // Send GET request to the API HttpResponseMessage response = await _httpClient.PatchAsync($"http://{hostname}",content); // Ensure the response was successful response.EnsureSuccessStatusCode(); // Display the response content return response; } catch (HttpRequestException ex) { _logger.LogError(ex.Message); // Handle any errors that occurred during the request return new HttpResponseMessage(HttpStatusCode.BadRequest); } } public async Task Put(string hostname, string body) { try { var content = new StringContent(body, Encoding.UTF8, "application/json"); // Send GET request to the API HttpResponseMessage response = await _httpClient.PutAsync($"http://{hostname}",content); // Ensure the response was successful response.EnsureSuccessStatusCode(); // Display the response content return response; } catch (HttpRequestException ex) { _logger.LogError(ex.Message); // Handle any errors that occurred during the request return new HttpResponseMessage(HttpStatusCode.BadRequest); } } }