New: Webhook improvements

pull/110/head
Qstick 7 years ago
parent 7e863d61ff
commit 6af1ffba50

@ -1,35 +1,78 @@
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
using NzbDrone.Core.Music;
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/Lidarr/Lidarr/wiki/Webhook";
public override void OnGrab(GrabMessage message)
{
_service.OnGrab(message.Artist, message.Album, message.Quality, Settings);
var remoteAlbum = message.Album;
var quality = message.Quality;
var payload = new WebhookGrabPayload
{
EventType = "Grab",
Artist = new WebhookArtist(message.Artist),
Albums = remoteAlbum.Albums.ConvertAll(x => new WebhookAlbum(x)
{
// TODO: Stop passing these parameters inside an album v3
Quality = quality.Quality.Name,
QualityVersion = quality.Revision.Version,
ReleaseGroup = remoteAlbum.ParsedAlbumInfo.ReleaseGroup
}),
Release = new WebhookRelease(quality, remoteAlbum)
};
_proxy.SendWebhook(payload, Settings);
}
public override void OnDownload(DownloadMessage message)
{
_service.OnDownload(message.Artist, message.TrackFile, Settings);
var trackFile = message.TrackFile;
var payload = new WebhookImportPayload
{
EventType = "Download",
Artist = new WebhookArtist(message.Artist),
Tracks = trackFile.Tracks.Value.ConvertAll(x => new WebhookTrack(x)
{
// TODO: Stop passing these parameters inside an episode v3
Quality = trackFile.Quality.Quality.Name,
QualityVersion = trackFile.Quality.Revision.Version,
ReleaseGroup = trackFile.ReleaseGroup
}),
TrackFile = new WebhookTrackFile(trackFile),
IsUpgrade = message.OldFiles.Any()
};
_proxy.SendWebhook(payload, Settings);
}
public override void OnRename(Artist artist)
{
_service.OnRename(artist, Settings);
var payload = new WebhookPayload
{
EventType = "Rename",
Artist = new WebhookArtist(artist)
};
_proxy.SendWebhook(payload, Settings);
}
public override string Name => "Webhook";
@ -38,9 +81,42 @@ 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",
Artist = new WebhookArtist()
{
Id = 1,
Name = "Test Name",
Path = "C:\\testpath",
MBId = "aaaaa-aaa-aaaa-aaaaaa"
},
Albums = new List<WebhookAlbum>() {
new WebhookAlbum()
{
Id = 123,
Title = "Test title"
}
}
};
_proxy.SendWebhook(payload, Settings);
}
catch (WebhookException ex)
{
return new NzbDroneValidationFailure("Url", ex.Message);
}
return null;
}
}
}

@ -5,7 +5,7 @@ namespace NzbDrone.Core.Notifications.Webhook
public class WebhookArtist
{
public int Id { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public string MBId { get; set; }
@ -14,7 +14,7 @@ namespace NzbDrone.Core.Notifications.Webhook
public WebhookArtist(Artist artist)
{
Id = artist.Id;
Title = artist.Name;
Name = artist.Name;
Path = artist.Path;
MBId = artist.ForeignArtistId;
}

@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookGrabPayload : WebhookPayload
{
public List<WebhookAlbum> Albums { 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<WebhookTrack> Tracks { get; set; }
public WebhookTrackFile TrackFile { 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
{
public class WebhookPayload
{
public string EventType { get; set; }
public WebhookArtist Artist { get; set; }
public List<WebhookAlbum> Albums { 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, RemoteAlbum remoteAlbum)
{
Quality = quality.Quality.Name;
QualityVersion = quality.Revision.Version;
ReleaseGroup = remoteAlbum.ParsedAlbumInfo.ReleaseGroup;
ReleaseTitle = remoteAlbum.Release.Title;
Indexer = remoteAlbum.Release.Indexer;
Size = remoteAlbum.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,116 +0,0 @@
using FluentValidation.Results;
using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Music;
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(Artist artist, TrackFile trackFile, WebhookSettings settings);
void OnRename(Artist artist, WebhookSettings settings);
void OnGrab(Artist artist, RemoteAlbum album, QualityModel quality, WebhookSettings settings);
ValidationFailure Test(WebhookSettings settings);
}
public class WebhookService : IWebhookService
{
public void OnDownload(Artist artist, TrackFile trackFile, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Download",
Artist = new WebhookArtist(artist),
Albums = trackFile.Tracks.Value.ConvertAll(x => new WebhookAlbum(x.Album) {
Quality = trackFile.Quality.Quality.Name,
QualityVersion = trackFile.Quality.Revision.Version,
ReleaseGroup = trackFile.ReleaseGroup,
SceneName = trackFile.SceneName
})
};
NotifyWebhook(payload, settings);
}
public void OnRename(Artist artist, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Rename",
Artist = new WebhookArtist(artist)
};
NotifyWebhook(payload, settings);
}
public void OnGrab(Artist artist, RemoteAlbum album, QualityModel quality, WebhookSettings settings)
{
var payload = new WebhookPayload
{
EventType = "Grab",
Artist = new WebhookArtist(artist),
Albums = album.Albums.ConvertAll(x => new WebhookAlbum(x)
{
Quality = quality.Quality.Name,
QualityVersion = quality.Revision.Version,
ReleaseGroup = album.ParsedAlbumInfo.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",
Artist = new WebhookArtist()
{
Id = 1,
Title = "Test Title",
Path = "C:\\testpath",
MBId = "1234"
},
Albums = new List<WebhookAlbum>() {
new WebhookAlbum()
{
Id = 123,
Title = "Test title"
}
}
},
settings
);
}
catch (WebhookException ex)
{
return new NzbDroneValidationFailure("Url", ex.Message);
}
return null;
}
}
}

@ -0,0 +1,26 @@
using NzbDrone.Core.Music;
using System;
namespace NzbDrone.Core.Notifications.Webhook
{
public class WebhookTrack
{
public WebhookTrack() { }
public WebhookTrack(Track track)
{
Id = track.Id;
Title = track.Title;
TrackNumber = track.TrackNumber;
}
public int Id { get; set; }
public string Title { get; set; }
public int TrackNumber { get; set; }
public string Quality { get; set; }
public int QualityVersion { get; set; }
public string ReleaseGroup { get; set; }
}
}

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

@ -816,12 +816,17 @@
<Compile Include="Notifications\Twitter\TwitterException.cs" />
<Compile Include="Notifications\Webhook\WebhookAlbum.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\WebhookArtist.cs" />
<Compile Include="Notifications\Webhook\WebhookService.cs" />
<Compile Include="Notifications\Webhook\WebhookProxy.cs" />
<Compile Include="Notifications\Webhook\WebhookRelease.cs" />
<Compile Include="Notifications\Webhook\WebhookSettings.cs" />
<Compile Include="Notifications\Webhook\Webhook.cs" />
<Compile Include="Notifications\Webhook\WebhookTrack.cs" />
<Compile Include="Notifications\Webhook\WebhookTrackFile.cs" />
<Compile Include="Organizer\AbsoluteTrackFormat.cs" />
<Compile Include="Organizer\TrackFormat.cs" />
<Compile Include="Organizer\NamingConfigRepository.cs" />

Loading…
Cancel
Save