New: (Apprise) Add notification type

Closes #1619
pull/1606/head
Bogdan 2 years ago
parent 742c680014
commit 2015156061

@ -0,0 +1,7 @@
namespace NzbDrone.Core.Notifications.Apprise
{
public class AppriseError
{
public string Error { get; set; }
}
}

@ -0,0 +1,18 @@
using System;
using NzbDrone.Common.Exceptions;
namespace NzbDrone.Core.Notifications.Apprise
{
public class AppriseException : NzbDroneException
{
public AppriseException(string message)
: base(message)
{
}
public AppriseException(string message, Exception innerException, params object[] args)
: base(message, innerException, args)
{
}
}
}

@ -0,0 +1,19 @@
using System.Runtime.Serialization;
namespace NzbDrone.Core.Notifications.Apprise
{
public enum AppriseNotificationType
{
[EnumMember(Value = "info")]
Info = 0,
[EnumMember(Value = "success")]
Success,
[EnumMember(Value = "warning")]
Warning,
[EnumMember(Value = "failure")]
Failure,
}
}

@ -0,0 +1,17 @@
using Newtonsoft.Json;
namespace NzbDrone.Core.Notifications.Apprise
{
public class ApprisePayload
{
public string Urls { get; set; }
public string Title { get; set; }
public string Body { get; set; }
public AppriseNotificationType Type { get; set; }
public string Tag { get; set; }
}
}

@ -3,16 +3,15 @@ using System.Linq;
using System.Net; using System.Net;
using FluentValidation.Results; using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Serializer;
namespace NzbDrone.Core.Notifications.Apprise namespace NzbDrone.Core.Notifications.Apprise
{ {
public interface IAppriseProxy public interface IAppriseProxy
{ {
void SendNotification(AppriseSettings settings, string title, string message); void SendNotification(AppriseSettings settings, string title, string message);
ValidationFailure Test(AppriseSettings settings); ValidationFailure Test(AppriseSettings settings);
} }
@ -27,11 +26,18 @@ namespace NzbDrone.Core.Notifications.Apprise
_logger = logger; _logger = logger;
} }
public void SendNotification(AppriseSettings settings, string title, string body) public void SendNotification(AppriseSettings settings, string title, string message)
{ {
var requestBuilder = new HttpRequestBuilder(settings.BaseUrl.TrimEnd('/', ' ')).Post() var payload = new ApprisePayload
.AddFormParameter("title", title) {
.AddFormParameter("body", body); Title = title,
Body = message,
Type = (AppriseNotificationType)settings.NotificationType
};
var requestBuilder = new HttpRequestBuilder(settings.BaseUrl.TrimEnd('/', ' '))
.Post()
.Accept(HttpAccept.Json);
if (settings.ConfigurationKey.IsNotNullOrWhiteSpace()) if (settings.ConfigurationKey.IsNotNullOrWhiteSpace())
{ {
@ -41,14 +47,14 @@ namespace NzbDrone.Core.Notifications.Apprise
} }
else if (settings.StatelessUrls.IsNotNullOrWhiteSpace()) else if (settings.StatelessUrls.IsNotNullOrWhiteSpace())
{ {
requestBuilder requestBuilder.Resource("/notify");
.Resource("/notify")
.AddFormParameter("urls", settings.StatelessUrls); payload.Urls = settings.StatelessUrls;
} }
if (settings.Tags.Any()) if (settings.Tags.Any())
{ {
requestBuilder.AddFormParameter("tag", settings.Tags.Join(",")); payload.Tag = settings.Tags.Join(",");
} }
if (settings.AuthUsername.IsNotNullOrWhiteSpace() || settings.AuthPassword.IsNotNullOrWhiteSpace()) if (settings.AuthUsername.IsNotNullOrWhiteSpace() || settings.AuthPassword.IsNotNullOrWhiteSpace())
@ -56,7 +62,20 @@ namespace NzbDrone.Core.Notifications.Apprise
requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.AuthUsername, settings.AuthPassword); requestBuilder.NetworkCredential = new BasicNetworkCredential(settings.AuthUsername, settings.AuthPassword);
} }
_httpClient.Execute(requestBuilder.Build()); var request = requestBuilder.Build();
request.Headers.ContentType = "application/json";
request.SetContent(payload.ToJson());
try
{
_httpClient.Execute(request);
}
catch (HttpException ex)
{
_logger.Error(ex, "Unable to send message");
throw new AppriseException("Unable to send Apprise notifications: {0}", ex, ex.Message);
}
} }
public ValidationFailure Test(AppriseSettings settings) public ValidationFailure Test(AppriseSettings settings)
@ -68,21 +87,29 @@ namespace NzbDrone.Core.Notifications.Apprise
{ {
SendNotification(settings, title, body); SendNotification(settings, title, body);
} }
catch (HttpException ex) catch (AppriseException ex) when (ex.InnerException is HttpException httpException)
{ {
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) if (httpException.Response.StatusCode == HttpStatusCode.Unauthorized)
{ {
_logger.Error(ex, $"HTTP Auth credentials are invalid: {ex.Message}"); _logger.Error(ex, $"HTTP Auth credentials are invalid: {0}", ex.Message);
return new ValidationFailure("AuthUsername", $"HTTP Auth credentials are invalid: {ex.Message}"); return new ValidationFailure("AuthUsername", $"HTTP Auth credentials are invalid: {ex.Message}");
} }
_logger.Error(ex, "Unable to send test message. Server connection failed. Status code: {0}", ex.Message); if (httpException.Response.Content.IsNotNullOrWhiteSpace())
return new ValidationFailure("Url", $"Unable to connect to Apprise API. Please try again later. Status code: {ex.Message}"); {
var error = Json.Deserialize<AppriseError>(httpException.Response.Content);
_logger.Error(ex, $"Unable to send test message. Response from API: {0}", error.Error);
return new ValidationFailure(string.Empty, $"Unable to send test message. Response from API: {error.Error}");
}
_logger.Error(ex, "Unable to send test message. Server connection failed: ({0}) {1}", httpException.Response.StatusCode, ex.Message);
return new ValidationFailure("Url", $"Unable to connect to Apprise API. Server connection failed: ({httpException.Response.StatusCode}) {ex.Message}");
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error(ex, "Unable to send test message. Status code: {0}", ex.Message); _logger.Error(ex, "Unable to send test message: {0}", ex.Message);
return new ValidationFailure("Url", $"Unable to send test message. Status code: {ex.Message}"); return new ValidationFailure("Url", $"Unable to send test message: {ex.Message}");
} }
return null; return null;

