Update GlobalInstanceOptionsValidator.cs
Cleanup Code
This commit is contained in:
@@ -1,65 +1,59 @@
|
|||||||
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()
|
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
|
||||||
{
|
{
|
||||||
|
var response = (HttpWebResponse)request.GetResponse();
|
||||||
RuleFor(x => x.Host)
|
reachable = response.StatusCode == HttpStatusCode.OK;
|
||||||
.NotEmpty().WithMessage("Host is required when Enabled is true.")
|
if (reachable)
|
||||||
.Must(BeAValidUrl).WithMessage("Host/Url must start with http:// or https:// and be a valid address.")
|
break;
|
||||||
.Must(BeReachable).WithMessage("Host/Url is not reachable. Please check your Host or your UmlautAdaptrr Settings");
|
Console.WriteLine($"The URL \"{url}\" is not reachable. Next attempt in 15 seconds...");
|
||||||
|
}
|
||||||
RuleFor(x => x.ApiKey)
|
catch
|
||||||
.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)
|
|
||||||
{
|
{
|
||||||
try
|
return false;
|
||||||
{
|
|
||||||
|
|
||||||
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 reachable;
|
// Wait for 15 seconds
|
||||||
|
Thread.Sleep(15000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return reachable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user