2021-10-02 15:15:54 +00:00
|
|
|
using System;
|
2024-01-03 20:41:16 +00:00
|
|
|
using System.Collections.Generic;
|
2021-10-02 15:15:54 +00:00
|
|
|
using FluentValidation.Results;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Http;
|
2024-01-03 20:41:16 +00:00
|
|
|
using NzbDrone.Core.Localization;
|
2021-10-02 15:15:54 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Notifications.Simplepush
|
|
|
|
{
|
|
|
|
public interface ISimplepushProxy
|
|
|
|
{
|
|
|
|
void SendNotification(string title, string message, SimplepushSettings settings);
|
|
|
|
ValidationFailure Test(SimplepushSettings settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class SimplepushProxy : ISimplepushProxy
|
|
|
|
{
|
|
|
|
private const string URL = "https://api.simplepush.io/send";
|
|
|
|
private readonly IHttpClient _httpClient;
|
2024-01-03 20:41:16 +00:00
|
|
|
private readonly ILocalizationService _localizationService;
|
2021-10-02 15:15:54 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2024-01-03 20:41:16 +00:00
|
|
|
public SimplepushProxy(IHttpClient httpClient, ILocalizationService localizationService, Logger logger)
|
2021-10-02 15:15:54 +00:00
|
|
|
{
|
|
|
|
_httpClient = httpClient;
|
2024-01-03 20:41:16 +00:00
|
|
|
_localizationService = localizationService;
|
2021-10-02 15:15:54 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendNotification(string title, string message, SimplepushSettings settings)
|
|
|
|
{
|
|
|
|
var requestBuilder = new HttpRequestBuilder(URL).Post();
|
|
|
|
|
|
|
|
requestBuilder.AddFormParameter("key", settings.Key)
|
|
|
|
.AddFormParameter("event", settings.Event)
|
|
|
|
.AddFormParameter("title", title)
|
|
|
|
.AddFormParameter("msg", message);
|
|
|
|
|
|
|
|
var request = requestBuilder.Build();
|
|
|
|
|
|
|
|
_httpClient.Post(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ValidationFailure Test(SimplepushSettings settings)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const string title = "Test Notification";
|
|
|
|
const string body = "This is a test message from Radarr";
|
|
|
|
|
|
|
|
SendNotification(title, body, settings);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, "Unable to send test message");
|
2024-01-03 20:41:16 +00:00
|
|
|
return new ValidationFailure("ApiKey", _localizationService.GetLocalizedString("NotificationsValidationUnableToSendTestMessage", new Dictionary<string, object> { { "exceptionMessage", ex.Message } }));
|
2021-10-02 15:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|