45 lines
1.5 KiB
C#
45 lines
1.5 KiB
C#
using System.Text.Json;
|
|
|
|
namespace MesaFabApproval.Client.Services;
|
|
|
|
public interface ICAService {
|
|
Task<bool> 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<bool> 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<bool>(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}");
|
|
}
|
|
}
|
|
}
|