diff --git a/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs b/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs index 255567784..2f3c651fc 100644 --- a/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs +++ b/src/NzbDrone.Core/Notifications/Notifiarr/Notifiarr.cs @@ -1,15 +1,18 @@ using System.Collections.Generic; -using System.Collections.Specialized; using FluentValidation.Results; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Notifications.Webhook; +using NzbDrone.Core.Validation; namespace NzbDrone.Core.Notifications.Notifiarr { - public class Notifiarr : NotificationBase + public class Notifiarr : WebhookBase { private readonly INotifiarrProxy _proxy; - public Notifiarr(INotifiarrProxy proxy) + public Notifiarr(INotifiarrProxy proxy, IConfigFileProvider configFileProvider) + : base(configFileProvider) { _proxy = proxy; } @@ -18,36 +21,35 @@ namespace NzbDrone.Core.Notifications.Notifiarr public override string Name => "Notifiarr"; public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) { - var variables = new StringDictionary(); - - 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); + _proxy.SendNotification(BuildHealthPayload(healthCheck), Settings); } public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) { - var variables = new StringDictionary(); - - 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); + _proxy.SendNotification(BuildApplicationUploadPayload(updateMessage), Settings); } public override ValidationResult Test() { var failures = new List(); - failures.AddIfNotNull(_proxy.Test(Settings)); + failures.AddIfNotNull(SendWebhookTest()); return new ValidationResult(failures); } + + private ValidationFailure SendWebhookTest() + { + try + { + _proxy.SendNotification(BuildTestPayload(), Settings); + } + catch (NotifiarrException ex) + { + return new NzbDroneValidationFailure("APIKey", ex.Message); + } + + return null; + } } } diff --git a/src/NzbDrone.Core/Notifications/Notifiarr/NotifiarrProxy.cs b/src/NzbDrone.Core/Notifications/Notifiarr/NotifiarrProxy.cs index 67728f88b..6b0bf6375 100644 --- a/src/NzbDrone.Core/Notifications/Notifiarr/NotifiarrProxy.cs +++ b/src/NzbDrone.Core/Notifications/Notifiarr/NotifiarrProxy.cs @@ -1,74 +1,45 @@ -using System; -using System.Collections.Specialized; -using FluentValidation.Results; +using System.Net.Http; using NLog; -using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; +using NzbDrone.Common.Serializer; using NzbDrone.Core.Configuration; +using NzbDrone.Core.Notifications.Webhook; namespace NzbDrone.Core.Notifications.Notifiarr { public interface INotifiarrProxy { - void SendNotification(StringDictionary message, NotifiarrSettings settings); - ValidationFailure Test(NotifiarrSettings settings); + void SendNotification(WebhookPayload payload, NotifiarrSettings settings); } public class NotifiarrProxy : INotifiarrProxy { private const string URL = "https://notifiarr.com"; 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; - _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 { - var variables = new StringDictionary(); - variables.Add("Prowlarr_EventType", "Test"); + var request = new HttpRequestBuilder(URL + "/api/v1/notification/prowlarr") + .Accept(HttpAccept.Json) + .SetHeader("X-API-Key", settings.APIKey) + .Build(); - SendNotification(variables, settings); - 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]); - } + request.Method = HttpMethod.Post; - var request = requestBuilder.Build(); + request.Headers.ContentType = "application/json"; + request.SetContent(payload.ToJson()); _httpClient.Post(request); } @@ -78,25 +49,20 @@ namespace NzbDrone.Core.Notifications.Notifiarr switch ((int)responseCode) { case 401: - _logger.Error("Unauthorized", "HTTP 401 - API key is invalid"); throw new NotifiarrException("API key is invalid"); 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"); case 502: case 503: case 504: - _logger.Error("Service Unavailable", "Unable to send notification. Service Unavailable"); throw new NotifiarrException("Unable to send notification. Service Unavailable", ex); case 520: case 521: case 522: case 523: 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); default: - _logger.Error(ex, "Unknown HTTP Error - Unable to send notification"); throw new NotifiarrException("Unknown HTTP Error - Unable to send notification", ex); } } diff --git a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs index 16c5e9983..fa09bc5f5 100755 --- a/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/Webhook.cs @@ -1,15 +1,17 @@ using System.Collections.Generic; using FluentValidation.Results; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; using NzbDrone.Core.Validation; namespace NzbDrone.Core.Notifications.Webhook { - public class Webhook : NotificationBase + public class Webhook : WebhookBase { private readonly IWebhookProxy _proxy; - public Webhook(IWebhookProxy proxy) + public Webhook(IWebhookProxy proxy, IConfigFileProvider configFileProvider) + : base(configFileProvider) { _proxy = proxy; } @@ -18,29 +20,12 @@ namespace NzbDrone.Core.Notifications.Webhook public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) { - var payload = new WebhookHealthPayload - { - EventType = WebhookEventType.Health, - Level = healthCheck.Type, - Message = healthCheck.Message, - Type = healthCheck.Source.Name, - WikiUrl = healthCheck.WikiUrl?.ToString() - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildHealthPayload(healthCheck), Settings); } public override void OnApplicationUpdate(ApplicationUpdateMessage updateMessage) { - var payload = new WebhookApplicationUpdatePayload - { - EventType = WebhookEventType.ApplicationUpdate, - Message = updateMessage.Message, - PreviousVersion = updateMessage.PreviousVersion.ToString(), - NewVersion = updateMessage.NewVersion.ToString() - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildApplicationUploadPayload(updateMessage), Settings); } public override string Name => "Webhook"; @@ -58,12 +43,7 @@ namespace NzbDrone.Core.Notifications.Webhook { try { - var payload = new WebhookHealthPayload - { - EventType = WebhookEventType.Test - }; - - _proxy.SendWebhook(payload, Settings); + _proxy.SendWebhook(BuildTestPayload(), Settings); } catch (WebhookException ex) { diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs new file mode 100644 index 000000000..f3f2f2f39 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookBase.cs @@ -0,0 +1,51 @@ +using NzbDrone.Core.Configuration; +using NzbDrone.Core.ThingiProvider; + +namespace NzbDrone.Core.Notifications.Webhook +{ + public abstract class WebhookBase : NotificationBase + 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 + }; + } + } +} diff --git a/src/NzbDrone.Core/Notifications/Webhook/WebhookPayload.cs b/src/NzbDrone.Core/Notifications/Webhook/WebhookPayload.cs index 4b63a748e..05d51c7c1 100755 --- a/src/NzbDrone.Core/Notifications/Webhook/WebhookPayload.cs +++ b/src/NzbDrone.Core/Notifications/Webhook/WebhookPayload.cs @@ -3,5 +3,6 @@ namespace NzbDrone.Core.Notifications.Webhook public class WebhookPayload { public WebhookEventType EventType { get; set; } + public string InstanceName { get; set; } } }