@ -1,4 +1,5 @@
using System.Collections.Generic ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Linq ;
using FluentValidation ;
using FluentValidation ;
using Nancy ;
using Nancy ;
@ -22,7 +23,7 @@ namespace NzbDrone.Api
: base ( resource )
: base ( resource )
{
{
_providerFactory = providerFactory ;
_providerFactory = providerFactory ;
Get [ " templates "] = x = > GetTemplates ( ) ;
Get [ " schema "] = x = > GetTemplates ( ) ;
GetResourceAll = GetAll ;
GetResourceAll = GetAll ;
GetResourceById = GetProviderById ;
GetResourceById = GetProviderById ;
CreateResource = CreateProvider ;
CreateResource = CreateProvider ;
@ -82,8 +83,13 @@ namespace NzbDrone.Api
definition . InjectFrom ( providerResource ) ;
definition . InjectFrom ( providerResource ) ;
var preset = _providerFactory . GetPresetDefinitions ( definition )
. Where ( v = > v . Name = = definition . Name )
. Select ( v = > v . Settings )
. FirstOrDefault ( ) ;
var configContract = ReflectionExtensions . CoreAssembly . FindTypeByName ( definition . ConfigContract ) ;
var configContract = ReflectionExtensions . CoreAssembly . FindTypeByName ( definition . ConfigContract ) ;
definition . Settings = ( IProviderConfig ) SchemaBuilder . ReadFormSchema ( providerResource . Fields , configContract ) ;
definition . Settings = ( IProviderConfig ) SchemaBuilder . ReadFormSchema ( providerResource . Fields , configContract , preset );
Validate ( definition ) ;
Validate ( definition ) ;
@ -97,15 +103,29 @@ namespace NzbDrone.Api
private Response GetTemplates ( )
private Response GetTemplates ( )
{
{
var templates = _providerFactory . Template s( ) ;
var defaultDefinitions = _providerFactory . GetDefaultDefinition s( ) ;
var result = new List < TProviderResource > ( template s. Count ( ) ) ;
var result = new List < TProviderResource > ( defaultDefinition s. Count ( ) ) ;
foreach ( var providerDefinition in template s)
foreach ( var providerDefinition in defaultDefinition s)
{
{
var providerResource = new TProviderResource ( ) ;
var providerResource = new TProviderResource ( ) ;
providerResource . InjectFrom ( providerDefinition ) ;
providerResource . InjectFrom ( providerDefinition ) ;
providerResource . Fields = SchemaBuilder . ToSchema ( providerDefinition . Settings ) ;
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 ( ) ;
result . Add ( providerResource ) ;
result . Add ( providerResource ) ;
}
}