Fixed: RestClient does not use global proxy settings

Co-Authored-By: Petr Shurgalin <pshurgal@users.noreply.github.com>
Co-Authored-By: taloth <taloth@users.noreply.github.com>
This commit is contained in:
Петр Шургалин
2020-01-16 13:38:26 +03:00
committed by Qstick
parent c45cff87fc
commit 1e4bdcc324
12 changed files with 95 additions and 34 deletions

View File

@@ -1,9 +1,10 @@
using System;
using System;
using System.Net;
using System.Web;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http.Proxy;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Rest;
using RestSharp;
@@ -19,10 +20,12 @@ namespace NzbDrone.Core.Notifications.Telegram
public class TelegramProxy : ITelegramProxy
{
private const string URL = "https://api.telegram.org";
private readonly IRestClientFactory _restClientFactory;
private readonly Logger _logger;
public TelegramProxy(Logger logger)
public TelegramProxy(IRestClientFactory restClientFactory, Logger logger)
{
_restClientFactory = restClientFactory;
_logger = logger;
}
@@ -30,7 +33,8 @@ 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 client = _restClientFactory.BuildClient(URL);
var request = new RestRequest("bot{token}/sendmessage", Method.POST);
request.AddUrlSegment("token", settings.BotToken);
@@ -54,9 +58,11 @@ namespace NzbDrone.Core.Notifications.Telegram
{
_logger.Error(ex, "Unable to send test message");
var restException = ex as RestException;
if (restException != null && restException.Response.StatusCode == HttpStatusCode.BadRequest)
if (ex is WebException webException)
{
return new ValidationFailure("Connection", $"{webException.Status.ToString()}: {webException.Message}");
}
else if (ex is RestException restException && restException.Response.StatusCode == HttpStatusCode.BadRequest)
{
var error = Json.Deserialize<TelegramError>(restException.Response.Content);
var property = error.Description.ContainsIgnoreCase("chat not found") ? "ChatId" : "BotToken";