Signed-off-by: Robin Dadswell <robin@dadswell.email>pull/770/head
parent
ad11ef9d2a
commit
7842acc76e
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using NzbDrone.Common.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Gotify
|
||||
{
|
||||
public class GotifyException : NzbDroneException
|
||||
{
|
||||
public GotifyException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public GotifyException(string message, Exception innerException, params object[] args)
|
||||
: base(message, innerException, args)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Gotify
|
||||
{
|
||||
public class InvalidResponseException : Exception
|
||||
{
|
||||
public InvalidResponseException()
|
||||
{
|
||||
}
|
||||
|
||||
public InvalidResponseException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using FluentValidation.Results;
|
||||
using NzbDrone.Common.Extensions;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Prowl
|
||||
{
|
||||
public class Prowl : NotificationBase<ProwlSettings>
|
||||
{
|
||||
private readonly IProwlProxy _prowlProxy;
|
||||
|
||||
public Prowl(IProwlProxy prowlProxy)
|
||||
{
|
||||
_prowlProxy = prowlProxy;
|
||||
}
|
||||
|
||||
public override string Link => "https://www.prowlapp.com/";
|
||||
public override string Name => "Prowl";
|
||||
|
||||
public override void OnGrab(GrabMessage message)
|
||||
{
|
||||
_prowlProxy.SendNotification(BOOK_GRABBED_TITLE, message.Message, Settings.ApiKey);
|
||||
}
|
||||
|
||||
public override void OnReleaseImport(BookDownloadMessage message)
|
||||
{
|
||||
_prowlProxy.SendNotification(BOOK_DOWNLOADED_TITLE, message.Message, Settings.ApiKey);
|
||||
}
|
||||
|
||||
public override void OnHealthIssue(HealthCheck.HealthCheck healthCheck)
|
||||
{
|
||||
_prowlProxy.SendNotification(HEALTH_ISSUE_TITLE, healthCheck.Message, Settings.ApiKey, (ProwlPriority)Settings.Priority);
|
||||
}
|
||||
|
||||
public override ValidationResult Test()
|
||||
{
|
||||
var failures = new List<ValidationFailure>();
|
||||
|
||||
failures.AddIfNotNull(_prowlProxy.Test(Settings));
|
||||
|
||||
return new ValidationResult(failures);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using NzbDrone.Common.Exceptions;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Prowl
|
||||
{
|
||||
public class ProwlException : NzbDroneException
|
||||
{
|
||||
public ProwlException(string message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public ProwlException(string message, Exception innerException, params object[] args)
|
||||
: base(message, innerException, args)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace NzbDrone.Core.Notifications.Prowl
|
||||
{
|
||||
public enum ProwlPriority
|
||||
{
|
||||
VeryLow = -2,
|
||||
Low = -1,
|
||||
Normal = 0,
|
||||
High = 1,
|
||||
Emergency = 2
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Common.Http;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Prowl
|
||||
{
|
||||
public interface IProwlProxy
|
||||
{
|
||||
void SendNotification(string title, string message, string apiKey, ProwlPriority priority = ProwlPriority.Normal, string url = null);
|
||||
ValidationFailure Test(ProwlSettings settings);
|
||||
}
|
||||
|
||||
public class ProwlProxy : IProwlProxy
|
||||
{
|
||||
private const string PUSH_URL = "https://api.prowlapp.com/publicapi/add";
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public ProwlProxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void SendNotification(string title, string message, string apiKey, ProwlPriority priority = ProwlPriority.Normal, string url = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var requestBuilder = new HttpRequestBuilder(PUSH_URL);
|
||||
|
||||
var request = requestBuilder.Post()
|
||||
.AddFormParameter("apikey", apiKey)
|
||||
.AddFormParameter("application", BuildInfo.AppName)
|
||||
.AddFormParameter("event", title)
|
||||
.AddFormParameter("description", message)
|
||||
.AddFormParameter("priority", priority)
|
||||
.AddFormParameter("url", url)
|
||||
.Build();
|
||||
|
||||
_httpClient.Post(request);
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
_logger.Error(ex, "Apikey is invalid: {0}", apiKey);
|
||||
throw new ProwlException("Apikey is invalid", ex);
|
||||
}
|
||||
|
||||
throw new ProwlException("Unable to send text message: " + ex.Message, ex);
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
throw new ProwlException("Failed to connect to prowl, please check your settings.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ValidationFailure Test(ProwlSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
const string title = "Test Notification";
|
||||
const string body = "This is a test message from Readarr";
|
||||
|
||||
SendNotification(title, body, settings.ApiKey);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new ValidationFailure("ApiKey", ex.Message);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
using FluentValidation;
|
||||
using NzbDrone.Core.Annotations;
|
||||
using NzbDrone.Core.ThingiProvider;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Prowl
|
||||
{
|
||||
public class ProwlSettingsValidator : AbstractValidator<ProwlSettings>
|
||||
{
|
||||
public ProwlSettingsValidator()
|
||||
{
|
||||
RuleFor(c => c.ApiKey).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class ProwlSettings : IProviderConfig
|
||||
{
|
||||
private static readonly ProwlSettingsValidator Validator = new ProwlSettingsValidator();
|
||||
|
||||
[FieldDefinition(0, Label = "API Key", Privacy = PrivacyLevel.ApiKey, HelpLink = "https://www.prowlapp.com/api_settings.php")]
|
||||
public string ApiKey { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(ProwlPriority))]
|
||||
public int Priority { get; set; }
|
||||
|
||||
public bool IsValid => !string.IsNullOrWhiteSpace(ApiKey) && Priority >= -2 && Priority <= 2;
|
||||
|
||||
public NzbDroneValidationResult Validate()
|
||||
{
|
||||
return new NzbDroneValidationResult(Validator.Validate(this));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
using RestSharp;
|
||||
|
||||
namespace NzbDrone.Core.Rest
|
||||
{
|
||||
public interface IRestClientFactory
|
||||
{
|
||||
RestClient BuildClient(string baseUrl);
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Common.Http.Proxy;
|
||||
using RestSharp;
|
||||
|
||||
namespace NzbDrone.Core.Rest
|
||||
{
|
||||
public class RestClientFactory : IRestClientFactory
|
||||
{
|
||||
private readonly IHttpProxySettingsProvider _httpProxySettingsProvider;
|
||||
private readonly ICreateManagedWebProxy _createManagedWebProxy;
|
||||
|
||||
public RestClientFactory(IHttpProxySettingsProvider httpProxySettingsProvider, ICreateManagedWebProxy createManagedWebProxy)
|
||||
{
|
||||
_httpProxySettingsProvider = httpProxySettingsProvider;
|
||||
_createManagedWebProxy = createManagedWebProxy;
|
||||
}
|
||||
|
||||
public RestClient BuildClient(string baseUrl)
|
||||
{
|
||||
var restClient = new RestClient(baseUrl)
|
||||
{
|
||||
UserAgent = $"{BuildInfo.AppName}/{BuildInfo.Version} ({OsInfo.Os})"
|
||||
};
|
||||
|
||||
var proxySettings = _httpProxySettingsProvider.GetProxySettings();
|
||||
if (proxySettings != null)
|
||||
{
|
||||
restClient.Proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
|
||||
}
|
||||
|
||||
return restClient;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue