diff --git a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs old mode 100644 new mode 100755 index 153f86af4..ed9a6d582 --- a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs @@ -1,35 +1,64 @@ - using System.Collections.Generic; +using System.Linq; using FluentValidation.Results; using NzbDrone.Core.Tv; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Validation; namespace NzbDrone.Core.Notifications.Webhook { public class Webhook : NotificationBase { - private readonly IWebhookService _service; + private readonly IWebhookProxy _proxy; - public Webhook(IWebhookService service) + public Webhook(IWebhookProxy proxy) { - _service = service; + _proxy = proxy; } public override string Link => "https://github.com/Radarr/Radarr/wiki/Webhook"; public override void OnGrab(GrabMessage message) { - _service.OnGrab(message.Movie, message.RemoteMovie, message.Quality, Settings); + var remoteMovie = message.RemoteMovie; + var quality = message.Quality; + + var payload = new WebhookGrabPayload + { + EventType = "Grab", + Movie = new WebhookMovie(message.Movie), + RemoteMovie = new WebhookRemoteMovie(remoteMovie), + Release = new WebhookRelease(quality, remoteMovie) + }; + + _proxy.SendWebhook(payload, Settings); } public override void OnDownload(DownloadMessage message) { - _service.OnDownload(message.Movie, message.MovieFile, Settings); + var movieFile = message.MovieFile; + + var payload = new WebhookImportPayload + { + EventType = "Download", + Movie = new WebhookMovie(message.Movie), + RemoteMovie = new WebhookRemoteMovie(message.Movie), + MovieFile = new WebhookMovieFile(movieFile), + IsUpgrade = message.OldMovieFiles.Any() + }; + + _proxy.SendWebhook(payload, Settings); } public override void OnMovieRename(Movie movie) { - _service.OnRename(movie, Settings); + var payload = new WebhookPayload + { + EventType = "Rename", + Movie = new WebhookMovie(movie) + }; + + _proxy.SendWebhook(payload, Settings); } public override void OnRename(Series series) @@ -42,9 +71,51 @@ namespace NzbDrone.Core.Notifications.Webhook { var failures = new List(); - failures.AddIfNotNull(_service.Test(Settings)); + failures.AddIfNotNull(SendWebhookTest()); return new ValidationResult(failures); } + + private ValidationFailure SendWebhookTest() + { + try + { + var payload = new WebhookGrabPayload + { + EventType = "Test", + Movie = new WebhookMovie + { + Id = 1, + Title = "Test Title", + FilePath = "C:\\testpath", + ReleaseDate = "1970-01-01" + }, + RemoteMovie = new WebhookRemoteMovie + { + TmdbId = 1234, + ImdbId = "5678", + Title = "Test title", + Year = 1970 + }, + Release = new WebhookRelease + { + Indexer = "Test Indexer", + Quality = "Test Quality", + QualityVersion = 1, + ReleaseGroup = "Test Group", + ReleaseTitle = "Test Title", + Size = 9999999 + } + }; + + _proxy.SendWebhook(payload, Settings); + } + catch (WebhookException ex) + { + return new NzbDroneValidationFailure("Url", ex.Message); + } + + return null; + } } } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookEpisode.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookEpisode.cs deleted file mode 100644 index a7979b726..000000000 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookEpisode.cs +++ /dev/null @@ -1,32 +0,0 @@ -using NzbDrone.Core.Tv; -using System; - -namespace NzbDrone.Core.Notifications.Webhook -{ - public class WebhookEpisode - { - public WebhookEpisode() { } - - public WebhookEpisode(Episode episode) - { - Id = episode.Id; - SeasonNumber = episode.SeasonNumber; - EpisodeNumber = episode.EpisodeNumber; - Title = episode.Title; - AirDate = episode.AirDate; - AirDateUtc = episode.AirDateUtc; - } - - public int Id { get; set; } - public int EpisodeNumber { get; set; } - public int SeasonNumber { get; set; } - public string Title { get; set; } - public string AirDate { get; set; } - public DateTime? AirDateUtc { get; set; } - - public string Quality { get; set; } - public int QualityVersion { get; set; } - public string ReleaseGroup { get; set; } - public string SceneName { get; set; } - } -} \ No newline at end of file diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookGrabPayload.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookGrabPayload.cs new file mode 100755 index 000000000..57d8f040a --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookGrabPayload.cs @@ -0,0 +1,8 @@ +namespace NzbDrone.Core.Notifications.Webhook +{ + class WebhookGrabPayload : WebhookPayload + { + public WebhookRemoteMovie RemoteMovie { get; set; } + public WebhookRelease Release { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookImportPayload.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookImportPayload.cs new file mode 100755 index 000000000..7043137c7 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookImportPayload.cs @@ -0,0 +1,9 @@ +namespace NzbDrone.Core.Notifications.Webhook +{ + class WebhookImportPayload : WebhookPayload + { + public WebhookRemoteMovie RemoteMovie { get; set; } + public WebhookMovieFile MovieFile { get; set; } + public bool IsUpgrade { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookMethod.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookMethod.cs old mode 100644 new mode 100755 index 42c080e00..5d6e859a6 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookMethod.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookMethod.cs @@ -1,8 +1,10 @@ -namespace NzbDrone.Core.Notifications.Webhook +using NzbDrone.Common.Http; + +namespace NzbDrone.Core.Notifications.Webhook { public enum WebhookMethod { - POST = RestSharp.Method.POST, - PUT = RestSharp.Method.PUT + POST = HttpMethod.POST, + PUT = HttpMethod.PUT } -} \ No newline at end of file +} diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookMovie.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookMovie.cs old mode 100644 new mode 100755 index 0d38862e8..0699ae44a --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookMovie.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookMovie.cs @@ -9,6 +9,7 @@ namespace NzbDrone.Core.Notifications.Webhook public int Id { get; set; } public string Title { get; set; } public string FilePath { get; set; } + public string ReleaseDate { get; set; } public WebhookMovie() { } @@ -16,12 +17,11 @@ namespace NzbDrone.Core.Notifications.Webhook { Id = movie.Id; Title = movie.Title; + ReleaseDate = movie.PhysicalReleaseDate().ToString("yyyy-MM-dd"); } - public WebhookMovie(Movie movie, MovieFile movieFile) + public WebhookMovie(Movie movie, MovieFile movieFile) : this(movie) { - Id = movie.Id; - Title = movie.Title; FilePath = Path.Combine(movie.Path, movieFile.RelativePath); } } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookMovieFile.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookMovieFile.cs new file mode 100755 index 000000000..cd19f2923 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookMovieFile.cs @@ -0,0 +1,28 @@ +using NzbDrone.Core.MediaFiles; + +namespace NzbDrone.Core.Notifications.Webhook +{ + class WebhookMovieFile + { + public WebhookMovieFile() { } + + public WebhookMovieFile(MovieFile movieFile) + { + Id = movieFile.Id; + RelativePath = movieFile.RelativePath; + Path = movieFile.Path; + Quality = movieFile.Quality.Quality.Name; + QualityVersion = movieFile.Quality.Revision.Version; + ReleaseGroup = movieFile.ReleaseGroup; + SceneName = movieFile.SceneName; + } + + public int Id { get; set; } + public string RelativePath { get; set; } + public string Path { get; set; } + public string Quality { get; set; } + public int QualityVersion { get; set; } + public string ReleaseGroup { get; set; } + public string SceneName { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookPayload.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookPayload.cs old mode 100644 new mode 100755 index 8be45eb18..91a936272 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookPayload.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookPayload.cs @@ -1,11 +1,8 @@ -using System.Collections.Generic; - namespace NzbDrone.Core.Notifications.Webhook { public class WebhookPayload { public string EventType { get; set; } public WebhookMovie Movie { get; set; } - public WebhookRemoteMovie RemoteMovie { get; set; } } } diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookProxy.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookProxy.cs new file mode 100755 index 000000000..328c838b8 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookProxy.cs @@ -0,0 +1,41 @@ +using NzbDrone.Common.Http; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Rest; + +namespace NzbDrone.Core.Notifications.Webhook +{ + public interface IWebhookProxy + { + void SendWebhook(WebhookPayload payload, WebhookSettings settings); + } + + class WebhookProxy : IWebhookProxy + { + private readonly IHttpClient _httpClient; + + public WebhookProxy(IHttpClient httpClient) + { + _httpClient = httpClient; + } + + public void SendWebhook(WebhookPayload body, WebhookSettings settings) + { + try + { + var request = new HttpRequestBuilder(settings.Url) + .Accept(HttpAccept.Json) + .Build(); + + request.Method = (HttpMethod)settings.Method; + request.Headers.ContentType = "application/json"; + request.SetContent(body.ToJson()); + + _httpClient.Execute(request); + } + catch (RestException ex) + { + throw new WebhookException("Unable to post to webhook: {0}", ex, ex.Message); + } + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookRelease.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookRelease.cs new file mode 100755 index 000000000..05d24ad99 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookRelease.cs @@ -0,0 +1,27 @@ +using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Qualities; + +namespace NzbDrone.Core.Notifications.Webhook +{ + public class WebhookRelease + { + public WebhookRelease() { } + + public WebhookRelease(QualityModel quality, RemoteMovie remoteMovie) + { + Quality = quality.Quality.Name; + QualityVersion = quality.Revision.Version; + ReleaseGroup = remoteMovie.ParsedMovieInfo.ReleaseGroup; + ReleaseTitle = remoteMovie.Release.Title; + Indexer = remoteMovie.Release.Indexer; + Size = remoteMovie.Release.Size; + } + + public string Quality { get; set; } + public int QualityVersion { get; set; } + public string ReleaseGroup { get; set; } + public string ReleaseTitle { get; set; } + public string Indexer { get; set; } + public long Size { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookSeries.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookSeries.cs deleted file mode 100644 index 222f9eebb..000000000 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookSeries.cs +++ /dev/null @@ -1,22 +0,0 @@ -using NzbDrone.Core.Tv; - -namespace NzbDrone.Core.Notifications.Webhook -{ - public class WebhookSeries - { - public int Id { get; set; } - public string Title { get; set; } - public string Path { get; set; } - public int TvdbId { get; set; } - - public WebhookSeries() { } - - public WebhookSeries(Series series) - { - Id = series.Id; - Title = series.Title; - Path = series.Path; - TvdbId = series.TvdbId; - } - } -} \ No newline at end of file diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookService.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookService.cs deleted file mode 100644 index f3677fbd3..000000000 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookService.cs +++ /dev/null @@ -1,103 +0,0 @@ -using FluentValidation.Results; -using NzbDrone.Core.MediaFiles; -using NzbDrone.Core.Tv; -using NzbDrone.Core.Validation; -using NzbDrone.Core.Rest; -using RestSharp; -using NzbDrone.Core.Qualities; -using NzbDrone.Core.Parser.Model; -using System.Collections.Generic; - -namespace NzbDrone.Core.Notifications.Webhook -{ - public interface IWebhookService - { - void OnDownload(Movie movie, MovieFile movieFile, WebhookSettings settings); - void OnRename(Movie movie, WebhookSettings settings); - void OnGrab(Movie movie, RemoteMovie remoteMovie, QualityModel quality, WebhookSettings settings); - ValidationFailure Test(WebhookSettings settings); - } - - public class WebhookService : IWebhookService - { - public void OnDownload(Movie movie, MovieFile movieFile, WebhookSettings settings) - { - var payload = new WebhookPayload - { - EventType = "Download", - Movie = new WebhookMovie(movie, movieFile), - RemoteMovie = new WebhookRemoteMovie(movie) - }; - - NotifyWebhook(payload, settings); - } - - public void OnRename(Movie movie, WebhookSettings settings) - { - var payload = new WebhookPayload - { - EventType = "Rename", - Movie = new WebhookMovie(movie) - }; - - NotifyWebhook(payload, settings); - } - - public void OnGrab(Movie movie, RemoteMovie remoteMovie, QualityModel quality, WebhookSettings settings) - { - var payload = new WebhookPayload - { - EventType = "Grab", - Movie = new WebhookMovie(movie), - RemoteMovie = new WebhookRemoteMovie(remoteMovie) - }; - NotifyWebhook(payload, settings); - } - - public void NotifyWebhook(WebhookPayload body, WebhookSettings settings) - { - try { - var client = RestClientFactory.BuildClient(settings.Url); - var request = new RestRequest((Method) settings.Method); - request.RequestFormat = DataFormat.Json; - request.AddBody(body); - client.ExecuteAndValidate(request); - } - catch (RestException ex) - { - throw new WebhookException("Unable to post to webhook: {0}", ex, ex.Message); - } - } - - public ValidationFailure Test(WebhookSettings settings) - { - try - { - NotifyWebhook( - new WebhookPayload - { - EventType = "Test", - Movie = new WebhookMovie() - { - Id = 1, - Title = "Test Title", - FilePath = "C:\\testpath", - }, - RemoteMovie = new WebhookRemoteMovie(){ - ImdbId = "tt012345", - Title = "My Awesome Movie!" - } - }, - - settings - ); - } - catch (WebhookException ex) - { - return new NzbDroneValidationFailure("Url", ex.Message); - } - - return null; - } - } -} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index e85b02d56..4c4ac5cf1 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -986,14 +986,16 @@ - + + + - + + - @@ -1391,4 +1393,4 @@ --> - \ No newline at end of file +