From ac346848504ecb6d4c58f9dd70dddd0f3b9cfd97 Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 13 Apr 2020 00:10:38 -0400 Subject: [PATCH] New: SendGrid Support --- .../Notifications/SendGrid/SendGrid.cs | 57 +++++++++++++ .../SendGrid/SendGridException.cs | 18 ++++ .../Notifications/SendGrid/SendGridPayload.cs | 50 +++++++++++ .../Notifications/SendGrid/SendGridProxy.cs | 84 +++++++++++++++++++ .../SendGrid/SendGridSettings.cs | 46 ++++++++++ 5 files changed, 255 insertions(+) create mode 100644 src/NzbDrone.Core/Notifications/SendGrid/SendGrid.cs create mode 100644 src/NzbDrone.Core/Notifications/SendGrid/SendGridException.cs create mode 100644 src/NzbDrone.Core/Notifications/SendGrid/SendGridPayload.cs create mode 100644 src/NzbDrone.Core/Notifications/SendGrid/SendGridProxy.cs create mode 100644 src/NzbDrone.Core/Notifications/SendGrid/SendGridSettings.cs diff --git a/src/NzbDrone.Core/Notifications/SendGrid/SendGrid.cs b/src/NzbDrone.Core/Notifications/SendGrid/SendGrid.cs new file mode 100644 index 000000000..02ba76af8 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/SendGrid/SendGrid.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using FluentValidation.Results; +using NLog; + +namespace NzbDrone.Core.Notifications.SendGrid +{ + public class SendGrid : NotificationBase + { + private readonly ISendGridProxy _proxy; + private readonly Logger _logger; + + public SendGrid(ISendGridProxy proxy, Logger logger) + { + _proxy = proxy; + _logger = logger; + } + + public override string Name => "SendGrid"; + public override string Link => "https://sendgrid.com/"; + + public override void OnGrab(GrabMessage grabMessage) + { + _proxy.SendNotification(MOVIE_GRABBED_TITLE, grabMessage.Message, Settings); + } + + public override void OnDownload(DownloadMessage message) + { + _proxy.SendNotification(MOVIE_DOWNLOADED_TITLE, message.Message, Settings); + } + + public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck) + { + _proxy.SendNotification(HEALTH_ISSUE_TITLE, healthCheck.Message, Settings); + } + + public override ValidationResult Test() + { + var failures = new List(); + + try + { + const string title = "Test Notification"; + const string body = "This is a test message from Radarr"; + + _proxy.SendNotification(title, body, Settings); + } + catch (Exception ex) + { + _logger.Error(ex, "Unable to send test message"); + failures.Add(new ValidationFailure("", "Unable to send test message")); + } + + return new ValidationResult(failures); + } + } +} diff --git a/src/NzbDrone.Core/Notifications/SendGrid/SendGridException.cs b/src/NzbDrone.Core/Notifications/SendGrid/SendGridException.cs new file mode 100644 index 000000000..6cb799a2b --- /dev/null +++ b/src/NzbDrone.Core/Notifications/SendGrid/SendGridException.cs @@ -0,0 +1,18 @@ +using System; +using NzbDrone.Common.Exceptions; + +namespace NzbDrone.Core.Notifications.SendGrid +{ + public class SendGridException : NzbDroneException + { + public SendGridException(string message) + : base(message) + { + } + + public SendGridException(string message, Exception innerException, params object[] args) + : base(message, innerException, args) + { + } + } +} diff --git a/src/NzbDrone.Core/Notifications/SendGrid/SendGridPayload.cs b/src/NzbDrone.Core/Notifications/SendGrid/SendGridPayload.cs new file mode 100644 index 000000000..64e8e6243 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/SendGrid/SendGridPayload.cs @@ -0,0 +1,50 @@ +using System.Collections.Generic; + +namespace NzbDrone.Core.Notifications.SendGrid +{ + public class SendGridPayload + { + public SendGridPayload() + { + Personalizations = new List(); + Content = new List(); + } + + public List Content { get; set; } + public SendGridEmail From { get; set; } + public List Personalizations { get; set; } + } + + public class SendGridContent + { + public string Type { get; set; } + public string Value { get; set; } + } + + public class SendGridEmail + { + public string Email { get; set; } + } + + public class SendGridPersonalization + { + public SendGridPersonalization() + { + To = new List(); + } + + public List To { get; set; } + public string Subject { get; set; } + } + + public class SendGridSenderResponse + { + public List Result { get; set; } + } + + public class SendGridSender + { + public SendGridEmail From { get; set; } + public string Nickname { get; set; } + } +} diff --git a/src/NzbDrone.Core/Notifications/SendGrid/SendGridProxy.cs b/src/NzbDrone.Core/Notifications/SendGrid/SendGridProxy.cs new file mode 100644 index 000000000..ed98bfc4c --- /dev/null +++ b/src/NzbDrone.Core/Notifications/SendGrid/SendGridProxy.cs @@ -0,0 +1,84 @@ +using System.Net; +using NzbDrone.Common.Http; +using NzbDrone.Common.Serializer; + +namespace NzbDrone.Core.Notifications.SendGrid +{ + public interface ISendGridProxy + { + void SendNotification(string title, string message, SendGridSettings settings); + } + + public class SendGridProxy : ISendGridProxy + { + private readonly IHttpClient _httpClient; + + public SendGridProxy(IHttpClient httpClient) + { + _httpClient = httpClient; + } + + public void SendNotification(string title, string message, SendGridSettings settings) + { + try + { + var request = BuildRequest(settings, "mail/send", HttpMethod.POST); + + var payload = new SendGridPayload + { + From = new SendGridEmail + { + Email = settings.From + } + }; + + payload.Content.Add(new SendGridContent + { + Type = "text/plain", + Value = message + }); + + var personalization = new SendGridPersonalization + { + Subject = title, + }; + + foreach (var recipient in settings.Recipients) + { + personalization.To.Add(new SendGridEmail + { + Email = recipient + }); + } + + payload.Personalizations.Add(personalization); + + request.SetContent(payload.ToJson()); + + _httpClient.Execute(request); + } + catch (HttpException ex) + { + if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) + { + throw new SendGridException("Unauthorized - AuthToken is invalid"); + } + + throw new SendGridException("Unable to connect to Gotify. Status Code: {0}", ex); + } + } + + private HttpRequest BuildRequest(SendGridSettings settings, string resource, HttpMethod method) + { + var request = new HttpRequestBuilder(settings.BaseUrl).Resource(resource) + .SetHeader("Authorization", $"Bearer {settings.ApiKey}") + .Build(); + + request.Headers.ContentType = "application/json"; + + request.Method = method; + + return request; + } + } +} diff --git a/src/NzbDrone.Core/Notifications/SendGrid/SendGridSettings.cs b/src/NzbDrone.Core/Notifications/SendGrid/SendGridSettings.cs new file mode 100644 index 000000000..41e1ca525 --- /dev/null +++ b/src/NzbDrone.Core/Notifications/SendGrid/SendGridSettings.cs @@ -0,0 +1,46 @@ +using System.Collections.Generic; +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.Notifications.SendGrid +{ + public class SendGridSettingsValidator : AbstractValidator + { + public SendGridSettingsValidator() + { + RuleFor(c => c.ApiKey).NotEmpty(); + RuleFor(c => c.From).NotEmpty().EmailAddress(); + RuleFor(c => c.Recipients).NotEmpty(); + RuleForEach(c => c.Recipients).NotEmpty().EmailAddress(); + } + } + + public class SendGridSettings : IProviderConfig + { + private static readonly SendGridSettingsValidator Validator = new SendGridSettingsValidator(); + + public SendGridSettings() + { + BaseUrl = "https://api.sendgrid.com/v3/"; + Recipients = new string[] { }; + } + + public string BaseUrl { get; set; } + + [FieldDefinition(1, Label = "API Key", HelpText = "The API Key generated by SendGrid")] + public string ApiKey { get; set; } + + [FieldDefinition(2, Label = "From Address")] + public string From { get; set; } + + [FieldDefinition(3, Label = "Recipient Address(es)", Type = FieldType.Tag)] + public IEnumerable Recipients { get; set; } + + public NzbDroneValidationResult Validate() + { + return new NzbDroneValidationResult(Validator.Validate(this)); + } + } +}