Convert Notifiarr Payload to JSON, Standardize with Webhook (#1194)

* Convert Notifiarr Payload to JSON, Standardize with Webhook

* fixup!
pull/1205/head
Qstick 2 years ago committed by GitHub
parent 4f4c011436
commit 9ff0b90626
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,15 +1,18 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized;
using FluentValidation.Results; using FluentValidation.Results;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Notifications.Webhook;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Notifiarr namespace NzbDrone.Core.Notifications.Notifiarr
{ {
public class Notifiarr : NotificationBase<NotifiarrSettings> public class Notifiarr : WebhookBase<NotifiarrSettings>
{ {
private readonly INotifiarrProxy _proxy; private readonly INotifiarrProxy _proxy;
public Notifiarr(INotifiarrProxy proxy) public Notifiarr(INotifiarrProxy proxy, IConfigFileProvider configFileProvider)
: base(configFileProvider)
{ {
_proxy = proxy; _proxy = proxy;
} }
@ -18,36 +21,35 @@ namespace NzbDrone.Core.Notifications.Notifiarr
public override string Name => "Notifiarr"; public override string Name => "Notifiarr";
public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck)
{ {
var variables = new StringDictionary(); _proxy.SendNotification(BuildHealthPayload(healthCheck), Settings);
variables.Add("Prowlarr_EventType", "HealthIssue");
variables.Add("Prowlarr_Health_Issue_Level", healthCheck.Type.ToString() ?? string.Empty);
variables.Add("Prowlarr_Health_Issue_Message", healthCheck.Message);
variables.Add("Prowlarr_Health_Issue_Type", healthCheck.Source.Name);
variables.Add("Prowlarr_Health_Issue_Wiki", healthCheck.WikiUrl.ToString() ?? string.Empty);
_proxy.SendNotification(variables, Settings);
} }
public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage)
{ {
var variables = new StringDictionary(); _proxy.SendNotification(BuildApplicationUploadPayload(updateMessage), Settings);
variables.Add("Prowlarr_EventType", "ApplicationUpdate");
variables.Add("Prowlarr_Update_Message", updateMessage.Message);
variables.Add("Prowlarr_Update_NewVersion", updateMessage.NewVersion.ToString());
variables.Add("Prowlarr_Update_PreviousVersion", updateMessage.PreviousVersion.ToString());
_proxy.SendNotification(variables, Settings);
} }
public override ValidationResult Test() public override ValidationResult Test()
{ {
var failures = new List<ValidationFailure>(); var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_proxy.Test(Settings)); failures.AddIfNotNull(SendWebhookTest());
return new ValidationResult(failures); return new ValidationResult(failures);
} }
private ValidationFailure SendWebhookTest()
{
try
{
_proxy.SendNotification(BuildTestPayload(), Settings);
}
catch (NotifiarrException ex)
{
return new NzbDroneValidationFailure("APIKey", ex.Message);
}
return null;
}
} }
} }

