Remove Prowlin Dependency

This commit is contained in:
Qstick 2020-08-05 22:28:03 -04:00 committed by Mark McDowall
parent 3fe659587f
commit c77c65c68a
8 changed files with 111 additions and 134 deletions

View File

@ -9,23 +9,27 @@ namespace NzbDrone.Core.Test.NotificationTests
[Explicit] [Explicit]
[ManualTest] [ManualTest]
[TestFixture] [TestFixture]
public class ProwlProviderTest : CoreTest<ProwlService> public class ProwlProviderTest : CoreTest<ProwlProxy>
{ {
private const string _apiKey = "66e9f688b512152eb2688f0486ae542c76e564a2"; private const string _apiKey = "66e9f688b512152eb2688f0486ae542c76e564a2";
private const string _badApiKey = "1234567890abcdefghijklmnopqrstuvwxyz1234"; private const string _badApiKey = "1234567890abcdefghijklmnopqrstuvwxyz1234";
private ProwlSettings _settings = new ProwlSettings { ApiKey = _apiKey };
[Test] [Test]
public void Verify_should_not_throw_for_a_valid_apiKey() public void Verify_should_not_throw_for_a_valid_apiKey()
{ {
Subject.Verify(_apiKey); Subject.Test(_settings);
ExceptionVerification.ExpectedWarns(0); ExceptionVerification.ExpectedWarns(0);
} }
[Test] [Test]
public void Verify_should_throw_for_an_invalid_apiKey() public void Verify_should_throw_for_an_invalid_apiKey()
{ {
Assert.Throws<InvalidApiKeyException>(() => Subject.Verify(_badApiKey)); _settings.ApiKey = _badApiKey;
Assert.Throws<ProwlException>(() => Subject.Test(_settings));
ExceptionVerification.ExpectedWarns(1); ExceptionVerification.ExpectedWarns(1);
} }

View File

@ -1,15 +0,0 @@
using System;
namespace NzbDrone.Core.Notifications.Prowl
{
public class InvalidApiKeyException : Exception
{
public InvalidApiKeyException()
{
}
public InvalidApiKeyException(string message) : base(message)
{
}
}
}

View File

@ -1,18 +1,16 @@
using System.Collections.Generic; using System.Collections.Generic;
using FluentValidation.Results; using FluentValidation.Results;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Tv;
using Prowlin;
namespace NzbDrone.Core.Notifications.Prowl namespace NzbDrone.Core.Notifications.Prowl
{ {
public class Prowl : NotificationBase<ProwlSettings> public class Prowl : NotificationBase<ProwlSettings>
{ {
private readonly IProwlService _prowlService; private readonly IProwlProxy _prowlProxy;
public Prowl(IProwlService prowlService) public Prowl(IProwlProxy prowlProxy)
{ {
_prowlService = prowlService; _prowlProxy = prowlProxy;
} }
public override string Link => "https://www.prowlapp.com/"; public override string Link => "https://www.prowlapp.com/";
@ -20,24 +18,24 @@ namespace NzbDrone.Core.Notifications.Prowl
public override void OnGrab(GrabMessage grabMessage) public override void OnGrab(GrabMessage grabMessage)
{ {
_prowlService.SendNotification(EPISODE_GRABBED_TITLE, grabMessage.Message, Settings.ApiKey, (NotificationPriority)Settings.Priority); _prowlProxy.SendNotification(EPISODE_GRABBED_TITLE, grabMessage.Message, Settings.ApiKey, (ProwlPriority)Settings.Priority);
} }
public override void OnDownload(DownloadMessage message) public override void OnDownload(DownloadMessage message)
{ {
_prowlService.SendNotification(EPISODE_DOWNLOADED_TITLE, message.Message, Settings.ApiKey, (NotificationPriority)Settings.Priority); _prowlProxy.SendNotification(EPISODE_DOWNLOADED_TITLE, message.Message, Settings.ApiKey, (ProwlPriority)Settings.Priority);
} }
public override void OnHealthIssue(HealthCheck.HealthCheck message) public override void OnHealthIssue(HealthCheck.HealthCheck message)
{ {
_prowlService.SendNotification(HEALTH_ISSUE_TITLE, message.Message, Settings.ApiKey, (NotificationPriority)Settings.Priority); _prowlProxy.SendNotification(HEALTH_ISSUE_TITLE, message.Message, Settings.ApiKey, (ProwlPriority)Settings.Priority);
} }
public override ValidationResult Test() public override ValidationResult Test()
{ {
var failures = new List<ValidationFailure>(); var failures = new List<ValidationFailure>();
failures.AddIfNotNull(_prowlService.Test(Settings)); failures.AddIfNotNull(_prowlProxy.Test(Settings));
return new ValidationResult(failures); return new ValidationResult(failures);
} }

View File

@ -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)
{
}
}
}

View File

@ -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 Sonarr";
SendNotification(title, body, settings.ApiKey);
}
catch (Exception ex)
{
return new ValidationFailure("ApiKey", ex.Message);
}
return null;
}
}
}

