mirror of
https://github.com/Radarr/Radarr.git
synced 2024-11-09 04:22:30 +01:00
created ProviderModuleBase
This commit is contained in:
parent
4f9b745e70
commit
eaed756655
16
NzbDrone.Api/IndexerResource.cs
Normal file
16
NzbDrone.Api/IndexerResource.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Api.ClientSchema;
|
||||
using NzbDrone.Api.REST;
|
||||
|
||||
namespace NzbDrone.Api
|
||||
{
|
||||
public class ProviderResource : RestResource
|
||||
{
|
||||
public Boolean Enable { get; set; }
|
||||
public String Name { get; set; }
|
||||
public List<Field> Fields { get; set; }
|
||||
public String Implementation { get; set; }
|
||||
public String ConfigContract { get; set; }
|
||||
}
|
||||
}
|
@ -1,104 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Api.ClientSchema;
|
||||
using NzbDrone.Common.Reflection;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.ThingiProvider;
|
||||
using Omu.ValueInjecter;
|
||||
using FluentValidation;
|
||||
using NzbDrone.Api.Mapping;
|
||||
using NzbDrone.Core.Indexers;
|
||||
|
||||
namespace NzbDrone.Api.Indexers
|
||||
{
|
||||
public class IndexerModule : NzbDroneRestModule<IndexerResource>
|
||||
public class IndexerModule : ProviderModuleBase<ProviderResource, IIndexer, IndexerDefinition>
|
||||
{
|
||||
private readonly IIndexerService _indexerService;
|
||||
|
||||
public IndexerModule(IIndexerService indexerService)
|
||||
public IndexerModule(IndexerFactory indexerFactory)
|
||||
: base(indexerFactory)
|
||||
{
|
||||
_indexerService = indexerService;
|
||||
GetResourceAll = GetAll;
|
||||
GetResourceById = GetIndexer;
|
||||
CreateResource = CreateIndexer;
|
||||
UpdateResource = UpdateIndexer;
|
||||
DeleteResource = DeleteIndexer;
|
||||
|
||||
|
||||
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
||||
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
|
||||
|
||||
PostValidator.RuleFor(c => c.Fields).NotEmpty();
|
||||
}
|
||||
|
||||
private IndexerResource GetIndexer(int id)
|
||||
{
|
||||
return _indexerService.Get(id).InjectTo<IndexerResource>();
|
||||
}
|
||||
|
||||
private List<IndexerResource> GetAll()
|
||||
{
|
||||
var indexerDefinitions = _indexerService.All();
|
||||
|
||||
var result = new List<IndexerResource>(indexerDefinitions.Count);
|
||||
|
||||
foreach (var definition in indexerDefinitions)
|
||||
{
|
||||
var indexerResource = new IndexerResource();
|
||||
indexerResource.InjectFrom(definition);
|
||||
indexerResource.Fields = SchemaBuilder.ToSchema(definition.Settings);
|
||||
|
||||
result.Add(indexerResource);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private int CreateIndexer(IndexerResource indexerResource)
|
||||
{
|
||||
var indexer = GetDefinition(indexerResource);
|
||||
indexer = _indexerService.Create(indexer);
|
||||
return indexer.Id;
|
||||
}
|
||||
|
||||
private void UpdateIndexer(IndexerResource indexerResource)
|
||||
{
|
||||
var indexer = GetDefinition(indexerResource);
|
||||
|
||||
ValidateIndexer(indexer);
|
||||
|
||||
_indexerService.Update(indexer);
|
||||
}
|
||||
|
||||
|
||||
private static void ValidateIndexer(ProviderDefinition definition)
|
||||
{
|
||||
if (!definition.Enable) return;
|
||||
|
||||
var validationResult = definition.Settings.Validate();
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
throw new ValidationException(validationResult.Errors);
|
||||
}
|
||||
}
|
||||
|
||||
private IndexerDefinition GetDefinition(IndexerResource indexerResource)
|
||||
{
|
||||
|
||||
var definition = new IndexerDefinition();
|
||||
|
||||
definition.InjectFrom(indexerResource);
|
||||
|
||||
var configContract = ReflectionExtensions.CoreAssembly.FindTypeByName(definition.ConfigContract);
|
||||
definition.Settings = (IProviderConfig)SchemaBuilder.ReadFormSchema(indexerResource.Fields, configContract);
|
||||
|
||||
ValidateIndexer(definition);
|
||||
|
||||
return definition;
|
||||
}
|
||||
|
||||
private void DeleteIndexer(int id)
|
||||
{
|
||||
_indexerService.Delete(id);
|
||||
}
|
||||
}
|
||||
}
|
@ -8,19 +8,19 @@ namespace NzbDrone.Api.Indexers
|
||||
{
|
||||
public class IndexerSchemaModule : NzbDroneRestModule<IndexerResource>
|
||||
{
|
||||
private readonly IIndexerService _indexerService;
|
||||
private readonly IIndexerFactory _indexerFactory;
|
||||
|
||||
public IndexerSchemaModule(IIndexerService indexerService)
|
||||
public IndexerSchemaModule(IIndexerFactory indexerFactory)
|
||||
: base("indexer/schema")
|
||||
{
|
||||
_indexerService = indexerService;
|
||||
_indexerFactory = indexerFactory;
|
||||
GetResourceAll = GetSchema;
|
||||
}
|
||||
|
||||
private List<IndexerResource> GetSchema()
|
||||
{
|
||||
|
||||
var indexers = _indexerService.Templates().Where(c => c.Implementation =="Newznab");
|
||||
var indexers = _indexerFactory.Templates().Where(c => c.Implementation =="Newznab");
|
||||
|
||||
|
||||
var result = new List<IndexerResource>(indexers.Count());
|
||||
|
@ -109,6 +109,8 @@
|
||||
<Compile Include="Frontend\StaticResourceModule.cs" />
|
||||
<Compile Include="History\HistoryResource.cs" />
|
||||
<Compile Include="History\HistoryModule.cs" />
|
||||
<Compile Include="IndexerResource.cs" />
|
||||
<Compile Include="ProviderModuleBase.cs" />
|
||||
<Compile Include="Indexers\IndexerSchemaModule.cs" />
|
||||
<Compile Include="Indexers\IndexerModule.cs" />
|
||||
<Compile Include="Indexers\IndexerResource.cs" />
|
||||
|
133
NzbDrone.Api/ProviderModuleBase.cs
Normal file
133
NzbDrone.Api/ProviderModuleBase.cs
Normal file
@ -0,0 +1,133 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FluentValidation;
|
||||
using Nancy;
|
||||
using NzbDrone.Api.ClientSchema;
|
||||
using NzbDrone.Api.Extensions;
|
||||
using NzbDrone.Api.Indexers;
|
||||
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;
|
||||
|
||||
protected ProviderModuleBase(IProviderFactory<TProvider, TProviderDefinition> providerFactory)
|
||||
{
|
||||
_providerFactory = providerFactory;
|
||||
Get["templates"] = x => GetTemplates();
|
||||
GetResourceAll = GetAll;
|
||||
GetResourceById = GetProviderById;
|
||||
CreateResource = CreateProvider;
|
||||
UpdateResource = UpdateProvider;
|
||||
DeleteResource = DeleteProvider;
|
||||
|
||||
|
||||
|
||||
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
||||
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
|
||||
SharedValidator.RuleFor(c => c.ConfigContract).NotEmpty();
|
||||
|
||||
PostValidator.RuleFor(c => c.Fields).NotEmpty();
|
||||
}
|
||||
|
||||
private TProviderResource GetProviderById(int id)
|
||||
{
|
||||
return _providerFactory.Get(id).InjectTo<TProviderResource>();
|
||||
}
|
||||
|
||||
private List<TProviderResource> GetAll()
|
||||
{
|
||||
var indexerDefinitions = _providerFactory.All();
|
||||
|
||||
var result = new List<TProviderResource>(indexerDefinitions.Count);
|
||||
|
||||
foreach (var definition in indexerDefinitions)
|
||||
{
|
||||
var indexerResource = new TProviderResource();
|
||||
indexerResource.InjectFrom(definition);
|
||||
indexerResource.Fields = SchemaBuilder.ToSchema(definition.Settings);
|
||||
|
||||
result.Add(indexerResource);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private int CreateProvider(TProviderResource indexerResource)
|
||||
{
|
||||
var indexer = GetDefinition(indexerResource);
|
||||
indexer = _providerFactory.Create(indexer);
|
||||
return indexer.Id;
|
||||
}
|
||||
|
||||
private void UpdateProvider(TProviderResource indexerResource)
|
||||
{
|
||||
var indexer = GetDefinition(indexerResource);
|
||||
|
||||
ValidateIndexer(indexer);
|
||||
|
||||
_providerFactory.Update(indexer);
|
||||
}
|
||||
|
||||
|
||||
private static void ValidateIndexer(ProviderDefinition definition)
|
||||
{
|
||||
if (!definition.Enable) return;
|
||||
|
||||
var validationResult = definition.Settings.Validate();
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
throw new ValidationException(validationResult.Errors);
|
||||
}
|
||||
}
|
||||
|
||||
private TProviderDefinition GetDefinition(TProviderResource indexerResource)
|
||||
{
|
||||
|
||||
var definition = new TProviderDefinition();
|
||||
|
||||
definition.InjectFrom(indexerResource);
|
||||
|
||||
var configContract = ReflectionExtensions.CoreAssembly.FindTypeByName(definition.ConfigContract);
|
||||
definition.Settings = (IProviderConfig)SchemaBuilder.ReadFormSchema(indexerResource.Fields, configContract);
|
||||
|
||||
ValidateIndexer(definition);
|
||||
|
||||
return definition;
|
||||
}
|
||||
|
||||
private void DeleteProvider(int id)
|
||||
{
|
||||
_providerFactory.Delete(id);
|
||||
}
|
||||
|
||||
private Response GetTemplates()
|
||||
{
|
||||
|
||||
var indexers = _providerFactory.Templates();
|
||||
|
||||
|
||||
var result = new List<IndexerResource>(indexers.Count());
|
||||
|
||||
foreach (var indexer in indexers)
|
||||
{
|
||||
var indexerResource = new IndexerResource();
|
||||
indexerResource.InjectFrom(indexer);
|
||||
indexerResource.Fields = SchemaBuilder.ToSchema(indexer.Settings);
|
||||
|
||||
result.Add(indexerResource);
|
||||
}
|
||||
|
||||
return result.AsResponse();
|
||||
}
|
||||
}
|
||||
}
|
@ -37,7 +37,7 @@ public void container_should_inject_itself()
|
||||
{
|
||||
var factory = MainAppContainerBuilder.BuildContainer(args).Resolve<IServiceFactory>();
|
||||
|
||||
factory.Build<IIndexerService>().Should().NotBeNull();
|
||||
factory.Build<IIndexerFactory>().Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -13,7 +13,7 @@
|
||||
|
||||
namespace NzbDrone.Core.Test.IndexerTests
|
||||
{
|
||||
public class IndexerServiceFixture : DbTest<IndexerService, IndexerDefinition>
|
||||
public class IndexerServiceFixture : DbTest<IndexerFactory, IndexerDefinition>
|
||||
{
|
||||
private List<IIndexer> _indexers;
|
||||
|
||||
|
@ -24,7 +24,7 @@ public interface ISearchForNzb
|
||||
|
||||
public class NzbSearchService : ISearchForNzb
|
||||
{
|
||||
private readonly IIndexerService _indexerService;
|
||||
private readonly IIndexerFactory _indexerFactory;
|
||||
private readonly IFetchFeedFromIndexers _feedFetcher;
|
||||
private readonly ISceneMappingService _sceneMapping;
|
||||
private readonly ISeriesService _seriesService;
|
||||
@ -32,7 +32,7 @@ public class NzbSearchService : ISearchForNzb
|
||||
private readonly IMakeDownloadDecision _makeDownloadDecision;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public NzbSearchService(IIndexerService indexerService,
|
||||
public NzbSearchService(IIndexerFactory indexerFactory,
|
||||
IFetchFeedFromIndexers feedFetcher,
|
||||
ISceneMappingService sceneMapping,
|
||||
ISeriesService seriesService,
|
||||
@ -40,7 +40,7 @@ public NzbSearchService(IIndexerService indexerService,
|
||||
IMakeDownloadDecision makeDownloadDecision,
|
||||
Logger logger)
|
||||
{
|
||||
_indexerService = indexerService;
|
||||
_indexerFactory = indexerFactory;
|
||||
_feedFetcher = feedFetcher;
|
||||
_sceneMapping = sceneMapping;
|
||||
_seriesService = seriesService;
|
||||
@ -132,7 +132,7 @@ public List<DownloadDecision> SeasonSearch(int seriesId, int seasonNumber)
|
||||
|
||||
private List<DownloadDecision> Dispatch(Func<IIndexer, IEnumerable<ReleaseInfo>> searchAction, SearchCriteriaBase criteriaBase)
|
||||
{
|
||||
var indexers = _indexerService.GetAvailableProviders().ToList();
|
||||
var indexers = _indexerFactory.GetAvailableProviders().ToList();
|
||||
var reports = new List<ReleaseInfo>();
|
||||
|
||||
_logger.ProgressInfo("Searching {0} indexers for {1}", indexers.Count, criteriaBase);
|
||||
|
@ -14,13 +14,13 @@ public interface IFetchAndParseRss
|
||||
|
||||
public class FetchAndParseRssService : IFetchAndParseRss
|
||||
{
|
||||
private readonly IIndexerService _indexerService;
|
||||
private readonly IIndexerFactory _indexerFactory;
|
||||
private readonly IFetchFeedFromIndexers _feedFetcher;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public FetchAndParseRssService(IIndexerService indexerService, IFetchFeedFromIndexers feedFetcher, Logger logger)
|
||||
public FetchAndParseRssService(IIndexerFactory indexerFactory, IFetchFeedFromIndexers feedFetcher, Logger logger)
|
||||
{
|
||||
_indexerService = indexerService;
|
||||
_indexerFactory = indexerFactory;
|
||||
_feedFetcher = feedFetcher;
|
||||
_logger = logger;
|
||||
}
|
||||
@ -29,7 +29,7 @@ public List<ReleaseInfo> Fetch()
|
||||
{
|
||||
var result = new List<ReleaseInfo>();
|
||||
|
||||
var indexers = _indexerService.GetAvailableProviders().ToList();
|
||||
var indexers = _indexerFactory.GetAvailableProviders().ToList();
|
||||
|
||||
if (!indexers.Any())
|
||||
{
|
||||
|
@ -5,17 +5,17 @@
|
||||
|
||||
namespace NzbDrone.Core.Indexers
|
||||
{
|
||||
public interface IIndexerService : IProviderFactory<IIndexer, IndexerDefinition>
|
||||
public interface IIndexerFactory : IProviderFactory<IIndexer, IndexerDefinition>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class IndexerService : ProviderFactory<IIndexer, IndexerDefinition>, IIndexerService
|
||||
public class IndexerFactory : ProviderFactory<IIndexer, IndexerDefinition>, IIndexerFactory
|
||||
{
|
||||
private readonly IIndexerRepository _providerRepository;
|
||||
private readonly IEnumerable<IIndexer> _providers;
|
||||
|
||||
public IndexerService(IIndexerRepository providerRepository, IEnumerable<IIndexer> providers, Logger logger)
|
||||
public IndexerFactory(IIndexerRepository providerRepository, IEnumerable<IIndexer> providers, Logger logger)
|
||||
: base(providerRepository, providers, logger)
|
||||
{
|
||||
_providerRepository = providerRepository;
|
@ -500,7 +500,7 @@
|
||||
<Compile Include="History\HistoryService.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Indexers\IndexerService.cs">
|
||||
<Compile Include="Indexers\IndexerFactory.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Indexers\IndexerFetchService.cs">
|
||||
|
Loading…
Reference in New Issue
Block a user