From 4347e92bef1c04ceb93d039cc850846b7457000f Mon Sep 17 00:00:00 2001 From: Qstick Date: Tue, 16 Oct 2018 22:05:40 -0400 Subject: [PATCH] New: Add Test all functionality to API (Clients/Indexers/Lists) Co-Authored-By: Mark McDowall --- src/Lidarr.Api.V1/Lidarr.Api.V1.csproj | 1 + src/Lidarr.Api.V1/ProviderModuleBase.cs | 22 ++++++++++++++++++++++ src/Lidarr.Api.V1/ProviderTestAllResult.cs | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/Lidarr.Api.V1/ProviderTestAllResult.cs diff --git a/src/Lidarr.Api.V1/Lidarr.Api.V1.csproj b/src/Lidarr.Api.V1/Lidarr.Api.V1.csproj index 1fae358bb..c3aeb676b 100644 --- a/src/Lidarr.Api.V1/Lidarr.Api.V1.csproj +++ b/src/Lidarr.Api.V1/Lidarr.Api.V1.csproj @@ -107,6 +107,7 @@ + diff --git a/src/Lidarr.Api.V1/ProviderModuleBase.cs b/src/Lidarr.Api.V1/ProviderModuleBase.cs index 1b46644d7..42775f031 100644 --- a/src/Lidarr.Api.V1/ProviderModuleBase.cs +++ b/src/Lidarr.Api.V1/ProviderModuleBase.cs @@ -28,6 +28,7 @@ namespace Lidarr.Api.V1 Get["schema"] = x => GetTemplates(); Post["test"] = x => Test(ReadResourceFromRequest(true)); + Post["testall"] = x => TestAll(); Post["action/{action}"] = x => RequestAction(x.action, ReadResourceFromRequest(true)); GetResourceAll = GetAll; @@ -144,6 +145,27 @@ namespace Lidarr.Api.V1 return "{}"; } + private Response TestAll() + { + var providerDefinitions = _providerFactory.All() + .Where(c => c.Settings.Validate().IsValid && c.Enable) + .ToList(); + var result = new List(); + + foreach (var definition in providerDefinitions) + { + var validationResult = _providerFactory.Test(definition); + + result.Add(new ProviderTestAllResult + { + Id = definition.Id, + ValidationFailures = validationResult.Errors.ToList() + }); + } + + return result.AsResponse(result.Any(c => !c.IsValid) ? HttpStatusCode.BadRequest : HttpStatusCode.OK); + } + private Response RequestAction(string action, TProviderResource providerResource) { var providerDefinition = GetDefinition(providerResource, true, false); diff --git a/src/Lidarr.Api.V1/ProviderTestAllResult.cs b/src/Lidarr.Api.V1/ProviderTestAllResult.cs new file mode 100644 index 000000000..ad96a3796 --- /dev/null +++ b/src/Lidarr.Api.V1/ProviderTestAllResult.cs @@ -0,0 +1,18 @@ +using System.Collections.Generic; +using FluentValidation.Results; +using NzbDrone.Common.Extensions; + +namespace Lidarr.Api.V1 +{ + public class ProviderTestAllResult + { + public int Id { get; set; } + public bool IsValid => ValidationFailures.Empty(); + public List ValidationFailures { get; set; } + + public ProviderTestAllResult() + { + ValidationFailures = new List(); + } + } +}