mirror of https://github.com/Sonarr/Sonarr
Convert Notifications from RestSharp to HttpClient
This commit is contained in:
parent
51528f63e9
commit
665b80f15c
|
@ -1,10 +1,9 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Common.Http;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Boxcar
|
||||
{
|
||||
|
@ -16,23 +15,21 @@ namespace NzbDrone.Core.Notifications.Boxcar
|
|||
|
||||
public class BoxcarProxy : IBoxcarProxy
|
||||
{
|
||||
private readonly IRestClientFactory _restClientFactory;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
private const string URL = "https://new.boxcar.io/api/notifications";
|
||||
|
||||
public BoxcarProxy(IRestClientFactory restClientFactory, Logger logger)
|
||||
public BoxcarProxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_restClientFactory = restClientFactory;
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void SendNotification(string title, string message, BoxcarSettings settings)
|
||||
{
|
||||
var request = new RestRequest(Method.POST);
|
||||
|
||||
try
|
||||
{
|
||||
SendNotification(title, message, request, settings);
|
||||
ProcessNotification(title, message, settings);
|
||||
}
|
||||
catch (BoxcarException ex)
|
||||
{
|
||||
|
@ -51,7 +48,7 @@ namespace NzbDrone.Core.Notifications.Boxcar
|
|||
SendNotification(title, body, settings);
|
||||
return null;
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
|
@ -69,21 +66,22 @@ namespace NzbDrone.Core.Notifications.Boxcar
|
|||
}
|
||||
}
|
||||
|
||||
private void SendNotification(string title, string message, RestRequest request, BoxcarSettings settings)
|
||||
private void ProcessNotification(string title, string message, BoxcarSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
var client = _restClientFactory.BuildClient(URL);
|
||||
var requestBuilder = new HttpRequestBuilder(URL).Post();
|
||||
|
||||
request.AddParameter("user_credentials", settings.Token);
|
||||
request.AddParameter("notification[title]", title);
|
||||
request.AddParameter("notification[long_message]", message);
|
||||
request.AddParameter("notification[source_name]", BuildInfo.AppName);
|
||||
request.AddParameter("notification[icon_url]", "https://raw.githubusercontent.com/Sonarr/Sonarr/7818f0c59b787312f0bcbc5c0eafc3c9dd7e5451/Logo/64.png");
|
||||
var request = requestBuilder.AddFormParameter("user_credentials", settings.Token)
|
||||
.AddFormParameter("notification[title]", title)
|
||||
.AddFormParameter("notification[long_message]", message)
|
||||
.AddFormParameter("notification[source_name]", BuildInfo.AppName)
|
||||
.AddFormParameter("notification[icon_url]", "https://raw.githubusercontent.com/Sonarr/Sonarr/phantom-develop/Logo/64.png")
|
||||
.Build();
|
||||
|
||||
client.ExecuteAndValidate(request);
|
||||
_httpClient.Post(request);
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
|
|
|
@ -2,7 +2,6 @@ using NLog;
|
|||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Notifications.Discord.Payloads;
|
||||
using NzbDrone.Core.Rest;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Discord
|
||||
{
|
||||
|
@ -36,7 +35,7 @@ namespace NzbDrone.Core.Notifications.Discord
|
|||
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
_logger.Error(ex, "Unable to post payload {0}", payload);
|
||||
throw new DiscordException("Unable to post payload", ex);
|
||||
|
|
|
@ -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,5 +1,5 @@
|
|||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
using System.Net;
|
||||
using NzbDrone.Common.Http;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Gotify
|
||||
{
|
||||
|
@ -10,24 +10,35 @@ namespace NzbDrone.Core.Notifications.Gotify
|
|||
|
||||
public class GotifyProxy : IGotifyProxy
|
||||
{
|
||||
private readonly IRestClientFactory _restClientFactory;
|
||||
private readonly IHttpClient _httpClient;
|
||||
|
||||
public GotifyProxy(IRestClientFactory restClientFactory)
|
||||
public GotifyProxy(IHttpClient httpClient)
|
||||
{
|
||||
_restClientFactory = restClientFactory;
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public void SendNotification(string title, string message, GotifySettings settings)
|
||||
{
|
||||
var client = _restClientFactory.BuildClient(settings.Server);
|
||||
var request = new RestRequest("message", Method.POST);
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestBuilder(settings.Server).Post()
|
||||
.AddFormParameter("token", settings.AppToken)
|
||||
.AddFormParameter("title", title)
|
||||
.AddFormParameter("message", message)
|
||||
.AddFormParameter("priority", settings.Priority)
|
||||
.Build();
|
||||
|
||||
request.AddQueryParameter("token", settings.AppToken);
|
||||
request.AddParameter("title", title);
|
||||
request.AddParameter("message", message);
|
||||
request.AddParameter("priority", settings.Priority);
|
||||
_httpClient.Post(request);
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
throw new GotifyException("Unauthorized - AuthToken is invalid");
|
||||
}
|
||||
|
||||
client.ExecuteAndValidate(request);
|
||||
throw new GotifyException("Unable to connect to Gotify. Status Code: {0}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Gotify
|
||||
{
|
||||
public class InvalidResponseException : Exception
|
||||
{
|
||||
public InvalidResponseException()
|
||||
{
|
||||
}
|
||||
|
||||
public InvalidResponseException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using FluentValidation.Results;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Growl
|
||||
{
|
||||
|
|
|
@ -4,6 +4,7 @@ using NLog;
|
|||
using NzbDrone.Common.Extensions;
|
||||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Join
|
||||
|
@ -16,23 +17,23 @@ namespace NzbDrone.Core.Notifications.Join
|
|||
|
||||
public class JoinProxy : IJoinProxy
|
||||
{
|
||||
private readonly IRestClientFactory _restClientFactory;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
private const string URL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?";
|
||||
|
||||
public JoinProxy(IRestClientFactory restClientFactory, Logger logger)
|
||||
public JoinProxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_restClientFactory = restClientFactory;
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void SendNotification(string title, string message, JoinSettings settings)
|
||||
{
|
||||
var request = new RestRequest(Method.GET);
|
||||
var method = HttpMethod.GET;
|
||||
|
||||
try
|
||||
{
|
||||
SendNotification(title, message, request, settings);
|
||||
SendNotification(title, message, method, settings);
|
||||
}
|
||||
catch (JoinException ex)
|
||||
{
|
||||
|
@ -61,7 +62,7 @@ namespace NzbDrone.Core.Notifications.Join
|
|||
_logger.Error(ex, "Unable to send test Join message.");
|
||||
return new ValidationFailure("ApiKey", ex.Message);
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
_logger.Error(ex, "Unable to send test Join message. Server connection failed.");
|
||||
return new ValidationFailure("ApiKey", "Unable to connect to Join API. Please try again later.");
|
||||
|
@ -73,32 +74,34 @@ namespace NzbDrone.Core.Notifications.Join
|
|||
}
|
||||
}
|
||||
|
||||
private void SendNotification(string title, string message, RestRequest request, JoinSettings settings)
|
||||
private void SendNotification(string title, string message, HttpMethod method, JoinSettings settings)
|
||||
{
|
||||
|
||||
var client = _restClientFactory.BuildClient(URL);
|
||||
var requestBuilder = new HttpRequestBuilder(URL);
|
||||
|
||||
if (settings.DeviceNames.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
request.AddParameter("deviceNames", settings.DeviceNames);
|
||||
requestBuilder.AddQueryParam("deviceNames", settings.DeviceNames);
|
||||
}
|
||||
else if (settings.DeviceIds.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
request.AddParameter("deviceIds", settings.DeviceIds);
|
||||
requestBuilder.AddQueryParam("deviceIds", settings.DeviceIds);
|
||||
}
|
||||
else
|
||||
{
|
||||
request.AddParameter("deviceId", "group.all");
|
||||
requestBuilder.AddQueryParam("deviceId", "group.all");
|
||||
}
|
||||
|
||||
request.AddParameter("apikey", settings.ApiKey);
|
||||
request.AddParameter("title", title);
|
||||
request.AddParameter("text", message);
|
||||
request.AddParameter("icon", "https://cdn.rawgit.com/Sonarr/Sonarr/develop/Logo/256.png"); // Use the Sonarr logo.
|
||||
request.AddParameter("smallicon", "https://cdn.rawgit.com/Sonarr/Sonarr/develop/Logo/96-Outline-White.png"); // 96x96px with outline at 88x88px on a transparent background.
|
||||
request.AddParameter("priority", settings.Priority);
|
||||
var request = requestBuilder.AddQueryParam("apikey", settings.ApiKey)
|
||||
.AddQueryParam("title", title)
|
||||
.AddQueryParam("text", message)
|
||||
.AddQueryParam("icon", "https://cdn.rawgit.com/Sonarr/Sonarr/phantom-develop/Logo/256.png") // Use the Radarr logo.
|
||||
.AddQueryParam("smallicon", "https://cdn.rawgit.com/Sonarr/Sonarr/phantom-develop/Logo/96-Outline-White.png") // 96x96px with outline at 88x88px on a transparent background.
|
||||
.AddQueryParam("priority", settings.Priority)
|
||||
.Build();
|
||||
|
||||
var response = client.ExecuteAndValidate(request);
|
||||
request.Method = method;
|
||||
|
||||
var response = _httpClient.Execute(request);
|
||||
var res = Json.Deserialize<JoinResponseModel>(response.Content);
|
||||
|
||||
if (res.success) return;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Rest;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Emby
|
||||
|
@ -43,7 +43,7 @@ namespace NzbDrone.Core.Notifications.Emby
|
|||
|
||||
Notify(settings, "Test from Sonarr", "Success! MediaBrowser has been successfully configured!");
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
|
|
|
@ -4,11 +4,9 @@ using System.Linq;
|
|||
using System.Net;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using RestSharp.Authenticators;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.PushBullet
|
||||
{
|
||||
|
@ -21,14 +19,14 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
|||
|
||||
public class PushBulletProxy : IPushBulletProxy
|
||||
{
|
||||
private readonly IRestClientFactory _restClientFactory;
|
||||
private readonly Logger _logger;
|
||||
private const string PUSH_URL = "https://api.pushbullet.com/v2/pushes";
|
||||
private const string DEVICE_URL = "https://api.pushbullet.com/v2/devices";
|
||||
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.AddBasicAuthentication(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,51 +148,61 @@ 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.AddBasicAuthentication(settings.ApiKey, string.Empty);
|
||||
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
|
|
|
@ -2,8 +2,7 @@ using System;
|
|||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
using NzbDrone.Common.Http;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Pushover
|
||||
{
|
||||
|
@ -15,40 +14,42 @@ namespace NzbDrone.Core.Notifications.Pushover
|
|||
|
||||
public class PushoverProxy : IPushoverProxy
|
||||
{
|
||||
private readonly IRestClientFactory _restClientFactory;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
private const string URL = "https://api.pushover.net/1/messages.json";
|
||||
|
||||
public PushoverProxy(IRestClientFactory restClientFactory, Logger logger)
|
||||
public PushoverProxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_restClientFactory = restClientFactory;
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void SendNotification(string title, string message, PushoverSettings settings)
|
||||
{
|
||||
var client = _restClientFactory.BuildClient(URL);
|
||||
var request = new RestRequest(Method.POST);
|
||||
request.AddParameter("token", settings.ApiKey);
|
||||
request.AddParameter("user", settings.UserKey);
|
||||
request.AddParameter("device", string.Join(",", settings.Devices));
|
||||
request.AddParameter("title", title);
|
||||
request.AddParameter("message", message);
|
||||
request.AddParameter("priority", settings.Priority);
|
||||
var requestBuilder = new HttpRequestBuilder(URL).Post();
|
||||
|
||||
requestBuilder.AddFormParameter("token", settings.ApiKey)
|
||||
.AddFormParameter("user", settings.UserKey)
|
||||
.AddFormParameter("device", string.Join(",", settings.Devices))
|
||||
.AddFormParameter("title", title)
|
||||
.AddFormParameter("message", message)
|
||||
.AddFormParameter("priority", settings.Priority);
|
||||
|
||||
if ((PushoverPriority)settings.Priority == PushoverPriority.Emergency)
|
||||
{
|
||||
request.AddParameter("retry", settings.Retry);
|
||||
request.AddParameter("expire", settings.Expire);
|
||||
requestBuilder.AddFormParameter("retry", settings.Retry);
|
||||
requestBuilder.AddFormParameter("expire", settings.Expire);
|
||||
}
|
||||
|
||||
if (!settings.Sound.IsNullOrWhiteSpace())
|
||||
{
|
||||
request.AddParameter("sound", settings.Sound);
|
||||
requestBuilder.AddFormParameter("sound", settings.Sound);
|
||||
}
|
||||
|
||||
|
||||
client.ExecuteAndValidate(request);
|
||||
var request = requestBuilder.Build();
|
||||
|
||||
_httpClient.Post(request);
|
||||
}
|
||||
|
||||
public ValidationFailure Test(PushoverSettings settings)
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
using NLog;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Notifications.Slack.Payloads;
|
||||
using NzbDrone.Core.Rest;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Slack
|
||||
{
|
||||
|
@ -36,7 +35,7 @@ namespace NzbDrone.Core.Notifications.Slack
|
|||
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
_logger.Error(ex, "Unable to post payload {0}", payload);
|
||||
throw new SlackExeption("Unable to post payload", ex);
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Web;
|
||||
using FluentValidation.Results;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Common.Http.Proxy;
|
||||
using RestSharp;
|
||||
using NzbDrone.Core.Rest;
|
||||
using System.Web;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Telegram
|
||||
{
|
||||
|
@ -19,13 +17,13 @@ namespace NzbDrone.Core.Notifications.Telegram
|
|||
|
||||
public class TelegramProxy : ITelegramProxy
|
||||
{
|
||||
private readonly IRestClientFactory _restClientFactory;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
private const string URL = "https://api.telegram.org";
|
||||
|
||||
public TelegramProxy(IRestClientFactory restClientFactory, Logger logger)
|
||||
public TelegramProxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_restClientFactory = restClientFactory;
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
@ -33,17 +31,16 @@ namespace NzbDrone.Core.Notifications.Telegram
|
|||
{
|
||||
//Format text to add the title before and bold using markdown
|
||||
var text = $"<b>{HttpUtility.HtmlEncode(title)}</b>\n{HttpUtility.HtmlEncode(message)}";
|
||||
var client = _restClientFactory.BuildClient(URL);
|
||||
|
||||
var request = new RestRequest("bot{token}/sendmessage", Method.POST);
|
||||
var requestBuilder = new HttpRequestBuilder(URL).Resource("bot{token}/sendmessage").Post();
|
||||
|
||||
request.AddUrlSegment("token", settings.BotToken);
|
||||
request.AddParameter("chat_id", settings.ChatId);
|
||||
request.AddParameter("parse_mode", "HTML");
|
||||
request.AddParameter("text", text);
|
||||
request.AddParameter("disable_notification", settings.SendSilently);
|
||||
var request = requestBuilder.SetSegment("token", settings.BotToken)
|
||||
.AddFormParameter("chat_id", settings.ChatId)
|
||||
.AddFormParameter("parse_mode", "HTML")
|
||||
.AddFormParameter("text", text)
|
||||
.Build();
|
||||
|
||||
client.ExecuteAndValidate(request);
|
||||
_httpClient.Post(request);
|
||||
}
|
||||
|
||||
public ValidationFailure Test(TelegramSettings settings)
|
||||
|
@ -63,7 +60,7 @@ namespace NzbDrone.Core.Notifications.Telegram
|
|||
{
|
||||
return new ValidationFailure("Connection", $"{webException.Status.ToString()}: {webException.Message}");
|
||||
}
|
||||
else if (ex is RestException restException && restException.Response.StatusCode == HttpStatusCode.BadRequest)
|
||||
else if (ex is Common.Http.HttpException restException && restException.Response.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
var error = Json.Deserialize<TelegramError>(restException.Response.Content);
|
||||
var property = error.Description.ContainsIgnoreCase("chat not found") ? "ChatId" : "BotToken";
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Rest;
|
||||
using NzbDrone.Common.Extensions;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Webhook
|
||||
|
@ -37,7 +36,7 @@ namespace NzbDrone.Core.Notifications.Webhook
|
|||
}
|
||||
_httpClient.Execute(request);
|
||||
}
|
||||
catch (RestException ex)
|
||||
catch (HttpException ex)
|
||||
{
|
||||
throw new WebhookException("Unable to post to webhook: {0}", ex, ex.Message);
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
using System.Collections.Generic;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
using NzbDrone.Core.Notifications.Xbmc.Model;
|
||||
using NzbDrone.Core.Rest;
|
||||
using RestSharp;
|
||||
using RestSharp.Authenticators;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Xbmc
|
||||
{
|
||||
|
@ -21,87 +19,66 @@ namespace NzbDrone.Core.Notifications.Xbmc
|
|||
|
||||
public class XbmcJsonApiProxy : IXbmcJsonApiProxy
|
||||
{
|
||||
private readonly IRestClientFactory _restClientFactory;
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public XbmcJsonApiProxy(IRestClientFactory restClientFactory, Logger logger)
|
||||
public XbmcJsonApiProxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_restClientFactory = restClientFactory;
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string GetJsonVersion(XbmcSettings settings)
|
||||
{
|
||||
var request = new RestRequest();
|
||||
return ProcessRequest(request, settings, "JSONRPC.Version");
|
||||
return ProcessRequest(settings, "JSONRPC.Version");
|
||||
}
|
||||
|
||||
public void Notify(XbmcSettings settings, string title, string message)
|
||||
{
|
||||
var request = new RestRequest();
|
||||
|
||||
var parameters = new Dictionary<string, object>();
|
||||
parameters.Add("title", title);
|
||||
parameters.Add("message", message);
|
||||
parameters.Add("image", "https://raw.github.com/Sonarr/Sonarr/develop/Logo/64.png");
|
||||
parameters.Add("displaytime", settings.DisplayTime * 1000);
|
||||
|
||||
ProcessRequest(request, settings, "GUI.ShowNotification", parameters);
|
||||
ProcessRequest(settings, "GUI.ShowNotification", title, message, "https://raw.github.com/Sonarr/Sonarr/phantom-develop/Logo/64.png", settings.DisplayTime * 1000);
|
||||
}
|
||||
|
||||
public string UpdateLibrary(XbmcSettings settings, string path)
|
||||
{
|
||||
var request = new RestRequest();
|
||||
var parameters = new Dictionary<string, object>();
|
||||
parameters.Add("directory", path);
|
||||
|
||||
if (path.IsNullOrWhiteSpace())
|
||||
{
|
||||
parameters = null;
|
||||
}
|
||||
|
||||
var response = ProcessRequest(request, settings, "VideoLibrary.Scan", parameters);
|
||||
var response = ProcessRequest(settings, "VideoLibrary.Scan", path);
|
||||
|
||||
return Json.Deserialize<XbmcJsonResult<string>>(response).Result;
|
||||
}
|
||||
|
||||
public void CleanLibrary(XbmcSettings settings)
|
||||
{
|
||||
var request = new RestRequest();
|
||||
|
||||
ProcessRequest(request, settings, "VideoLibrary.Clean");
|
||||
ProcessRequest(settings, "VideoLibrary.Clean");
|
||||
}
|
||||
|
||||
public List<ActivePlayer> GetActivePlayers(XbmcSettings settings)
|
||||
{
|
||||
var request = new RestRequest();
|
||||
|
||||
var response = ProcessRequest(request, settings, "Player.GetActivePlayers");
|
||||
var response = ProcessRequest(settings, "Player.GetActivePlayers");
|
||||
|
||||
return Json.Deserialize<ActivePlayersEdenResult>(response).Result;
|
||||
}
|
||||
|
||||
public List<TvShow> GetSeries(XbmcSettings settings)
|
||||
{
|
||||
var request = new RestRequest();
|
||||
var parameters = new Dictionary<string, object>();
|
||||
parameters.Add("properties", new[] { "file", "imdbnumber" });
|
||||
|
||||
var response = ProcessRequest(request, settings, "VideoLibrary.GetTvShows", parameters);
|
||||
var response = ProcessRequest(settings, "VideoLibrary.GetMovies", new[] { "file", "imdbnumber" });
|
||||
|
||||
return Json.Deserialize<TvShowResponse>(response).Result.TvShows;
|
||||
}
|
||||
|
||||
private string ProcessRequest(IRestRequest request, XbmcSettings settings, string method, Dictionary<string, object> parameters = null)
|
||||
private string ProcessRequest(XbmcSettings settings, string method, params object[] parameters)
|
||||
{
|
||||
var client = BuildClient(settings);
|
||||
var url = string.Format(@"http://{0}/jsonrpc", settings.Address);
|
||||
var requestBuilder = new JsonRpcRequestBuilder(url, method, parameters);
|
||||
|
||||
request.Method = Method.POST;
|
||||
request.RequestFormat = DataFormat.Json;
|
||||
request.JsonSerializer = new JsonNetSerializer();
|
||||
request.AddBody(new { jsonrpc = "2.0", method = method, id = 10, @params = parameters });
|
||||
requestBuilder.LogResponseContent = true;
|
||||
|
||||
var response = client.ExecuteAndValidate(request);
|
||||
var request = requestBuilder.Build();
|
||||
|
||||
if (!settings.Username.IsNullOrWhiteSpace())
|
||||
{
|
||||
request.AddBasicAuthentication(settings.Username, settings.Password);
|
||||
}
|
||||
|
||||
var response = _httpClient.Execute(request);
|
||||
_logger.Trace("Response: {0}", response.Content);
|
||||
|
||||
CheckForError(response);
|
||||
|
@ -109,20 +86,7 @@ namespace NzbDrone.Core.Notifications.Xbmc
|
|||
return response.Content;
|
||||
}
|
||||
|
||||
private IRestClient BuildClient(XbmcSettings settings)
|
||||
{
|
||||
var url = string.Format(@"http://{0}/jsonrpc", settings.Address);
|
||||
var client = _restClientFactory.BuildClient(url);
|
||||
|
||||
if (!settings.Username.IsNullOrWhiteSpace())
|
||||
{
|
||||
client.Authenticator = new HttpBasicAuthenticator(settings.Username, settings.Password);
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
private void CheckForError(IRestResponse response)
|
||||
private void CheckForError(HttpResponse response)
|
||||
{
|
||||
|
||||
if (string.IsNullOrWhiteSpace(response.Content))
|
||||
|
|
Loading…
Reference in New Issue