Update GlobalInstanceOptionsValidator.cs

Cleanup Code
This commit is contained in:
Felix Glang
2024-04-29 20:35:18 +02:00
parent 5931fd6a8a
commit ef7182888b

View File

@@ -1,9 +1,9 @@
using FluentValidation; using System.Net;
using System.Net; using FluentValidation;
using UmlautAdaptarr.Options.ArrOptions.InstanceOptions; using UmlautAdaptarr.Options.ArrOptions.InstanceOptions;
namespace UmlautAdaptarr.Validator namespace UmlautAdaptarr.Validator;
{
public class GlobalInstanceOptionsValidator : AbstractValidator<GlobalInstanceOptions> public class GlobalInstanceOptionsValidator : AbstractValidator<GlobalInstanceOptions>
{ {
public GlobalInstanceOptionsValidator() public GlobalInstanceOptionsValidator()
@@ -12,11 +12,11 @@ namespace UmlautAdaptarr.Validator
When(x => x.Enabled, () => When(x => x.Enabled, () =>
{ {
RuleFor(x => x.Host) RuleFor(x => x.Host)
.NotEmpty().WithMessage("Host is required when Enabled is true.") .NotEmpty().WithMessage("Host is required when Enabled is true.")
.Must(BeAValidUrl).WithMessage("Host/Url must start with http:// or https:// and be a valid address.") .Must(BeAValidUrl).WithMessage("Host/Url must start with http:// or https:// and be a valid address.")
.Must(BeReachable).WithMessage("Host/Url is not reachable. Please check your Host or your UmlautAdaptrr Settings"); .Must(BeReachable)
.WithMessage("Host/Url is not reachable. Please check your Host or your UmlautAdaptrr Settings");
RuleFor(x => x.ApiKey) RuleFor(x => x.ApiKey)
.NotEmpty().WithMessage("ApiKey is required when Enabled is true."); .NotEmpty().WithMessage("ApiKey is required when Enabled is true.");
@@ -31,35 +31,29 @@ namespace UmlautAdaptarr.Validator
private static bool BeReachable(string url) private static bool BeReachable(string url)
{ {
DateTime endTime = DateTime.Now.AddMinutes(3); var endTime = DateTime.Now.AddMinutes(3);
bool reachable = false; var reachable = false;
var request = WebRequest.Create(url); var request = WebRequest.Create(url);
while (DateTime.Now < endTime) while (DateTime.Now < endTime)
{ {
try try
{ {
var response = (HttpWebResponse)request.GetResponse(); var response = (HttpWebResponse)request.GetResponse();
reachable = response.StatusCode == HttpStatusCode.OK; reachable = response.StatusCode == HttpStatusCode.OK;
if (reachable) if (reachable)
{
break; break;
}
else
{
Console.WriteLine($"The URL \"{url}\" is not reachable. Next attempt in 15 seconds..."); Console.WriteLine($"The URL \"{url}\" is not reachable. Next attempt in 15 seconds...");
} }
}
catch catch
{ {
return false; return false;
} }
// Wait for 15 seconds // Wait for 15 seconds
System.Threading.Thread.Sleep(15000); Thread.Sleep(15000);
} }
return reachable; return reachable;
} }
} }
}