New: Choose PushBullet device from the UI

pull/2789/head
Mark McDowall 6 years ago committed by Taloth Saldono
parent d52b63e5e9
commit a86397f4bd

@ -35,6 +35,7 @@ namespace NzbDrone.Core.Annotations
Action,
Url,
Captcha,
OAuth
OAuth,
Device
}
}

@ -1,6 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Notifications.PushBullet
{
@ -16,7 +19,6 @@ namespace NzbDrone.Core.Notifications.PushBullet
public override string Name => "Pushbullet";
public override string Link => "https://www.pushbullet.com/";
public override void OnGrab(GrabMessage grabMessage)
{
_proxy.SendNotification(EPISODE_GRABBED_TITLE_BRANDED, grabMessage.Message, Settings);
@ -35,5 +37,36 @@ namespace NzbDrone.Core.Notifications.PushBullet
return new ValidationResult(failures);
}
public override object RequestAction(string action, IDictionary<string, string> query)
{
if (action == "getDevices")
{
// Return early if there is not an API key
if (Settings.ApiKey.IsNullOrWhiteSpace())
{
return new
{
devices = new List<object>()
};
}
Settings.Validate().Filter("ApiKey").ThrowOnError();
var devices = _proxy.GetDevices(Settings);
return new
{
devices = devices.Where(d => d.Nickname.IsNotNullOrWhiteSpace())
.OrderBy(d => d.Nickname, StringComparer.InvariantCultureIgnoreCase)
.Select(d => new
{
id = d.Id,
name = d.Nickname
})
};
}
return new { };
}
}
}

@ -0,0 +1,14 @@
using Newtonsoft.Json;
namespace NzbDrone.Core.Notifications.PushBullet
{
public class PushBulletDevice
{
[JsonProperty(PropertyName = "Iden")]
public string Id { get; set; }
public string Nickname { get; set; }
public string Manufacturer { get; set; }
public string Model { get; set; }
}
}

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace NzbDrone.Core.Notifications.PushBullet
{
public class PushBulletDevicesResponse
{
public List<PushBulletDevice> Devices { get; set; }
}
}

@ -1,4 +1,5 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
@ -6,6 +7,7 @@ using NLog;
using RestSharp;
using NzbDrone.Core.Rest;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Serializer;
using RestSharp.Authenticators;
namespace NzbDrone.Core.Notifications.PushBullet
@ -13,13 +15,15 @@ namespace NzbDrone.Core.Notifications.PushBullet
public interface IPushBulletProxy
{
void SendNotification(string title, string message, PushBulletSettings settings);
List<PushBulletDevice> GetDevices(PushBulletSettings settings);
ValidationFailure Test(PushBulletSettings settings);
}
public class PushBulletProxy : IPushBulletProxy
{
private readonly Logger _logger;
private const string URL = "https://api.pushbullet.com/v2/pushes";
private const string PUSH_URL = "https://api.pushbullet.com/v2/pushes";
private const string DEVICE_URL = "https://api.pushbullet.com/v2/devices";
public PushBulletProxy(Logger logger)
{
@ -88,6 +92,30 @@ namespace NzbDrone.Core.Notifications.PushBullet
}
}
public List<PushBulletDevice> GetDevices(PushBulletSettings settings)
{
try
{
var client = RestClientFactory.BuildClient(DEVICE_URL);
var request = new RestRequest(Method.GET);
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty);
var response = client.ExecuteAndValidate(request);
return Json.Deserialize<PushBulletDevicesResponse>(response.Content).Devices;
}
catch (RestException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.Error(ex, "Access token is invalid");
throw;
}
}
return new List<PushBulletDevice>();
}
public ValidationFailure Test(PushBulletSettings settings)
{
try
@ -147,7 +175,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
{
try
{
var client = RestClientFactory.BuildClient(URL);
var client = RestClientFactory.BuildClient(PUSH_URL);
request.AddParameter("type", "note");
request.AddParameter("title", title);
@ -165,7 +193,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.Error(ex, "API Key is invalid");
_logger.Error(ex, "Access token is invalid");
throw;
}

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.ThingiProvider;
@ -20,14 +20,14 @@ namespace NzbDrone.Core.Notifications.PushBullet
public PushBulletSettings()
{
DeviceIds = new string[]{};
ChannelTags = new string[]{};
DeviceIds = new string[] { };
ChannelTags = new string[] { };
}
[FieldDefinition(0, Label = "API Key", HelpLink = "https://www.pushbullet.com/")]
[FieldDefinition(0, Label = "Access Token", HelpLink = "https://www.pushbullet.com/#settings/account")]
public string ApiKey { get; set; }
[FieldDefinition(1, Label = "Device IDs", HelpText = "List of device IDs, use device_iden in the device's URL on pushbullet.com (leave blank to send to all devices)", Type = FieldType.Tag)]
[FieldDefinition(1, Label = "Device IDs", HelpText = "List of device IDs (leave blank to send to all devices)", Type = FieldType.Device)]
public IEnumerable<string> DeviceIds { get; set; }
[FieldDefinition(2, Label = "Channel Tags", HelpText = "List of Channel Tags to send notifications to", Type = FieldType.Tag)]

@ -778,6 +778,8 @@
<Compile Include="Notifications\Plex\PlexTv\PlexTvPinResponse.cs" />
<Compile Include="Notifications\Plex\PlexTv\PlexTvProxy.cs" />
<Compile Include="Notifications\Plex\PlexTv\PlexTvService.cs" />
<Compile Include="Notifications\PushBullet\PushBulletDevice.cs" />
<Compile Include="Notifications\PushBullet\PushBulletDevicesResponse.cs" />
<Compile Include="Profiles\Languages\LanguageProfile.cs" />
<Compile Include="Profiles\Languages\LanguageProfileInUseException.cs" />
<Compile Include="Lifecycle\ApplicationShutdownRequested.cs" />
@ -1330,4 +1332,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Loading…
Cancel
Save