@ -41,6 +41,7 @@ namespace NzbDrone.Core.Notifications.Apprise
public AppriseSettings() public AppriseSettings()
{ {
NotificationType = (int)AppriseNotificationType.Info;
Tags = Array.Empty<string>(); Tags = Array.Empty<string>();
} }
@ -53,13 +54,16 @@ namespace NzbDrone.Core.Notifications.Apprise
[FieldDefinition(3, Label = "Apprise Stateless Urls", Type = FieldType.Textbox, HelpText = "One or more URLs separated by commas identifying where the notification should be sent to. Leave empty if Persistent Storage is used.", HelpLink = "https://github.com/caronc/apprise#productivity-based-notifications")] [FieldDefinition(3, Label = "Apprise Stateless Urls", Type = FieldType.Textbox, HelpText = "One or more URLs separated by commas identifying where the notification should be sent to. Leave empty if Persistent Storage is used.", HelpLink = "https://github.com/caronc/apprise#productivity-based-notifications")]
public string StatelessUrls { get; set; } public string StatelessUrls { get; set; }
[FieldDefinition(4, Label = "Apprise Tags", Type = FieldType.Tag, HelpText = "Optionally notify only those tagged accordingly.")] [FieldDefinition(4, Label = "Apprise Notification Type", Type = FieldType.Select, SelectOptions = typeof(AppriseNotificationType))]
public int NotificationType { get; set; }
[FieldDefinition(5, Label = "Apprise Tags", Type = FieldType.Tag, HelpText = "Optionally notify only those tagged accordingly.")]
public IEnumerable<string> Tags { get; set; } public IEnumerable<string> Tags { get; set; }
[FieldDefinition(5, Label = "Auth Username", Type = FieldType.Textbox, HelpText = "HTTP Basic Auth Username", Privacy = PrivacyLevel.UserName)] [FieldDefinition(6, Label = "Auth Username", Type = FieldType.Textbox, HelpText = "HTTP Basic Auth Username", Privacy = PrivacyLevel.UserName)]
public string AuthUsername { get; set; } public string AuthUsername { get; set; }
[FieldDefinition(6, Label = "Auth Password", Type = FieldType.Password, HelpText = "HTTP Basic Auth Password", Privacy = PrivacyLevel.Password)] [FieldDefinition(7, Label = "Auth Password", Type = FieldType.Password, HelpText = "HTTP Basic Auth Password", Privacy = PrivacyLevel.Password)]
public string AuthPassword { get; set; } public string AuthPassword { get; set; }
public NzbDroneValidationResult Validate() public NzbDroneValidationResult Validate()

Loading…
Cancel
Save