From 7bd96ae75cf68dedc8f76819075912e510578388 Mon Sep 17 00:00:00 2001 From: Qstick Date: Fri, 5 Apr 2019 23:51:21 -0400 Subject: [PATCH] Fixed: Re-Clean Release Endpoints, we have ReadResourceFromRequest! --- src/Lidarr.Api.V1/Indexers/ReleaseModule.cs | 20 ++------- .../Indexers/ReleasePushModule.cs | 44 ++++--------------- src/Lidarr.Http/REST/RestModule.cs | 13 +++++- 3 files changed, 24 insertions(+), 53 deletions(-) diff --git a/src/Lidarr.Api.V1/Indexers/ReleaseModule.cs b/src/Lidarr.Api.V1/Indexers/ReleaseModule.cs index 69fd534f5..debc7dc28 100644 --- a/src/Lidarr.Api.V1/Indexers/ReleaseModule.cs +++ b/src/Lidarr.Api.V1/Indexers/ReleaseModule.cs @@ -1,9 +1,7 @@ using System; using System.Collections.Generic; -using System.Linq; using FluentValidation; using Nancy; -using Nancy.ModelBinding; using NLog; using NzbDrone.Common.Cache; using NzbDrone.Core.DecisionEngine; @@ -14,7 +12,6 @@ using NzbDrone.Core.IndexerSearch; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Validation; using Lidarr.Http.Extensions; -using Lidarr.Http.REST; using HttpStatusCode = System.Net.HttpStatusCode; namespace Lidarr.Api.V1.Indexers @@ -27,7 +24,6 @@ namespace Lidarr.Api.V1.Indexers private readonly IPrioritizeDownloadDecision _prioritizeDownloadDecision; private readonly IDownloadService _downloadService; private readonly Logger _logger; - private ResourceValidator _releaseValidator; private readonly ICached _remoteAlbumCache; @@ -46,25 +42,17 @@ namespace Lidarr.Api.V1.Indexers _downloadService = downloadService; _logger = logger; - _releaseValidator = new ResourceValidator(); - _releaseValidator.RuleFor(s => s.IndexerId).ValidId(); - _releaseValidator.RuleFor(s => s.Guid).NotEmpty(); - GetResourceAll = GetReleases; - Post["/"] = x => DownloadRelease(this.Bind()); + Post["/"] = x => DownloadRelease(ReadResourceFromRequest()); + + PostValidator.RuleFor(s => s.IndexerId).ValidId(); + PostValidator.RuleFor(s => s.Guid).NotEmpty(); _remoteAlbumCache = cacheManager.GetCache(GetType(), "remoteAlbums"); } private Response DownloadRelease(ReleaseResource release) { - var validationFailures = _releaseValidator.Validate(release).Errors; - - if (validationFailures.Any()) - { - throw new ValidationException(validationFailures); - } - var remoteAlbum = _remoteAlbumCache.Find(GetCacheKey(release)); if (remoteAlbum == null) diff --git a/src/Lidarr.Api.V1/Indexers/ReleasePushModule.cs b/src/Lidarr.Api.V1/Indexers/ReleasePushModule.cs index e484cdd85..c770cb8ed 100644 --- a/src/Lidarr.Api.V1/Indexers/ReleasePushModule.cs +++ b/src/Lidarr.Api.V1/Indexers/ReleasePushModule.cs @@ -3,7 +3,6 @@ using System.Linq; using FluentValidation; using FluentValidation.Results; using Nancy; -using Nancy.ModelBinding; using NLog; using NzbDrone.Core.DecisionEngine; using NzbDrone.Core.Download; @@ -12,10 +11,6 @@ using Lidarr.Http.Extensions; using NzbDrone.Common.Extensions; using NzbDrone.Core.Datastore; using NzbDrone.Core.Indexers; -using Lidarr.Http.REST; -using System; -using NzbDrone.Core.Exceptions; -using HttpStatusCode = System.Net.HttpStatusCode; namespace Lidarr.Api.V1.Indexers { @@ -25,7 +20,6 @@ namespace Lidarr.Api.V1.Indexers private readonly IProcessDownloadDecisions _downloadDecisionProcessor; private readonly IIndexerFactory _indexerFactory; private readonly Logger _logger; - private ResourceValidator _releaseValidator; public ReleasePushModule(IMakeDownloadDecision downloadDecisionMaker, IProcessDownloadDecisions downloadDecisionProcessor, @@ -37,39 +31,19 @@ namespace Lidarr.Api.V1.Indexers _indexerFactory = indexerFactory; _logger = logger; - _releaseValidator = new ResourceValidator(); - _releaseValidator.RuleFor(s => s.Title).NotEmpty(); - _releaseValidator.RuleFor(s => s.DownloadUrl).NotEmpty(); - _releaseValidator.RuleFor(s => s.DownloadProtocol).NotEmpty(); - _releaseValidator.RuleFor(s => s.PublishDate).NotEmpty(); + Post["/push"] = x => ProcessRelease(ReadResourceFromRequest()); - Post["/push"] = x => ProcessRelease(); + PostValidator.RuleFor(s => s.Title).NotEmpty(); + PostValidator.RuleFor(s => s.DownloadUrl).NotEmpty(); + PostValidator.RuleFor(s => s.DownloadProtocol).NotEmpty(); + PostValidator.RuleFor(s => s.PublishDate).NotEmpty(); } - private Response ProcessRelease() + private Response ProcessRelease(ReleaseResource release) { + _logger.Info("Release pushed: {0} - {1}", release.Title, release.DownloadUrl); - var resource = new ReleaseResource(); - - try - { - resource = Request.Body.FromJson(); - } - catch (Exception ex) - { - throw new NzbDroneClientException(HttpStatusCode.BadRequest, ex.Message); - } - - var validationFailures = _releaseValidator.Validate(resource).Errors; - - if (validationFailures.Any()) - { - throw new ValidationException(validationFailures); - } - - _logger.Info("Release pushed: {0} - {1}", resource.Title, resource.DownloadUrl); - - var info = resource.ToModel(); + var info = release.ToModel(); info.Guid = "PUSH-" + info.DownloadUrl; @@ -82,7 +56,7 @@ namespace Lidarr.Api.V1.Indexers if (firstDecision?.RemoteAlbum.ParsedAlbumInfo == null) { - throw new ValidationException(new List { new ValidationFailure("Title", "Unable to parse", resource.Title) }); + throw new ValidationException(new List { new ValidationFailure("Title", "Unable to parse", release.Title) }); } return MapDecisions(new[] { firstDecision }).AsResponse(); diff --git a/src/Lidarr.Http/REST/RestModule.cs b/src/Lidarr.Http/REST/RestModule.cs index 3fea34aed..8c7f7f714 100644 --- a/src/Lidarr.Http/REST/RestModule.cs +++ b/src/Lidarr.Http/REST/RestModule.cs @@ -5,6 +5,7 @@ using FluentValidation; using Nancy; using NzbDrone.Core.Datastore; using Lidarr.Http.Extensions; +using Newtonsoft.Json; namespace Lidarr.Http.REST { @@ -194,8 +195,16 @@ namespace Lidarr.Http.REST protected TResource ReadResourceFromRequest(bool skipValidate = false) { - //TODO: handle when request is null - var resource = Request.Body.FromJson(); + var resource = new TResource(); + + try + { + resource = Request.Body.FromJson(); + } + catch (JsonReaderException ex) + { + throw new BadRequestException(ex.Message); + } if (resource == null) {