diff --git a/UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs b/UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs index 70263d6..9ad27e4 100644 --- a/UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs +++ b/UmlautAdaptarr/Validator/GlobalInstanceOptionsValidator.cs @@ -1,65 +1,59 @@ -using FluentValidation; -using System.Net; +using System.Net; +using FluentValidation; using UmlautAdaptarr.Options.ArrOptions.InstanceOptions; -namespace UmlautAdaptarr.Validator +namespace UmlautAdaptarr.Validator; + +public class GlobalInstanceOptionsValidator : AbstractValidator { - public class GlobalInstanceOptionsValidator : AbstractValidator + public GlobalInstanceOptionsValidator() { - public GlobalInstanceOptionsValidator() - { - RuleFor(x => x.Enabled).NotNull(); + RuleFor(x => x.Enabled).NotNull(); - When(x => x.Enabled, () => + When(x => x.Enabled, () => + { + RuleFor(x => x.Host) + .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(BeReachable) + .WithMessage("Host/Url is not reachable. Please check your Host or your UmlautAdaptrr Settings"); + + RuleFor(x => x.ApiKey) + .NotEmpty().WithMessage("ApiKey is required when Enabled is true."); + }); + } + + private bool BeAValidUrl(string url) + { + return Uri.TryCreate(url, UriKind.Absolute, out var uriResult) + && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); + } + + private static bool BeReachable(string url) + { + var endTime = DateTime.Now.AddMinutes(3); + var reachable = false; + var request = WebRequest.Create(url); + + while (DateTime.Now < endTime) + { + try { - - RuleFor(x => x.Host) - .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(BeReachable).WithMessage("Host/Url is not reachable. Please check your Host or your UmlautAdaptrr Settings"); - - RuleFor(x => x.ApiKey) - .NotEmpty().WithMessage("ApiKey is required when Enabled is true."); - }); - } - - private bool BeAValidUrl(string url) - { - return Uri.TryCreate(url, UriKind.Absolute, out var uriResult) - && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); - } - - private static bool BeReachable(string url) - { - DateTime endTime = DateTime.Now.AddMinutes(3); - bool reachable = false; - var request = WebRequest.Create(url); - - while (DateTime.Now < endTime) + var response = (HttpWebResponse)request.GetResponse(); + reachable = response.StatusCode == HttpStatusCode.OK; + if (reachable) + break; + Console.WriteLine($"The URL \"{url}\" is not reachable. Next attempt in 15 seconds..."); + } + catch { - try - { - - var response = (HttpWebResponse)request.GetResponse(); - reachable = response.StatusCode == HttpStatusCode.OK; - if (reachable) - { - break; - } - else - { - Console.WriteLine($"The URL \"{url}\" is not reachable. Next attempt in 15 seconds..."); - } - } - catch - { - return false; - } - // Wait for 15 seconds - System.Threading.Thread.Sleep(15000); + return false; } - return reachable; + // Wait for 15 seconds + Thread.Sleep(15000); } + + return reachable; } -} +} \ No newline at end of file