Radarr/src/NzbDrone.Api/ProviderModuleBase.cs

180 lines
6.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2013-09-24 23:42:55 +00:00
using System.Linq;
using FluentValidation;
using Nancy;
using NzbDrone.Api.ClientSchema;
using NzbDrone.Api.Extensions;
using NzbDrone.Api.Mapping;
using NzbDrone.Common.Reflection;
using NzbDrone.Core.ThingiProvider;
using Omu.ValueInjecter;
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;
Get["schema"] = x => GetTemplates();
Post["test"] = x => Test(ReadResourceFromRequest());
2013-09-24 23:42:55 +00:00
GetResourceAll = GetAll;
GetResourceById = GetProviderById;
CreateResource = CreateProvider;
UpdateResource = UpdateProvider;
DeleteResource = DeleteProvider;
SharedValidator.RuleFor(c => c.Name).NotEmpty();
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();
PostValidator.RuleFor(c => c.Fields).NotNull();
2013-09-24 23:42:55 +00:00
}
private TProviderResource GetProviderById(int id)
{
var definition = _providerFactory.Get(id);
var resource = definition.InjectTo<TProviderResource>();
resource.InjectFrom(_providerFactory.GetProviderCharacteristics(_providerFactory.GetInstance(definition), definition));
return resource;
2013-09-24 23:42:55 +00:00
}
private List<TProviderResource> GetAll()
{
2014-01-08 01:29:43 +00:00
var providerDefinitions = _providerFactory.All();
2013-09-24 23:42:55 +00:00
2014-01-08 01:29:43 +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
{
2014-01-08 01:29:43 +00:00
var providerResource = new TProviderResource();
providerResource.InjectFrom(definition);
providerResource.InjectFrom(_providerFactory.GetProviderCharacteristics(_providerFactory.GetInstance(definition), definition));
2014-01-08 01:29:43 +00:00
providerResource.Fields = SchemaBuilder.ToSchema(definition.Settings);
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
}
return result;
}
2014-01-08 01:29:43 +00:00
private int CreateProvider(TProviderResource providerResource)
2013-09-24 23:42:55 +00:00
{
var providerDefinition = GetDefinition(providerResource);
if (providerDefinition.Enable)
{
Test(providerDefinition);
}
providerDefinition = _providerFactory.Create(providerDefinition);
return providerDefinition.Id;
2013-09-24 23:42:55 +00:00
}
private void UpdateProvider(TProviderResource providerResource)
2013-09-24 23:42:55 +00:00
{
var providerDefinition = GetDefinition(providerResource);
2013-09-24 23:42:55 +00:00
_providerFactory.Update(providerDefinition);
2013-09-24 23:42:55 +00:00
}
private TProviderDefinition GetDefinition(TProviderResource providerResource)
2013-09-24 23:42:55 +00:00
{
var definition = new TProviderDefinition();
definition.InjectFrom(providerResource);
2013-09-24 23:42:55 +00:00
var preset = _providerFactory.GetPresetDefinitions(definition)
.Where(v => v.Name == definition.Name)
.Select(v => v.Settings)
.FirstOrDefault();
2013-09-24 23:42:55 +00:00
var configContract = ReflectionExtensions.CoreAssembly.FindTypeByName(definition.ConfigContract);
definition.Settings = (IProviderConfig)SchemaBuilder.ReadFormSchema(providerResource.Fields, configContract, preset);
2013-09-24 23:42:55 +00:00
Validate(definition);
2013-09-24 23:42:55 +00:00
return definition;
}
private void DeleteProvider(int id)
{
_providerFactory.Delete(id);
}
private Response GetTemplates()
{
var defaultDefinitions = _providerFactory.GetDefaultDefinitions().ToList();
2013-09-24 23:42:55 +00:00
var result = new List<TProviderResource>(defaultDefinitions.Count());
2013-09-24 23:42:55 +00:00
foreach (var providerDefinition in defaultDefinitions)
2013-09-24 23:42:55 +00:00
{
var providerResource = new TProviderResource();
providerResource.InjectFrom(providerDefinition);
providerResource.Fields = SchemaBuilder.ToSchema(providerDefinition.Settings);
providerResource.InfoLink = String.Format("https://github.com/NzbDrone/NzbDrone/wiki/Supported-{0}#{1}",
typeof(TProviderResource).Name.Replace("Resource", "s"),
providerDefinition.Implementation.ToLower());
var presetDefinitions = _providerFactory.GetPresetDefinitions(providerDefinition);
providerResource.Presets = presetDefinitions.Select(v =>
{
var presetResource = new TProviderResource();
presetResource.InjectFrom(v);
presetResource.Fields = SchemaBuilder.ToSchema(v.Settings);
return presetResource as ProviderResource;
}).ToList();
2013-09-24 23:42:55 +00:00
result.Add(providerResource);
2013-09-24 23:42:55 +00:00
}
return result.AsResponse();
}
private Response Test(TProviderResource providerResource)
{
var providerDefinition = GetDefinition(providerResource);
Test(providerDefinition);
return "{}";
}
private void Test(TProviderDefinition providerDefinition)
{
var result = _providerFactory.Test(providerDefinition);
if (!result.IsValid)
{
throw new ValidationException(result.Errors);
}
}
protected virtual void Validate(TProviderDefinition definition)
{
var validationResult = definition.Settings.Validate();
if (!validationResult.IsValid)
{
throw new ValidationException(validationResult.Errors);
}
}
2013-09-24 23:42:55 +00:00
}
}