mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2026-04-22 22:34:53 -04:00
New: SendGrid Support
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user