Webhook improvements

New: Include Path/Relative Path for on download Webhooks
New: IsUpgrade flag for on download Webhooks
Mark McDowall 8 years ago
parent 601b54c244
commit 4189bc6f76
No known key found for this signature in database
GPG Key ID: D4CEFA9A718052E0

@ -1,35 +1,77 @@

using System.Collections.Generic;
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<WebhookSettings>
{
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/Sonarr/Sonarr/wiki/Webhook";
public override void OnGrab(GrabMessage message)
{
_service.OnGrab(message.Series, message.Episode, message.Quality, Settings);
var remoteEpisode = message.Episode;
var quality = message.Quality;
var payload = new WebhookGrabPayload
{
EventType = "Grab",
Series = new WebhookSeries(message.Series),
Episodes = remoteEpisode.Episodes.ConvertAll(x => new WebhookEpisode(x)
{
// TODO: Stop passing these parameters inside an episode v3
Quality = quality.Quality.Name,
QualityVersion = quality.Revision.Version,
ReleaseGroup = remoteEpisode.ParsedEpisodeInfo.ReleaseGroup
}),
Release = new WebhookRelease(quality, remoteEpisode)
};
_proxy.SendWebhook(payload, Settings);
}
public override void OnDownload(DownloadMessage message)
{
_service.OnDownload(message.Series, message.EpisodeFile, Settings);
var episodeFile = message.EpisodeFile;
var payload = new WebhookImportPayload
{
EventType = "Download",
Series = new WebhookSeries(message.Series),
Episodes = episodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x)
{
// TODO: Stop passing these parameters inside an episode v3
Quality = episodeFile.Quality.Quality.Name,
QualityVersion = episodeFile.Quality.Revision.Version,
ReleaseGroup = episodeFile.ReleaseGroup,
SceneName = episodeFile.SceneName
}),
EpisodeFile = new WebhookEpisodeFile(episodeFile),
IsUpgrade = message.OldFiles.Any()
};
_proxy.SendWebhook(payload, Settings);
}
public override void OnRename(Series series)
{
_service.OnRename(series, Settings);
var payload = new WebhookPayload
{
EventType = "Rename",
Series = new WebhookSeries(series)
};
_proxy.SendWebhook(payload, Settings);
}
public override string Name => "Webhook";
@ -38,9 +80,44 @@ namespace NzbDrone.Core.Notifications.Webhook
{
var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_service.Test(Settings));
failures.AddIfNotNull(SendWebhookTest());
return new ValidationResult(failures);
}
private ValidationFailure SendWebhookTest()
{
try
{
var payload = new WebhookGrabPayload
{
EventType = "Test",
Series = new WebhookSeries()
{
Id = 1,
Title = "Test Title",
Path = "C:\\testpath",
TvdbId = 1234
},
Episodes = new List<WebhookEpisode>() {
new WebhookEpisode()
{
Id = 123,
EpisodeNumber = 1,
SeasonNumber = 1,
Title = "Test title"
}
}
};
_proxy.SendWebhook(payload, Settings);
}
catch (WebhookException ex)
{
return new NzbDroneValidationFailure("Url", ex.Message);
}
return null;
}
}
}

@ -0,0 +1,28 @@
using NzbDrone.Core.MediaFiles;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookEpisodeFile
{
public WebhookEpisodeFile() { }
public WebhookEpisodeFile(EpisodeFile episodeFile)
{
Id = episodeFile.Id;
RelativePath = episodeFile.RelativePath;
Path = episodeFile.Path;
Quality = episodeFile.Quality.Quality.Name;
QualityVersion = episodeFile.Quality.Revision.Version;
ReleaseGroup = episodeFile.ReleaseGroup;
SceneName = episodeFile.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; }
}
}

@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookGrabPayload : WebhookPayload
{
public List<WebhookEpisode> Episodes { get; set; }
public WebhookRelease Release { get; set; }
}
}

@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookImportPayload : WebhookPayload
{
public List<WebhookEpisode> Episodes { get; set; }
public WebhookEpisodeFile EpisodeFile { get; set; }
public bool IsUpgrade { get; set; }
}
}

