2018-11-23 07:03:32 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using FluentValidation;
|
|
|
|
using FluentValidation.Results;
|
2021-10-21 20:04:19 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-11-23 07:03:32 +00:00
|
|
|
using NzbDrone.Common.Serializer;
|
|
|
|
using NzbDrone.Core.ThingiProvider;
|
|
|
|
using NzbDrone.Core.Validation;
|
2021-10-21 20:04:19 +00:00
|
|
|
using Radarr.Http.REST;
|
|
|
|
using Radarr.Http.REST.Attributes;
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2019-12-03 01:36:18 +00:00
|
|
|
namespace Radarr.Api.V3
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2023-05-25 04:13:27 +00:00
|
|
|
public abstract class ProviderControllerBase<TProviderResource, TBulkProviderResource, TProvider, TProviderDefinition> : RestController<TProviderResource>
|
2018-11-23 07:03:32 +00:00
|
|
|
where TProviderDefinition : ProviderDefinition, new()
|
|
|
|
where TProvider : IProvider
|
2021-02-04 21:14:52 +00:00
|
|
|
where TProviderResource : ProviderResource<TProviderResource>, new()
|
2023-05-25 04:13:27 +00:00
|
|
|
where TBulkProviderResource : ProviderBulkResource<TBulkProviderResource>, new()
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
private readonly IProviderFactory<TProvider, TProviderDefinition> _providerFactory;
|
|
|
|
private readonly ProviderResourceMapper<TProviderResource, TProviderDefinition> _resourceMapper;
|
2023-05-25 04:13:27 +00:00
|
|
|
private readonly ProviderBulkResourceMapper<TBulkProviderResource, TProviderDefinition> _bulkResourceMapper;
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2023-05-25 04:13:27 +00:00
|
|
|
protected ProviderControllerBase(IProviderFactory<TProvider,
|
|
|
|
TProviderDefinition> providerFactory,
|
|
|
|
string resource,
|
|
|
|
ProviderResourceMapper<TProviderResource, TProviderDefinition> resourceMapper,
|
|
|
|
ProviderBulkResourceMapper<TBulkProviderResource, TProviderDefinition> bulkResourceMapper)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
_providerFactory = providerFactory;
|
|
|
|
_resourceMapper = resourceMapper;
|
2023-05-25 04:13:27 +00:00
|
|
|
_bulkResourceMapper = bulkResourceMapper;
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
SharedValidator.RuleFor(c => c.Name).NotEmpty();
|
2019-12-22 22:08:53 +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");
|
2018-11-23 07:03:32 +00:00
|
|
|
SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
|
|
|
|
SharedValidator.RuleFor(c => c.ConfigContract).NotEmpty();
|
|
|
|
|
|
|
|
PostValidator.RuleFor(c => c.Fields).NotNull();
|
|
|
|
}
|
|
|
|
|
2021-10-24 20:35:22 +00:00
|
|
|
protected override TProviderResource GetResourceById(int id)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
var definition = _providerFactory.Get(id);
|
|
|
|
_providerFactory.SetProviderCharacteristics(definition);
|
|
|
|
|
|
|
|
return _resourceMapper.ToResource(definition);
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
[HttpGet]
|
|
|
|
public List<TProviderResource> GetAll()
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2023-03-27 03:40:51 +00:00
|
|
|
var providerDefinitions = _providerFactory.All();
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2023-03-27 03:40:51 +00:00
|
|
|
var result = new List<TProviderResource>(providerDefinitions.Count);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2023-03-27 03:40:51 +00:00
|
|
|
foreach (var definition in providerDefinitions.OrderBy(p => p.ImplementationName))
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
_providerFactory.SetProviderCharacteristics(definition);
|
|
|
|
|
|
|
|
result.Add(_resourceMapper.ToResource(definition));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.OrderBy(p => p.Name).ToList();
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
[RestPostById]
|
2023-06-21 03:34:07 +00:00
|
|
|
[Consumes("application/json")]
|
|
|
|
[Produces("application/json")]
|
2023-05-26 15:35:31 +00:00
|
|
|
public ActionResult<TProviderResource> CreateProvider([FromBody] TProviderResource providerResource, [FromQuery] bool forceSave = false)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2023-05-26 15:35:31 +00:00
|
|
|
var providerDefinition = GetDefinition(providerResource, true, !forceSave, false);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
if (providerDefinition.Enable)
|
|
|
|
{
|
|
|
|
Test(providerDefinition, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
providerDefinition = _providerFactory.Create(providerDefinition);
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
return Created(providerDefinition.Id);
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
[RestPutById]
|
2023-06-21 03:34:07 +00:00
|
|
|
[Consumes("application/json")]
|
|
|
|
[Produces("application/json")]
|
2023-05-18 10:17:36 +00:00
|
|
|
public ActionResult<TProviderResource> UpdateProvider([FromBody] TProviderResource providerResource, [FromQuery] bool forceSave = false)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2023-05-26 15:35:31 +00:00
|
|
|
var providerDefinition = GetDefinition(providerResource, true, !forceSave, false);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2020-10-17 02:28:19 +00:00
|
|
|
// Only test existing definitions if it is enabled and forceSave isn't set.
|
|
|
|
if (providerDefinition.Enable && !forceSave)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
Test(providerDefinition, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
_providerFactory.Update(providerDefinition);
|
2021-10-21 20:04:19 +00:00
|
|
|
|
|
|
|
return Accepted(providerResource.Id);
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 04:13:27 +00:00
|
|
|
[HttpPut("bulk")]
|
|
|
|
[Consumes("application/json")]
|
2023-06-21 03:34:07 +00:00
|
|
|
[Produces("application/json")]
|
2023-07-10 19:37:47 +00:00
|
|
|
public virtual ActionResult<TProviderResource> UpdateProvider([FromBody] TBulkProviderResource providerResource)
|
2023-05-25 04:13:27 +00:00
|
|
|
{
|
2023-07-10 19:37:47 +00:00
|
|
|
if (!providerResource.Ids.Any())
|
|
|
|
{
|
|
|
|
throw new BadRequestException("ids must be provided");
|
|
|
|
}
|
|
|
|
|
2023-05-25 04:13:27 +00:00
|
|
|
var definitionsToUpdate = _providerFactory.Get(providerResource.Ids).ToList();
|
|
|
|
|
|
|
|
foreach (var definition in definitionsToUpdate)
|
|
|
|
{
|
2023-07-07 21:00:08 +00:00
|
|
|
_providerFactory.SetProviderCharacteristics(definition);
|
|
|
|
|
2023-05-25 04:13:27 +00:00
|
|
|
if (providerResource.Tags != null)
|
|
|
|
{
|
|
|
|
var newTags = providerResource.Tags;
|
|
|
|
var applyTags = providerResource.ApplyTags;
|
|
|
|
|
|
|
|
switch (applyTags)
|
|
|
|
{
|
|
|
|
case ApplyTags.Add:
|
|
|
|
newTags.ForEach(t => definition.Tags.Add(t));
|
|
|
|
break;
|
|
|
|
case ApplyTags.Remove:
|
|
|
|
newTags.ForEach(t => definition.Tags.Remove(t));
|
|
|
|
break;
|
|
|
|
case ApplyTags.Replace:
|
|
|
|
definition.Tags = new HashSet<int>(newTags);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_bulkResourceMapper.UpdateModel(providerResource, definitionsToUpdate);
|
|
|
|
|
|
|
|
return Accepted(_providerFactory.Update(definitionsToUpdate).Select(x => _resourceMapper.ToResource(x)));
|
|
|
|
}
|
|
|
|
|
2022-03-29 01:04:10 +00:00
|
|
|
private TProviderDefinition GetDefinition(TProviderResource providerResource, bool validate, bool includeWarnings, bool forceValidate)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2022-05-24 03:41:50 +00:00
|
|
|
var existingDefinition = providerResource.Id > 0 ? _providerFactory.Find(providerResource.Id) : null;
|
|
|
|
var definition = _resourceMapper.ToModel(providerResource, existingDefinition);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2022-03-29 01:04:10 +00:00
|
|
|
if (validate && (definition.Enable || forceValidate))
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
Validate(definition, includeWarnings);
|
|
|
|
}
|
|
|
|
|
|
|
|
return definition;
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
[RestDeleteById]
|
2021-12-01 21:28:41 +00:00
|
|
|
public object DeleteProvider(int id)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
_providerFactory.Delete(id);
|
2021-12-01 21:28:41 +00:00
|
|
|
return new { };
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 04:13:27 +00:00
|
|
|
[HttpDelete("bulk")]
|
|
|
|
[Consumes("application/json")]
|
2023-07-10 19:37:47 +00:00
|
|
|
public virtual object DeleteProviders([FromBody] TBulkProviderResource resource)
|
2023-05-25 04:13:27 +00:00
|
|
|
{
|
|
|
|
_providerFactory.Delete(resource.Ids);
|
|
|
|
|
|
|
|
return new { };
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
[HttpGet("schema")]
|
|
|
|
public List<TProviderResource> GetTemplates()
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
var defaultDefinitions = _providerFactory.GetDefaultDefinitions().OrderBy(p => p.ImplementationName).ToList();
|
|
|
|
|
2020-10-02 18:05:34 +00:00
|
|
|
var result = new List<TProviderResource>(defaultDefinitions.Count);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
foreach (var providerDefinition in defaultDefinitions)
|
|
|
|
{
|
|
|
|
var providerResource = _resourceMapper.ToResource(providerDefinition);
|
|
|
|
var presetDefinitions = _providerFactory.GetPresetDefinitions(providerDefinition);
|
|
|
|
|
2021-02-04 21:14:52 +00:00
|
|
|
providerResource.Presets = presetDefinitions
|
|
|
|
.Select(v => _resourceMapper.ToResource(v))
|
|
|
|
.ToList();
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
result.Add(providerResource);
|
|
|
|
}
|
|
|
|
|
2019-08-28 21:43:55 +00:00
|
|
|
return result;
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
[SkipValidation(true, false)]
|
|
|
|
[HttpPost("test")]
|
|
|
|
public object Test([FromBody] TProviderResource providerResource)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2022-03-29 01:04:10 +00:00
|
|
|
var providerDefinition = GetDefinition(providerResource, true, true, true);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
Test(providerDefinition, true);
|
|
|
|
|
|
|
|
return "{}";
|
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
[HttpPost("testall")]
|
2023-06-21 03:34:07 +00:00
|
|
|
[Produces("application/json")]
|
2021-10-21 20:04:19 +00:00
|
|
|
public IActionResult TestAll()
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
var providerDefinitions = _providerFactory.All()
|
|
|
|
.Where(c => c.Settings.Validate().IsValid && c.Enable)
|
|
|
|
.ToList();
|
|
|
|
var result = new List<ProviderTestAllResult>();
|
|
|
|
|
|
|
|
foreach (var definition in providerDefinitions)
|
|
|
|
{
|
|
|
|
var validationResult = _providerFactory.Test(definition);
|
|
|
|
|
|
|
|
result.Add(new ProviderTestAllResult
|
2019-12-22 22:08:53 +00:00
|
|
|
{
|
|
|
|
Id = definition.Id,
|
|
|
|
ValidationFailures = validationResult.Errors.ToList()
|
|
|
|
});
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
return result.Any(c => !c.IsValid) ? BadRequest(result) : Ok(result);
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
[SkipValidation]
|
|
|
|
[HttpPost("action/{name}")]
|
2023-06-21 03:34:07 +00:00
|
|
|
[Consumes("application/json")]
|
|
|
|
[Produces("application/json")]
|
2021-10-21 20:04:19 +00:00
|
|
|
public IActionResult RequestAction(string name, [FromBody] TProviderResource resource)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
2022-03-29 01:04:10 +00:00
|
|
|
var providerDefinition = GetDefinition(resource, false, false, false);
|
2021-10-21 20:04:19 +00:00
|
|
|
|
|
|
|
var query = Request.Query.ToDictionary(x => x.Key, x => x.Value.ToString());
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
var data = _providerFactory.RequestAction(providerDefinition, name, query);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
2021-10-21 20:04:19 +00:00
|
|
|
return Content(data.ToJson(), "application/json");
|
2018-11-23 07:03:32 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 01:04:10 +00:00
|
|
|
private void Validate(TProviderDefinition definition, bool includeWarnings)
|
2018-11-23 07:03:32 +00:00
|
|
|
{
|
|
|
|
var validationResult = definition.Settings.Validate();
|
|
|
|
|
|
|
|
VerifyValidationResult(validationResult, includeWarnings);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Test(TProviderDefinition definition, bool includeWarnings)
|
|
|
|
{
|
|
|
|
var validationResult = _providerFactory.Test(definition);
|
|
|
|
|
|
|
|
VerifyValidationResult(validationResult, includeWarnings);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void VerifyValidationResult(ValidationResult validationResult, bool includeWarnings)
|
|
|
|
{
|
2023-05-26 15:35:31 +00:00
|
|
|
var result = validationResult as NzbDroneValidationResult ?? new NzbDroneValidationResult(validationResult.Errors);
|
2018-11-23 07:03:32 +00:00
|
|
|
|
|
|
|
if (includeWarnings && (!result.IsValid || result.HasWarnings))
|
|
|
|
{
|
|
|
|
throw new ValidationException(result.Failures);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.IsValid)
|
|
|
|
{
|
|
|
|
throw new ValidationException(result.Errors);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|