mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-24 22:35:39 -04:00
Convert Notifications from RestSharp to HttpClient
Signed-off-by: Robin Dadswell <robin@dadswell.email>
This commit is contained in:
@@ -5,10 +5,8 @@ using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Rest;
|
||||
using RestSharp;
|
||||
using RestSharp.Authenticators;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.PushBullet
|
||||
{
|
||||
@@ -23,12 +21,12 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
{
|
||||
private const string PUSH_URL = "https://api.pushbullet.com/v2/pushes";
|
||||
private const string DEVICE_URL = "https://api.pushbullet.com/v2/devices";
|
||||
private readonly IRestClientFactory _restClientFactory;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public PushBulletProxy(IRestClientFactory restClientFactory, Logger logger)
|
||||
public PushBulletProxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_restClientFactory = restClientFactory;
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@@ -98,15 +96,18 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = _restClientFactory.BuildClient(DEVICE_URL);
|
||||
var request = new RestRequest(Method.GET);
|
||||
var requestBuilder = new HttpRequestBuilder(DEVICE_URL);
|
||||
|
||||
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty);
|
||||
var response = client.ExecuteAndValidate(request);
|
||||
var request = requestBuilder.Build();
|
||||
|
||||
request.Method = HttpMethod.GET;
|
||||
request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);
|
||||
|
||||
var response = _httpClient.Execute(request);
|
||||
|
||||
return Json.Deserialize<PushBulletDevicesResponse>(response.Content).Devices;
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
@@ -127,7 +128,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
|
||||
SendNotification(title, body, settings);
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
@@ -147,50 +148,60 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
||||
return null;
|
||||
}
|
||||
|
||||
private RestRequest BuildDeviceRequest(string deviceId)
|
||||
private HttpRequestBuilder BuildDeviceRequest(string deviceId)
|
||||
{
|
||||
var request = new RestRequest(Method.POST);
|
||||
var requestBuilder = new HttpRequestBuilder(PUSH_URL).Post();
|
||||
long integerId;
|
||||
|
||||
if (deviceId.IsNullOrWhiteSpace())
|
||||
{
|
||||
return requestBuilder;
|
||||
}
|
||||
|
||||
if (long.TryParse(deviceId, out integerId))
|
||||
{
|
||||
request.AddParameter("device_id", integerId);
|
||||
requestBuilder.AddFormParameter("device_id", integerId);
|
||||
}
|
||||
else
|
||||
{
|
||||
request.AddParameter("device_iden", deviceId);
|
||||
requestBuilder.AddFormParameter("device_iden", deviceId);
|
||||
}
|
||||
|
||||
return request;
|
||||
return requestBuilder;
|
||||
}
|
||||
|
||||
private RestRequest BuildChannelRequest(string channelTag)
|
||||
private HttpRequestBuilder BuildChannelRequest(string channelTag)
|
||||
{
|
||||
var request = new RestRequest(Method.POST);
|
||||
request.AddParameter("channel_tag", channelTag);
|
||||
var requestBuilder = new HttpRequestBuilder(PUSH_URL).Post();
|
||||
|
||||
return request;
|
||||
if (channelTag.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
requestBuilder.AddFormParameter("channel_tag", channelTag);
|
||||
}
|
||||
|
||||
return requestBuilder;
|
||||
}
|
||||
|
||||
private void SendNotification(string title, string message, RestRequest request, PushBulletSettings settings)
|
||||
private void SendNotification(string title, string message, HttpRequestBuilder requestBuilder, PushBulletSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = _restClientFactory.BuildClient(PUSH_URL);
|
||||
|
||||
request.AddParameter("type", "note");
|
||||
request.AddParameter("title", title);
|
||||
request.AddParameter("body", message);
|
||||
requestBuilder.AddFormParameter("type", "note")
|
||||
.AddFormParameter("title", title)
|
||||
.AddFormParameter("body", message);
|
||||
|
||||
if (settings.SenderId.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
request.AddParameter("source_device_iden", settings.SenderId);
|
||||
requestBuilder.AddFormParameter("source_device_iden", settings.SenderId);
|
||||
}
|
||||
|
||||
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty);
|
||||
client.ExecuteAndValidate(request);
|
||||
var request = requestBuilder.Build();
|
||||
|
||||
request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);
|
||||
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user