Add missing On Delete Notifications to Mailgun notifications

(cherry picked from commit a824fa44d21328d9c8bbb1ccfd6d247d4ff5c888)
This commit is contained in:
Robin Dadswell 2021-04-23 15:54:15 +01:00
parent 5c0ee04271
commit b9abc1be11
4 changed files with 50 additions and 33 deletions

View File

@ -9,7 +9,7 @@ namespace NzbDrone.Core.Notifications.Mailgun
{
private readonly IMailgunProxy _proxy;
private readonly Logger _logger;
public MailGun(IMailgunProxy proxy, Logger logger)
{
_proxy = proxy;
@ -29,6 +29,20 @@ namespace NzbDrone.Core.Notifications.Mailgun
_proxy.SendNotification(MOVIE_GRABBED_TITLE, downloadMessage.Message, Settings);
}
public override void OnMovieFileDelete(MovieFileDeleteMessage deleteMessage)
{
var body = $"{deleteMessage.Message} deleted.";
_proxy.SendNotification(MOVIE_FILE_DELETED_TITLE, body, Settings);
}
public override void OnMovieDelete(MovieDeleteMessage deleteMessage)
{
var body = $"{deleteMessage.Message}";
_proxy.SendNotification(MOVIE_DELETED_TITLE, body, Settings);
}
public override void OnHealthIssue(HealthCheck.HealthCheck healthCheckMessage)
{
_proxy.SendNotification(HEALTH_ISSUE_TITLE, healthCheckMessage.Message, Settings);
@ -41,7 +55,7 @@ namespace NzbDrone.Core.Notifications.Mailgun
try
{
const string title = "Test Notification";
const string body = "This is a test message from Sonarr, though Mailgun.";
const string body = "This is a test message from Radarr, though Mailgun.";
_proxy.SendNotification(title, body, Settings);
_logger.Info("Successsfully sent email though Mailgun.");
@ -51,7 +65,7 @@ namespace NzbDrone.Core.Notifications.Mailgun
_logger.Error(ex, "Unable to send test message though Mailgun.");
failures.Add(new ValidationFailure("", "Unable to send test message though Mailgun."));
}
return new ValidationResult(failures);
}
}

View File

@ -5,9 +5,14 @@ namespace NzbDrone.Core.Notifications.Mailgun
{
public class MailgunException : NzbDroneException
{
public MailgunException(string message) : base (message) { }
public MailgunException(string message)
: base(message)
{
}
public MailgunException(string message, Exception innerException, params object[] args)
: base(message, innerException, args) { }
: base(message, innerException, args)
{
}
}
}

View File

@ -3,19 +3,19 @@ using NLog;
using NzbDrone.Common.Http;
using HttpMethod = NzbDrone.Common.Http.HttpMethod;
namespace NzbDrone.Core.Notifications.Mailgun {
namespace NzbDrone.Core.Notifications.Mailgun
{
public interface IMailgunProxy
{
void SendNotification(string tittle, string message, MailgunSettings settings);
void SendNotification(string title, string message, MailgunSettings settings);
}
public class MailgunProxy : IMailgunProxy
{
private readonly IHttpClient _httpClient;
private readonly Logger _logger;
private const string BaseUrlEu = "https://api.eu.mailgun.net/v3";
private const string BaseUrlUs = "https://api.mailgun.net/v3";
private readonly IHttpClient _httpClient;
private readonly Logger _logger;
public MailgunProxy(IHttpClient httpClient, Logger logger)
{
@ -27,42 +27,41 @@ namespace NzbDrone.Core.Notifications.Mailgun {
{
try
{
var request = BuildRequest(settings, $"{settings.SenderDomain}/messages", HttpMethod.POST,
title, message).Build();
var request = BuildRequest(settings, $"{settings.SenderDomain}/messages", HttpMethod.POST, title, message).Build();
_httpClient.Execute(request);
}
catch (HttpException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
throw new MailgunException("Unauthorised - ApiKey is invalid");
_logger.Error("Unathorized - ApiKey is invalid");
throw new MailgunException("Unauthorized - ApiKey is invalid");
}
_logger.Error(ex, "Unable to connect to Mailgun. Status code: " + ex.Message);
throw new MailgunException("Unable to connect to Mailgun. Status code: {0}", ex);
}
}
private HttpRequestBuilder BuildRequest(MailgunSettings settings, string resource, HttpMethod method,
string messageSubject, string messageBody)
private HttpRequestBuilder BuildRequest(MailgunSettings settings, string resource, HttpMethod method, string messageSubject, string messageBody)
{
var loginCredentials = new NetworkCredential("api", settings.ApiKey);
var url = settings.UseEuEndpoint ? BaseUrlEu : BaseUrlUs;
var requestBuilder = new HttpRequestBuilder(url).Resource(resource);
requestBuilder.Method = method;
requestBuilder.NetworkCredential = loginCredentials;
requestBuilder.AddFormParameter("from", $"{settings.From}");
foreach (var recipient in settings.Recipients)
{
{
requestBuilder.AddFormParameter("to", $"{recipient}");
}
requestBuilder.AddFormParameter("subject", $"{messageSubject}");
requestBuilder.AddFormParameter("text", $"{messageBody}");
return requestBuilder;
}
}

View File

@ -15,7 +15,7 @@ namespace NzbDrone.Core.Notifications.Mailgun
RuleFor(c => c.Recipients).NotEmpty();
}
}
public class MailgunSettings : IProviderConfig
{
private static readonly MailGunSettingsValidator Validator = new MailGunSettingsValidator();
@ -24,20 +24,19 @@ namespace NzbDrone.Core.Notifications.Mailgun
{
Recipients = new string[] { };
}
[FieldDefinition(0, Label = "API Key", HelpText = "The API key generated from MailGun")]
public string ApiKey { get; set; }
[FieldDefinition(1, Label = "Use EU Endpoint?", HelpText = "You can choose to use the EU MailGun endpoint with this", Type = FieldType.Checkbox)]
[FieldDefinition(1, Label = "Use EU Endpoint?", HelpText = "Use the EU MailGun endpoint", Type = FieldType.Checkbox)]
public bool UseEuEndpoint { get; set; }
[FieldDefinition(2, Label = "From Address")]
public string From { get; set; }
[FieldDefinition(3, Label = "Sender Domain")]
public string SenderDomain { get; set; }
[FieldDefinition(4, Label = "Recipient Address(es)", Type = FieldType.Tag)]
public IEnumerable<string> Recipients { get; set; }