@ -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
}
}

@ -1,11 +1,8 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Webhook
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookPayload
{
public string EventType { get; set; }
public WebhookSeries Series { get; set; }
public List<WebhookEpisode> Episodes { get; set; }
}
}

@ -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);
}
public 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);
}
}
}
}

@ -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, RemoteEpisode remoteEpisode)
{
Quality = quality.Quality.Name;
QualityVersion = quality.Revision.Version;
ReleaseGroup = remoteEpisode.ParsedEpisodeInfo.ReleaseGroup;
ReleaseTitle = remoteEpisode.Release.Title;
Indexer = remoteEpisode.Release.Indexer;
Size = remoteEpisode.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; }
}
}

@ -1,118 +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(Series series, EpisodeFile episodeFile, WebhookSettings settings);
void OnRename(Series series, WebhookSettings settings);
void OnGrab(Series series, RemoteEpisode episode, QualityModel quality, WebhookSettings settings);
ValidationFailure Test(WebhookSettings settings);
}
public class WebhookService : IWebhookService
{
public void OnDownload(Series series, EpisodeFile episodeFile, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Download",
Series = new WebhookSeries(series),
Episodes = episodeFile.Episodes.Value.ConvertAll(x => new WebhookEpisode(x) {
Quality = episodeFile.Quality.Quality.Name,
QualityVersion = episodeFile.Quality.Revision.Version,
ReleaseGroup = episodeFile.ReleaseGroup,
SceneName = episodeFile.SceneName
})
};
NotifyWebhook(payload, settings);
}
public void OnRename(Series series, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Rename",
Series = new WebhookSeries(series)
};
NotifyWebhook(payload, settings);
}
public void OnGrab(Series series, RemoteEpisode episode, QualityModel quality, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Grab",
Series = new WebhookSeries(series),
Episodes = episode.Episodes.ConvertAll(x => new WebhookEpisode(x)
{
Quality = quality.Quality.Name,
QualityVersion = quality.Revision.Version,
ReleaseGroup = episode.ParsedEpisodeInfo.ReleaseGroup
})
};
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",
Series = new WebhookSeries()
{
Id = 1,
Title = "Test Title",
Path = "C:\\testpath",
TvdbId = 1234
},
Episodes = new List<WebhookEpisode>() {
new WebhookEpisode()
{
Id = 123,
EpisodeNumber = 1,
SeasonNumber = 1,
Title = "Test title"
}
}
},
settings
);
}
catch (WebhookException ex)
{
return new NzbDroneValidationFailure("Url", ex.Message);
}
return null;
}
}
}

@ -905,12 +905,16 @@
<Compile Include="Notifications\Telegram\TelegramSettings.cs" />
<Compile Include="Notifications\Twitter\OAuthToken.cs" />
<Compile Include="Notifications\Twitter\TwitterException.cs" />
<Compile Include="Notifications\Webhook\WebhookRelease.cs" />
<Compile Include="Notifications\Webhook\WebhookEpisodeFile.cs" />
<Compile Include="Notifications\Webhook\WebhookEpisode.cs" />
<Compile Include="Notifications\Webhook\WebhookException.cs" />
<Compile Include="Notifications\Webhook\WebhookGrabPayload.cs" />
<Compile Include="Notifications\Webhook\WebhookImportPayload.cs" />
<Compile Include="Notifications\Webhook\WebhookMethod.cs" />
<Compile Include="Notifications\Webhook\WebhookPayload.cs" />
<Compile Include="Notifications\Webhook\WebhookSeries.cs" />
<Compile Include="Notifications\Webhook\WebhookService.cs" />
<Compile Include="Notifications\Webhook\WebhookProxy.cs" />
<Compile Include="Notifications\Webhook\WebhookSettings.cs" />
<Compile Include="Notifications\Webhook\Webhook.cs" />
<Compile Include="Organizer\NamingConfigRepository.cs" />

Loading…
Cancel
Save