1
0
mirror of https://github.com/Radarr/Radarr.git synced 2026-04-27 22:57:09 -04:00

New: Use ASP.NET Core instead of Nancy

This commit is contained in:
ta264
2021-10-21 21:04:19 +01:00
committed by Qstick
parent c14ef7bee7
commit 2d53ec24f8
160 changed files with 2866 additions and 3657 deletions
@@ -0,0 +1,14 @@
using System;
using Microsoft.AspNetCore.Mvc;
namespace Radarr.Http.REST.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
public class RestDeleteByIdAttribute : HttpDeleteAttribute
{
public RestDeleteByIdAttribute()
: base("{id:int}")
{
}
}
}
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Routing;
namespace Radarr.Http.REST.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
public class RestGetByIdAttribute : ActionFilterAttribute, IActionHttpMethodProvider, IRouteTemplateProvider
{
public override void OnActionExecuting(ActionExecutingContext context)
{
Console.WriteLine($"OnExecuting {context.Controller.GetType()} {context.ActionDescriptor.DisplayName}");
}
public IEnumerable<string> HttpMethods => new[] { "GET" };
public string Template => "{id:int}";
public new int? Order => 0;
public string Name { get; }
}
}
@@ -0,0 +1,10 @@
using System;
using Microsoft.AspNetCore.Mvc;
namespace Radarr.Http.REST.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
public class RestPostByIdAttribute : HttpPostAttribute
{
}
}
@@ -0,0 +1,14 @@
using System;
using Microsoft.AspNetCore.Mvc;
namespace Radarr.Http.REST.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
public class RestPutByIdAttribute : HttpPutAttribute
{
public RestPutByIdAttribute()
: base("{id:int?}")
{
}
}
}
@@ -0,0 +1,17 @@
using System;
namespace Radarr.Http.REST.Attributes
{
[AttributeUsage(AttributeTargets.Method)]
public class SkipValidationAttribute : Attribute
{
public SkipValidationAttribute(bool skip = true, bool skipShared = true)
{
Skip = skip;
SkipShared = skipShared;
}
public bool Skip { get; }
public bool SkipShared { get; }
}
}
+1 -1
View File
@@ -1,4 +1,4 @@
using Nancy;
using System.Net;
using Radarr.Http.Exceptions;
namespace Radarr.Http.REST
@@ -1,4 +1,4 @@
using Nancy;
using System.Net;
using Radarr.Http.Exceptions;
namespace Radarr.Http.REST
+1 -1
View File
@@ -1,4 +1,4 @@
using Nancy;
using System.Net;
using Radarr.Http.Exceptions;
namespace Radarr.Http.REST
+130
View File
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using FluentValidation.Results;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using NzbDrone.Core.Datastore;
using Radarr.Http.REST.Attributes;
using Radarr.Http.Validation;
namespace Radarr.Http.REST
{
public abstract class RestController<TResource> : Controller
where TResource : RestResource, new()
{
private static readonly List<Type> VALIDATE_ID_ATTRIBUTES = new List<Type> { typeof(RestPutByIdAttribute), typeof(RestDeleteByIdAttribute) };
protected ResourceValidator<TResource> PostValidator { get; private set; }
protected ResourceValidator<TResource> PutValidator { get; private set; }
protected ResourceValidator<TResource> SharedValidator { get; private set; }
protected void ValidateId(int id)
{
if (id <= 0)
{
throw new BadRequestException(id + " is not a valid ID");
}
}
protected RestController()
{
PostValidator = new ResourceValidator<TResource>();
PutValidator = new ResourceValidator<TResource>();
SharedValidator = new ResourceValidator<TResource>();
PutValidator.RuleFor(r => r.Id).ValidId();
}
[RestGetById]
public abstract TResource GetResourceById(int id);
public override void OnActionExecuting(ActionExecutingContext context)
{
var descriptor = context.ActionDescriptor as ControllerActionDescriptor;
var skipAttribute = (SkipValidationAttribute)Attribute.GetCustomAttribute(descriptor.MethodInfo, typeof(SkipValidationAttribute), true);
var skipValidate = skipAttribute?.Skip ?? false;
var skipShared = skipAttribute?.SkipShared ?? false;
if (Request.Method == "POST" || Request.Method == "PUT")
{
var resourceArgs = context.ActionArguments.Values.Where(x => x.GetType() == typeof(TResource))
.Select(x => x as TResource)
.ToList();
foreach (var resource in resourceArgs)
{
ValidateResource(resource, skipValidate, skipShared);
}
}
var attributes = descriptor.MethodInfo.CustomAttributes;
if (attributes.Any(x => VALIDATE_ID_ATTRIBUTES.Contains(x.GetType())) && !skipValidate)
{
if (context.ActionArguments.TryGetValue("id", out var idObj))
{
ValidateId((int)idObj);
}
}
base.OnActionExecuting(context);
}
public override void OnActionExecuted(ActionExecutedContext context)
{
var descriptor = context.ActionDescriptor as ControllerActionDescriptor;
var attributes = descriptor.MethodInfo.CustomAttributes;
if (context.Exception?.GetType() == typeof(ModelNotFoundException) &&
attributes.Any(x => x.AttributeType == typeof(RestGetByIdAttribute)))
{
context.Result = new NotFoundResult();
}
}
protected void ValidateResource(TResource resource, bool skipValidate = false, bool skipSharedValidate = false)
{
if (resource == null)
{
throw new BadRequestException("Request body can't be empty");
}
var errors = new List<ValidationFailure>();
if (!skipSharedValidate)
{
errors.AddRange(SharedValidator.Validate(resource).Errors);
}
if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase) && !skipValidate && !Request.Path.ToString().EndsWith("/test", StringComparison.InvariantCultureIgnoreCase))
{
errors.AddRange(PostValidator.Validate(resource).Errors);
}
else if (Request.Method.Equals("PUT", StringComparison.InvariantCultureIgnoreCase))
{
errors.AddRange(PutValidator.Validate(resource).Errors);
}
if (errors.Any())
{
throw new ValidationException(errors);
}
}
protected ActionResult<TResource> Accepted(int id)
{
var result = GetResourceById(id);
return AcceptedAtAction(nameof(GetResourceById), new { id = id }, result);
}
protected ActionResult<TResource> Created(int id)
{
var result = GetResourceById(id);
return CreatedAtAction(nameof(GetResourceById), new { id = id }, result);
}
}
}
@@ -0,0 +1,106 @@
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.SignalR;
namespace Radarr.Http.REST
{
public abstract class RestControllerWithSignalR<TResource, TModel> : RestController<TResource>, IHandle<ModelEvent<TModel>>
where TResource : RestResource, new()
where TModel : ModelBase, new()
{
protected string Resource { get; }
private readonly IBroadcastSignalRMessage _signalRBroadcaster;
protected RestControllerWithSignalR(IBroadcastSignalRMessage signalRBroadcaster)
{
_signalRBroadcaster = signalRBroadcaster;
var apiAttribute = GetType().GetCustomAttribute<VersionedApiControllerAttribute>();
if (apiAttribute != null && apiAttribute.Resource != VersionedApiControllerAttribute.CONTROLLER_RESOURCE)
{
Resource = apiAttribute.Resource;
}
else
{
Resource = new TResource().ResourceName.Trim('/');
}
}
[NonAction]
public void Handle(ModelEvent<TModel> message)
{
if (!_signalRBroadcaster.IsConnected)
{
return;
}
if (message.Action == ModelAction.Deleted || message.Action == ModelAction.Sync)
{
BroadcastResourceChange(message.Action);
}
BroadcastResourceChange(message.Action, message.Model.Id);
}
protected void BroadcastResourceChange(ModelAction action, int id)
{
if (!_signalRBroadcaster.IsConnected)
{
return;
}
if (action == ModelAction.Deleted)
{
BroadcastResourceChange(action, new TResource { Id = id });
}
else
{
var resource = GetResourceById(id);
BroadcastResourceChange(action, resource);
}
}
protected void BroadcastResourceChange(ModelAction action, TResource resource)
{
if (!_signalRBroadcaster.IsConnected)
{
return;
}
if (GetType().Namespace.Contains("V3"))
{
var signalRMessage = new SignalRMessage
{
Name = Resource,
Body = new ResourceChangeMessage<TResource>(resource, action),
Action = action
};
_signalRBroadcaster.BroadcastMessage(signalRMessage);
}
}
protected void BroadcastResourceChange(ModelAction action)
{
if (!_signalRBroadcaster.IsConnected)
{
return;
}
if (GetType().Namespace.Contains("V3"))
{
var signalRMessage = new SignalRMessage
{
Name = Resource,
Body = new ResourceChangeMessage<TResource>(action),
Action = action
};
_signalRBroadcaster.BroadcastMessage(signalRMessage);
}
}
}
}
-373
View File
@@ -1,373 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using FluentValidation;
using FluentValidation.Results;
using Nancy;
using Nancy.Responses.Negotiation;
using NzbDrone.Core.Datastore;
using Radarr.Http.Extensions;
namespace Radarr.Http.REST
{
public abstract class RestModule<TResource> : NancyModule
where TResource : RestResource, new()
{
private const string ROOT_ROUTE = "/";
private const string ID_ROUTE = @"/(?<id>[\d]{1,10})";
// See src/Radarr.Api.V3/Queue/QueueModule.cs
private static readonly HashSet<string> VALID_SORT_KEYS = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"timeleft",
"estimatedCompletionTime",
"protocol",
"indexer",
"downloadClient",
"quality",
"languages",
"status",
"title",
"progress"
};
private readonly HashSet<string> _excludedKeys = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
{
"page",
"pageSize",
"sortKey",
"sortDirection",
"filterKey",
"filterValue",
};
private Action<int> _deleteResource;
private Func<int, TResource> _getResourceById;
private Func<List<TResource>> _getResourceAll;
private Func<PagingResource<TResource>, PagingResource<TResource>> _getResourcePaged;
private Func<TResource> _getResourceSingle;
private Func<TResource, int> _createResource;
private Action<TResource> _updateResource;
protected ResourceValidator<TResource> PostValidator { get; private set; }
protected ResourceValidator<TResource> PutValidator { get; private set; }
protected ResourceValidator<TResource> SharedValidator { get; private set; }
protected void ValidateId(int id)
{
if (id <= 0)
{
throw new BadRequestException(id + " is not a valid ID");
}
}
protected RestModule(string modulePath)
: base(modulePath)
{
ValidateModule();
PostValidator = new ResourceValidator<TResource>();
PutValidator = new ResourceValidator<TResource>();
SharedValidator = new ResourceValidator<TResource>();
}
private void ValidateModule()
{
if (GetResourceById != null)
{
return;
}
if (CreateResource != null || UpdateResource != null)
{
throw new InvalidOperationException("GetResourceById route must be defined before defining Create/Update routes.");
}
}
protected Action<int> DeleteResource
{
private get
{
return _deleteResource;
}
set
{
_deleteResource = value;
Delete(ID_ROUTE, options =>
{
ValidateId(options.Id);
DeleteResource((int)options.Id);
return new object();
});
}
}
protected Func<int, TResource> GetResourceById
{
get
{
return _getResourceById;
}
set
{
_getResourceById = value;
Get(ID_ROUTE, options =>
{
ValidateId(options.Id);
try
{
var resource = GetResourceById((int)options.Id);
if (resource == null)
{
return new NotFoundResponse();
}
return resource;
}
catch (ModelNotFoundException)
{
return new NotFoundResponse();
}
});
}
}
protected Func<List<TResource>> GetResourceAll
{
private get
{
return _getResourceAll;
}
set
{
_getResourceAll = value;
Get(ROOT_ROUTE, options =>
{
var resource = GetResourceAll();
return resource;
});
}
}
protected Func<PagingResource<TResource>, PagingResource<TResource>> GetResourcePaged
{
private get
{
return _getResourcePaged;
}
set
{
_getResourcePaged = value;
Get(ROOT_ROUTE, options =>
{
var resource = GetResourcePaged(ReadPagingResourceFromRequest());
return resource;
});
}
}
protected Func<TResource> GetResourceSingle
{
private get
{
return _getResourceSingle;
}
set
{
_getResourceSingle = value;
Get(ROOT_ROUTE, options =>
{
var resource = GetResourceSingle();
return resource;
});
}
}
protected Func<TResource, int> CreateResource
{
private get
{
return _createResource;
}
set
{
_createResource = value;
Post(ROOT_ROUTE, options =>
{
var id = CreateResource(ReadResourceFromRequest());
return ResponseWithCode(GetResourceById(id), HttpStatusCode.Created);
});
}
}
protected Action<TResource> UpdateResource
{
private get
{
return _updateResource;
}
set
{
_updateResource = value;
Put(ROOT_ROUTE, options =>
{
var resource = ReadResourceFromRequest();
UpdateResource(resource);
return ResponseWithCode(GetResourceById(resource.Id), HttpStatusCode.Accepted);
});
Put(ID_ROUTE, options =>
{
var resource = ReadResourceFromRequest();
resource.Id = options.Id;
UpdateResource(resource);
return ResponseWithCode(GetResourceById(resource.Id), HttpStatusCode.Accepted);
});
}
}
protected Negotiator ResponseWithCode(object model, HttpStatusCode statusCode)
{
return Negotiate.WithModel(model).WithStatusCode(statusCode);
}
protected TResource ReadResourceFromRequest(bool skipValidate = false, bool skipSharedValidate = false)
{
TResource resource;
try
{
resource = Request.Body.FromJson<TResource>();
}
catch (JsonException e)
{
throw new BadRequestException($"Invalid request body. {e.Message}");
}
if (resource == null)
{
throw new BadRequestException("Request body can't be empty");
}
var errors = new List<ValidationFailure>();
if (!skipSharedValidate)
{
errors.AddRange(SharedValidator.Validate(resource).Errors);
}
if (Request.Method.Equals("POST", StringComparison.InvariantCultureIgnoreCase) && !skipValidate && !Request.Url.Path.EndsWith("/test", StringComparison.InvariantCultureIgnoreCase))
{
errors.AddRange(PostValidator.Validate(resource).Errors);
}
else if (Request.Method.Equals("PUT", StringComparison.InvariantCultureIgnoreCase))
{
errors.AddRange(PutValidator.Validate(resource).Errors);
}
if (errors.Any())
{
throw new ValidationException(errors);
}
return resource;
}
private PagingResource<TResource> ReadPagingResourceFromRequest()
{
int pageSize;
int.TryParse(Request.Query.PageSize.ToString(), out pageSize);
if (pageSize == 0)
{
pageSize = 10;
}
int page;
int.TryParse(Request.Query.Page.ToString(), out page);
if (page == 0)
{
page = 1;
}
var pagingResource = new PagingResource<TResource>
{
PageSize = pageSize,
Page = page,
Filters = new List<PagingResourceFilter>()
};
if (Request.Query.SortKey != null)
{
var sortKey = Request.Query.SortKey.ToString();
if (!VALID_SORT_KEYS.Contains(sortKey) &&
!TableMapping.Mapper.IsValidSortKey(sortKey))
{
throw new BadRequestException($"Invalid sort key {sortKey}");
}
pagingResource.SortKey = sortKey;
// For backwards compatibility with v2
if (Request.Query.SortDir != null)
{
pagingResource.SortDirection = Request.Query.SortDir.ToString()
.Equals("Asc", StringComparison.InvariantCultureIgnoreCase)
? SortDirection.Ascending
: SortDirection.Descending;
}
// v3 uses SortDirection instead of SortDir to be consistent with every other use of it
if (Request.Query.SortDirection != null)
{
pagingResource.SortDirection = Request.Query.SortDirection.ToString()
.Equals("ascending", StringComparison.InvariantCultureIgnoreCase)
? SortDirection.Ascending
: SortDirection.Descending;
}
}
// For backwards compatibility with v2
if (Request.Query.FilterKey != null)
{
var filter = new PagingResourceFilter
{
Key = Request.Query.FilterKey.ToString()
};
if (Request.Query.FilterValue != null)
{
filter.Value = Request.Query.FilterValue?.ToString();
}
pagingResource.Filters.Add(filter);
}
// v3 uses filters in key=value format
foreach (var key in Request.Query)
{
if (_excludedKeys.Contains(key))
{
continue;
}
pagingResource.Filters.Add(new PagingResourceFilter
{
Key = key,
Value = Request.Query[key]
});
}
return pagingResource;
}
}
}
@@ -1,4 +1,4 @@
using Nancy;
using System.Net;
using Radarr.Http.Exceptions;
namespace Radarr.Http.REST