@ -1,74 +1,45 @@
using System; using System.Net.Http;
using System.Collections.Specialized;
using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Notifications.Webhook;
namespace NzbDrone.Core.Notifications.Notifiarr namespace NzbDrone.Core.Notifications.Notifiarr
{ {
public interface INotifiarrProxy public interface INotifiarrProxy
{ {
void SendNotification(StringDictionary message, NotifiarrSettings settings); void SendNotification(WebhookPayload payload, NotifiarrSettings settings);
ValidationFailure Test(NotifiarrSettings settings);
} }
public class NotifiarrProxy : INotifiarrProxy public class NotifiarrProxy : INotifiarrProxy
{ {
private const string URL = "https://notifiarr.com"; private const string URL = "https://notifiarr.com";
private readonly IHttpClient _httpClient; private readonly IHttpClient _httpClient;
private readonly IConfigFileProvider _configFileProvider;
private readonly Logger _logger;
public NotifiarrProxy(IHttpClient httpClient, IConfigFileProvider configFileProvider, Logger logger) public NotifiarrProxy(IHttpClient httpClient)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_configFileProvider = configFileProvider;
_logger = logger;
} }
public void SendNotification(StringDictionary message, NotifiarrSettings settings) public void SendNotification(WebhookPayload payload, NotifiarrSettings settings)
{ {
ProcessNotification(message, settings); ProcessNotification(payload, settings);
} }
public ValidationFailure Test(NotifiarrSettings settings) private void ProcessNotification(WebhookPayload payload, NotifiarrSettings settings)
{ {
try try
{ {
var variables = new StringDictionary(); var request = new HttpRequestBuilder(URL + "/api/v1/notification/prowlarr")
variables.Add("Prowlarr_EventType", "Test"); .Accept(HttpAccept.Json)
.SetHeader("X-API-Key", settings.APIKey)
.Build();
SendNotification(variables, settings); request.Method = HttpMethod.Post;
return null;
}
catch (NotifiarrException ex)
{
return new ValidationFailure("APIKey", ex.Message);
}
catch (Exception ex)
{
_logger.Error(ex, ex.Message);
return new ValidationFailure("", "Unable to send test notification. Check the log for more details.");
}
}
private void ProcessNotification(StringDictionary message, NotifiarrSettings settings)
{
try
{
var instanceName = _configFileProvider.InstanceName;
var requestBuilder = new HttpRequestBuilder(URL + "/api/v1/notification/prowlarr").Post();
requestBuilder.AddFormParameter("instanceName", instanceName).Build();
requestBuilder.SetHeader("X-API-Key", settings.APIKey);
foreach (string key in message.Keys)
{
requestBuilder.AddFormParameter(key, message[key]);
}
var request = requestBuilder.Build(); request.Headers.ContentType = "application/json";
request.SetContent(payload.ToJson());
_httpClient.Post(request); _httpClient.Post(request);
} }
@ -78,25 +49,20 @@ namespace NzbDrone.Core.Notifications.Notifiarr
switch ((int)responseCode) switch ((int)responseCode)
{ {
case 401: case 401:
_logger.Error("Unauthorized", "HTTP 401 - API key is invalid");
throw new NotifiarrException("API key is invalid"); throw new NotifiarrException("API key is invalid");
case 400: case 400:
_logger.Error("Invalid Request", "HTTP 400 - Unable to send notification. Ensure Prowlarr Integration is enabled & assigned a channel on Notifiarr");
throw new NotifiarrException("Unable to send notification. Ensure Prowlarr Integration is enabled & assigned a channel on Notifiarr"); throw new NotifiarrException("Unable to send notification. Ensure Prowlarr Integration is enabled & assigned a channel on Notifiarr");
case 502: case 502:
case 503: case 503:
case 504: case 504:
_logger.Error("Service Unavailable", "Unable to send notification. Service Unavailable");
throw new NotifiarrException("Unable to send notification. Service Unavailable", ex); throw new NotifiarrException("Unable to send notification. Service Unavailable", ex);
case 520: case 520:
case 521: case 521:
case 522: case 522:
case 523: case 523:
case 524: case 524:
_logger.Error(ex, "Cloudflare Related HTTP Error - Unable to send notification");
throw new NotifiarrException("Cloudflare Related HTTP Error - Unable to send notification", ex); throw new NotifiarrException("Cloudflare Related HTTP Error - Unable to send notification", ex);
default: default:
_logger.Error(ex, "Unknown HTTP Error - Unable to send notification");
throw new NotifiarrException("Unknown HTTP Error - Unable to send notification", ex); throw new NotifiarrException("Unknown HTTP Error - Unable to send notification", ex);
} }
} }

@ -1,15 +1,17 @@
using System.Collections.Generic; using System.Collections.Generic;
using FluentValidation.Results; using FluentValidation.Results;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Validation; using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.Webhook namespace NzbDrone.Core.Notifications.Webhook
{ {
public class Webhook : NotificationBase<WebhookSettings> public class Webhook : WebhookBase<WebhookSettings>
{ {
private readonly IWebhookProxy _proxy; private readonly IWebhookProxy _proxy;
public Webhook(IWebhookProxy proxy) public Webhook(IWebhookProxy proxy, IConfigFileProvider configFileProvider)
: base(configFileProvider)
{ {
_proxy = proxy; _proxy = proxy;
} }
@ -18,29 +20,12 @@ namespace NzbDrone.Core.Notifications.Webhook
public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck)
{ {
var payload = new WebhookHealthPayload _proxy.SendWebhook(BuildHealthPayload(healthCheck), Settings);
{
EventType = WebhookEventType.Health,
Level = healthCheck.Type,
Message = healthCheck.Message,
Type = healthCheck.Source.Name,
WikiUrl = healthCheck.WikiUrl?.ToString()
};
_proxy.SendWebhook(payload, Settings);
} }
public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage)
{ {
var payload = new WebhookApplicationUpdatePayload _proxy.SendWebhook(BuildApplicationUploadPayload(updateMessage), Settings);
{
EventType = WebhookEventType.ApplicationUpdate,
Message = updateMessage.Message,
PreviousVersion = updateMessage.PreviousVersion.ToString(),
NewVersion = updateMessage.NewVersion.ToString()
};
_proxy.SendWebhook(payload, Settings);
} }
public override string Name => "Webhook"; public override string Name => "Webhook";
@ -58,12 +43,7 @@ namespace NzbDrone.Core.Notifications.Webhook
{ {
try try
{ {
var payload = new WebhookHealthPayload _proxy.SendWebhook(BuildTestPayload(), Settings);
{
EventType = WebhookEventType.Test
};
_proxy.SendWebhook(payload, Settings);
} }
catch (WebhookException ex) catch (WebhookException ex)
{ {

@ -0,0 +1,51 @@
using NzbDrone.Core.Configuration;
using NzbDrone.Core.ThingiProvider;
namespace NzbDrone.Core.Notifications.Webhook
{
public abstract class WebhookBase<TSettings> : NotificationBase<TSettings>
where TSettings : IProviderConfig, new()
{
private readonly IConfigFileProvider _configFileProvider;
protected WebhookBase(IConfigFileProvider configFileProvider)
: base()
{
_configFileProvider = configFileProvider;
}
protected WebhookHealthPayload BuildHealthPayload(HealthCheck.HealthCheck healthCheck)
{
return new WebhookHealthPayload
{
EventType = WebhookEventType.Health,
InstanceName = _configFileProvider.InstanceName,
Level = healthCheck.Type,
Message = healthCheck.Message,
Type = healthCheck.Source.Name,
WikiUrl = healthCheck.WikiUrl?.ToString()
};
}
protected WebhookApplicationUpdatePayload BuildApplicationUploadPayload(ApplicationUpdateMessage updateMessage)
{
return new WebhookApplicationUpdatePayload
{
EventType = WebhookEventType.ApplicationUpdate,
InstanceName = _configFileProvider.InstanceName,
Message = updateMessage.Message,
PreviousVersion = updateMessage.PreviousVersion.ToString(),
NewVersion = updateMessage.NewVersion.ToString()
};
}
protected WebhookPayload BuildTestPayload()
{
return new WebhookPayload
{
EventType = WebhookEventType.Test,
InstanceName = _configFileProvider.InstanceName
};
}
}
}

@ -3,5 +3,6 @@ namespace NzbDrone.Core.Notifications.Webhook
public class WebhookPayload public class WebhookPayload
{ {
public WebhookEventType EventType { get; set; } public WebhookEventType EventType { get; set; }
public string InstanceName { get; set; }
} }
} }

Loading…
Cancel
Save