View File

@ -1,105 +0,0 @@
using System;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.EnvironmentInfo;
using Prowlin;
namespace NzbDrone.Core.Notifications.Prowl
{
public interface IProwlService
{
void SendNotification(string title, string message, string apiKey, NotificationPriority priority = NotificationPriority.Normal, string url = null);
ValidationFailure Test(ProwlSettings settings);
}
public class ProwlService : IProwlService
{
private readonly Logger _logger;
public ProwlService(Logger logger)
{
_logger = logger;
}
public void SendNotification(string title, string message, string apiKey, NotificationPriority priority = NotificationPriority.Normal, string url = null)
{
try
{
var notification = new Prowlin.Notification
{
Application = BuildInfo.AppName,
Description = message,
Event = title,
Priority = priority,
Url = url
};
notification.AddApiKey(apiKey.Trim());
var client = new ProwlClient();
_logger.Debug("Sending Prowl Notification");
var notificationResult = client.SendNotification(notification);
if (!string.IsNullOrWhiteSpace(notificationResult.ErrorMessage))
{
throw new InvalidApiKeyException("API Key: " + apiKey + " is invalid");
}
}
catch (Exception ex)
{
_logger.Debug(ex, ex.Message);
_logger.Warn("Invalid API Key: {0}", apiKey);
}
}
public void Verify(string apiKey)
{
try
{
var verificationRequest = new Verification();
verificationRequest.ApiKey = apiKey;
var client = new ProwlClient();
_logger.Debug("Verifying API Key: {0}", apiKey);
var verificationResult = client.SendVerification(verificationRequest);
if (!string.IsNullOrWhiteSpace(verificationResult.ErrorMessage) &&
verificationResult.ResultCode != "200")
{
throw new InvalidApiKeyException("API Key: " + apiKey + " is invalid");
}
}
catch (Exception ex)
{
_logger.Debug(ex, ex.Message);
_logger.Warn("Invalid API Key: {0}", apiKey);
throw new InvalidApiKeyException("API Key: " + apiKey + " is invalid");
}
}
public ValidationFailure Test(ProwlSettings settings)
{
try
{
Verify(settings.ApiKey);
const string title = "Test Notification";
const string body = "This is a test message from Sonarr";
SendNotification(title, body, settings.ApiKey);
}
catch (Exception ex)
{
_logger.Error(ex, "Unable to send test message");
return new ValidationFailure("ApiKey", "Unable to send test message");
}
return null;
}
}
}

View File

@ -20,7 +20,7 @@ namespace NzbDrone.Core.Notifications.Prowl
[FieldDefinition(0, Label = "API Key", HelpLink = "https://www.prowlapp.com/api_settings.php")] [FieldDefinition(0, Label = "API Key", HelpLink = "https://www.prowlapp.com/api_settings.php")]
public string ApiKey { get; set; } public string ApiKey { get; set; }
[FieldDefinition(1, Label = "Priority", Type = FieldType.Select, SelectOptions= typeof(ProwlPriority) )] [FieldDefinition(1, Label = "Priority", Type = FieldType.Select, SelectOptions = typeof(ProwlPriority))]
public int Priority { get; set; } public int Priority { get; set; }
public bool IsValid => !string.IsNullOrWhiteSpace(ApiKey) && Priority >= -2 && Priority <= 2; public bool IsValid => !string.IsNullOrWhiteSpace(ApiKey) && Priority >= -2 && Priority <= 2;

View File

@ -11,7 +11,6 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="NLog" Version="4.6.6" /> <PackageReference Include="NLog" Version="4.6.6" />
<PackageReference Include="OAuth" Version="1.0.3" /> <PackageReference Include="OAuth" Version="1.0.3" />
<PackageReference Include="Prowlin" Version="0.9.4456.26422" />
<PackageReference Include="RestSharp" Version="106.6.10" /> <PackageReference Include="RestSharp" Version="106.6.10" />
<PackageReference Include="TinyTwitter" Version="1.1.2" /> <PackageReference Include="TinyTwitter" Version="1.1.2" />
<PackageReference Include="xmlrpcnet" Version="3.0.0.266" /> <PackageReference Include="xmlrpcnet" Version="3.0.0.266" />