2014-06-19 22:56:49 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-09-24 23:42:55 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using FluentValidation;
|
2015-02-17 23:43:33 +00:00
|
|
|
|
using FluentValidation.Results;
|
2013-09-24 23:42:55 +00:00
|
|
|
|
using Nancy;
|
|
|
|
|
using NzbDrone.Api.ClientSchema;
|
|
|
|
|
using NzbDrone.Api.Extensions;
|
|
|
|
|
using NzbDrone.Common.Reflection;
|
|
|
|
|
using NzbDrone.Core.ThingiProvider;
|
2015-02-17 23:43:33 +00:00
|
|
|
|
using NzbDrone.Core.Validation;
|
2015-03-29 05:30:58 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2013-09-24 23:42:55 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api
|
|
|
|
|
{
|
|
|
|
|
public abstract class ProviderModuleBase<TProviderResource, TProvider, TProviderDefinition> : NzbDroneRestModule<TProviderResource>
|
|
|
|
|
where TProviderDefinition : ProviderDefinition, new()
|
|
|
|
|
where TProvider : IProvider
|
|
|
|
|
where TProviderResource : ProviderResource, new()
|
|
|
|
|
{
|
|
|
|
|
private readonly IProviderFactory<TProvider, TProviderDefinition> _providerFactory;
|
|
|
|
|
|
2013-10-01 17:27:22 +00:00
|
|
|
|
protected ProviderModuleBase(IProviderFactory<TProvider, TProviderDefinition> providerFactory, string resource)
|
|
|
|
|
: base(resource)
|
2013-09-24 23:42:55 +00:00
|
|
|
|
{
|
|
|
|
|
_providerFactory = providerFactory;
|
2014-07-04 08:09:48 +00:00
|
|
|
|
|
2014-06-19 22:56:49 +00:00
|
|
|
|
Get["schema"] = x => GetTemplates();
|
2015-03-29 05:30:58 +00:00
|
|
|
|
Post["test"] = x => Test(ReadResourceFromRequest(true));
|
2016-08-10 18:45:48 +00:00
|
|
|
|
Post["action/{action}"] = x => RequestAction(x.action, ReadResourceFromRequest(true));
|
2014-07-04 08:09:48 +00:00
|
|
|
|
|
2013-09-24 23:42:55 +00:00
|
|
|
|
GetResourceAll = GetAll;
|
|
|
|
|
GetResourceById = GetProviderById;
|
|
|
|
|
CreateResource = CreateProvider;
|
|
|
|
|
UpdateResource = UpdateProvider;
|
|
|
|
|
DeleteResource = DeleteProvider;
|
|
|
|
|
|
|
|
|
|
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
2014-04-19 15:09:22 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.Name).Must((v,c) => !_providerFactory.All().Any(p => p.Name == c && p.Id != v.Id)).WithMessage("Should be unique");
|
2013-09-24 23:42:55 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
|
|
|
|
|
SharedValidator.RuleFor(c => c.ConfigContract).NotEmpty();
|
|
|
|
|
|
2014-04-19 15:09:22 +00:00
|
|
|
|
PostValidator.RuleFor(c => c.Fields).NotNull();
|
2013-09-24 23:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TProviderResource GetProviderById(int id)
|
|
|
|
|
{
|
2014-08-18 02:25:00 +00:00
|
|
|
|
var definition = _providerFactory.Get(id);
|
2016-03-25 00:56:29 +00:00
|
|
|
|
_providerFactory.SetProviderCharacteristics(definition);
|
2014-08-18 02:25:00 +00:00
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
|
var resource = new TProviderResource();
|
|
|
|
|
MapToResource(resource, definition);
|
2014-08-18 02:25:00 +00:00
|
|
|
|
|
|
|
|
|
return resource;
|
2013-09-24 23:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<TProviderResource> GetAll()
|
|
|
|
|
{
|
2015-04-25 16:02:17 +00:00
|
|
|
|
var providerDefinitions = _providerFactory.All().OrderBy(p => p.ImplementationName);
|
2013-09-24 23:42:55 +00:00
|
|
|
|
|
2015-04-25 16:02:17 +00:00
|
|
|
|
var result = new List<TProviderResource>(providerDefinitions.Count());
|
2013-09-24 23:42:55 +00:00
|
|
|
|
|
2014-01-08 01:29:43 +00:00
|
|
|
|
foreach (var definition in providerDefinitions)
|
2013-09-24 23:42:55 +00:00
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
|
_providerFactory.SetProviderCharacteristics(definition);
|
|
|
|
|
|
2014-01-08 01:29:43 +00:00
|
|
|
|
var providerResource = new TProviderResource();
|
2016-03-25 00:56:29 +00:00
|
|
|
|
MapToResource(providerResource, definition);
|
2013-09-24 23:42:55 +00:00
|
|
|
|
|
2014-01-08 01:29:43 +00:00
|
|
|
|
result.Add(providerResource);
|
2013-09-24 23:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-26 07:25:40 +00:00
|
|
|
|
return result.OrderBy(p => p.Name).ToList();
|
2013-09-24 23:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-08 01:29:43 +00:00
|
|
|
|
private int CreateProvider(TProviderResource providerResource)
|
2013-09-24 23:42:55 +00:00
|
|
|
|
{
|
2015-02-17 23:43:33 +00:00
|
|
|
|
var providerDefinition = GetDefinition(providerResource, false);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
|
2015-04-19 05:56:36 +00:00
|
|
|
|
if (providerDefinition.Enable)
|
|
|
|
|
{
|
|
|
|
|
Test(providerDefinition, false);
|
|
|
|
|
}
|
2014-07-04 08:09:48 +00:00
|
|
|
|
|
|
|
|
|
providerDefinition = _providerFactory.Create(providerDefinition);
|
|
|
|
|
|
|
|
|
|
return providerDefinition.Id;
|
2013-09-24 23:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-10-12 18:44:40 +00:00
|
|
|
|
private void UpdateProvider(TProviderResource providerResource)
|
2013-09-24 23:42:55 +00:00
|
|
|
|
{
|
2015-02-17 23:43:33 +00:00
|
|
|
|
var providerDefinition = GetDefinition(providerResource, false);
|
|
|
|
|
|
2013-10-12 18:44:40 +00:00
|
|
|
|
_providerFactory.Update(providerDefinition);
|
2013-09-24 23:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 05:30:58 +00:00
|
|
|
|
private TProviderDefinition GetDefinition(TProviderResource providerResource, bool includeWarnings = false, bool validate = true)
|
2013-09-24 23:42:55 +00:00
|
|
|
|
{
|
|
|
|
|
var definition = new TProviderDefinition();
|
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
|
MapToModel(definition, providerResource);
|
2014-06-19 22:56:49 +00:00
|
|
|
|
|
2015-03-29 05:30:58 +00:00
|
|
|
|
if (validate)
|
|
|
|
|
{
|
|
|
|
|
Validate(definition, includeWarnings);
|
|
|
|
|
}
|
2013-09-24 23:42:55 +00:00
|
|
|
|
|
|
|
|
|
return definition;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 00:56:29 +00:00
|
|
|
|
protected virtual void MapToResource(TProviderResource resource, TProviderDefinition definition)
|
|
|
|
|
{
|
|
|
|
|
resource.Id = definition.Id;
|
|
|
|
|
|
|
|
|
|
resource.Name = definition.Name;
|
|
|
|
|
resource.ImplementationName = definition.ImplementationName;
|
|
|
|
|
resource.Implementation = definition.Implementation;
|
|
|
|
|
resource.ConfigContract = definition.ConfigContract;
|
|
|
|
|
resource.Message = definition.Message;
|
|
|
|
|
|
|
|
|
|
resource.Fields = SchemaBuilder.ToSchema(definition.Settings);
|
|
|
|
|
|
|
|
|
|
resource.InfoLink = string.Format("https://github.com/Sonarr/Sonarr/wiki/Supported-{0}#{1}",
|
|
|
|
|
typeof(TProviderResource).Name.Replace("Resource", "s"),
|
|
|
|
|
definition.Implementation.ToLower());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void MapToModel(TProviderDefinition definition, TProviderResource resource)
|
|
|
|
|
{
|
|
|
|
|
definition.Id = resource.Id;
|
|
|
|
|
|
|
|
|
|
definition.Name = resource.Name;
|
|
|
|
|
definition.ImplementationName = resource.ImplementationName;
|
|
|
|
|
definition.Implementation = resource.Implementation;
|
|
|
|
|
definition.ConfigContract = resource.ConfigContract;
|
|
|
|
|
definition.Message = resource.Message;
|
|
|
|
|
|
|
|
|
|
var configContract = ReflectionExtensions.CoreAssembly.FindTypeByName(definition.ConfigContract);
|
|
|
|
|
definition.Settings = (IProviderConfig)SchemaBuilder.ReadFromSchema(resource.Fields, configContract);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-24 23:42:55 +00:00
|
|
|
|
private void DeleteProvider(int id)
|
|
|
|
|
{
|
|
|
|
|
_providerFactory.Delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Response GetTemplates()
|
|
|
|
|
{
|
2015-04-25 16:02:17 +00:00
|
|
|
|
var defaultDefinitions = _providerFactory.GetDefaultDefinitions().OrderBy(p => p.ImplementationName).ToList();
|
2013-09-24 23:42:55 +00:00
|
|
|
|
|
2014-06-19 22:56:49 +00:00
|
|
|
|
var result = new List<TProviderResource>(defaultDefinitions.Count());
|
2013-09-24 23:42:55 +00:00
|
|
|
|
|
2014-06-19 22:56:49 +00:00
|
|
|
|
foreach (var providerDefinition in defaultDefinitions)
|
2013-09-24 23:42:55 +00:00
|
|
|
|
{
|
2013-10-12 18:44:40 +00:00
|
|
|
|
var providerResource = new TProviderResource();
|
2016-03-25 00:56:29 +00:00
|
|
|
|
MapToResource(providerResource, providerDefinition);
|
2014-06-19 22:56:49 +00:00
|
|
|
|
|
|
|
|
|
var presetDefinitions = _providerFactory.GetPresetDefinitions(providerDefinition);
|
|
|
|
|
|
|
|
|
|
providerResource.Presets = presetDefinitions.Select(v =>
|
|
|
|
|
{
|
|
|
|
|
var presetResource = new TProviderResource();
|
2016-03-25 00:56:29 +00:00
|
|
|
|
MapToResource(presetResource, v);
|
2014-06-19 22:56:49 +00:00
|
|
|
|
|
|
|
|
|
return presetResource as ProviderResource;
|
|
|
|
|
}).ToList();
|
2013-09-24 23:42:55 +00:00
|
|
|
|
|
2013-10-12 18:44:40 +00:00
|
|
|
|
result.Add(providerResource);
|
2013-09-24 23:42:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.AsResponse();
|
|
|
|
|
}
|
2013-10-12 18:44:40 +00:00
|
|
|
|
|
2014-07-04 08:09:48 +00:00
|
|
|
|
private Response Test(TProviderResource providerResource)
|
|
|
|
|
{
|
2016-03-18 22:42:19 +00:00
|
|
|
|
// Don't validate when getting the definition so we can validate afterwards (avoids validation being skipped because the provider is disabled)
|
|
|
|
|
var providerDefinition = GetDefinition(providerResource, true, false);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
|
2016-03-18 22:42:19 +00:00
|
|
|
|
Validate(providerDefinition, true);
|
2015-02-17 23:43:33 +00:00
|
|
|
|
Test(providerDefinition, true);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
|
|
|
|
|
return "{}";
|
2015-03-29 05:30:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-08-10 18:45:48 +00:00
|
|
|
|
private Response RequestAction(string action, TProviderResource providerResource)
|
2015-03-29 05:30:58 +00:00
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
|
var providerDefinition = GetDefinition(providerResource, true, false);
|
2015-03-29 05:30:58 +00:00
|
|
|
|
|
2016-08-10 18:45:48 +00:00
|
|
|
|
var query = ((IDictionary<string, object>)Request.Query.ToDictionary()).ToDictionary(k => k.Key, k => k.Value.ToString());
|
2015-03-29 05:30:58 +00:00
|
|
|
|
|
2016-08-10 18:45:48 +00:00
|
|
|
|
var data = _providerFactory.RequestAction(providerDefinition, action, query);
|
2015-03-29 05:30:58 +00:00
|
|
|
|
Response resp = JsonConvert.SerializeObject(data);
|
|
|
|
|
resp.ContentType = "application/json";
|
|
|
|
|
return resp;
|
2014-07-04 08:09:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 23:43:33 +00:00
|
|
|
|
protected virtual void Validate(TProviderDefinition definition, bool includeWarnings)
|
2014-07-04 08:09:48 +00:00
|
|
|
|
{
|
2015-02-17 23:43:33 +00:00
|
|
|
|
var validationResult = definition.Settings.Validate();
|
2014-07-04 08:09:48 +00:00
|
|
|
|
|
2015-02-17 23:43:33 +00:00
|
|
|
|
VerifyValidationResult(validationResult, includeWarnings);
|
2014-07-04 08:09:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 23:43:33 +00:00
|
|
|
|
protected virtual void Test(TProviderDefinition definition, bool includeWarnings)
|
2013-10-12 18:44:40 +00:00
|
|
|
|
{
|
2015-02-17 23:43:33 +00:00
|
|
|
|
var validationResult = _providerFactory.Test(definition);
|
2013-10-12 18:44:40 +00:00
|
|
|
|
|
2015-02-17 23:43:33 +00:00
|
|
|
|
VerifyValidationResult(validationResult, includeWarnings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void VerifyValidationResult(ValidationResult validationResult, bool includeWarnings)
|
|
|
|
|
{
|
|
|
|
|
var result = new NzbDroneValidationResult(validationResult.Errors);
|
|
|
|
|
|
|
|
|
|
if (includeWarnings && (!result.IsValid || result.HasWarnings))
|
2013-10-12 18:44:40 +00:00
|
|
|
|
{
|
2015-02-17 23:43:33 +00:00
|
|
|
|
throw new ValidationException(result.Failures);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!result.IsValid)
|
|
|
|
|
{
|
|
|
|
|
throw new ValidationException(result.Errors);
|
2013-10-12 18:44:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-24 23:42:55 +00:00
|
|
|
|
}
|
2014-07-04 08:09:48 +00:00
|
|
|
|
}
|