using System.Text.Json; namespace MesaFabApproval.Client.Services; public interface ICAService { Task CANumberIsValid(int number); } public class CAService : ICAService { private readonly IHttpClientFactory _httpClientFactory; public CAService(IHttpClientFactory httpClientFactory) { _httpClientFactory = httpClientFactory ?? throw new ArgumentNullException("IHttpClientFactory not injected"); } public async Task CANumberIsValid(int number) { if (number <= 0) return false; try { HttpClient httpClient = _httpClientFactory.CreateClient("API"); HttpRequestMessage requestMessage = new(HttpMethod.Get, $"ca/isValidCANumber?number={number}"); HttpResponseMessage responseMessage = await httpClient.SendAsync(requestMessage); if (responseMessage.IsSuccessStatusCode) { string responseContent = await responseMessage.Content.ReadAsStringAsync(); JsonSerializerOptions jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true }; bool isValid = JsonSerializer.Deserialize(responseContent, jsonSerializerOptions); return isValid; } else { throw new Exception(responseMessage.ReasonPhrase); } } catch (Exception ex) { throw new Exception($"Unable to determine if {number} is a valid CA#, because {ex.Message}"); } } }