mirror of
https://github.com/Radarr/Radarr.git
synced 2024-10-27 06:02:33 +01:00
Fixed: Qbittorrent api errors when only one of two seed criteria was configured
Fixed: Qbittorrent API errors when only one of two seed criteria was configured
This commit is contained in:
parent
bfc969c45c
commit
53b9332675
@ -190,6 +190,28 @@ public void should_throw_on_unsuccessful_status_codes(int statusCode)
|
||||
ExceptionVerification.IgnoreWarns();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_throw_on_suppressed_status_codes()
|
||||
{
|
||||
var request = new HttpRequest($"https://{_httpBinHost}/status/{HttpStatusCode.NotFound}");
|
||||
request.SuppressHttpErrorStatusCodes = new[] { HttpStatusCode.NotFound };
|
||||
|
||||
Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
|
||||
|
||||
ExceptionVerification.IgnoreWarns();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_log_unsuccessful_status_codes()
|
||||
{
|
||||
var request = new HttpRequest($"https://{_httpBinHost}/status/{HttpStatusCode.NotFound}");
|
||||
request.LogHttpError = false;
|
||||
|
||||
Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
|
||||
|
||||
ExceptionVerification.ExpectedWarns(0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_not_follow_redirects_when_not_in_production()
|
||||
{
|
||||
|
@ -83,9 +83,12 @@ public HttpResponse Execute(HttpRequest request)
|
||||
_logger.Error("Server requested a redirect to [{0}] while in developer mode. Update the request URL to avoid this redirect.", response.Headers["Location"]);
|
||||
}
|
||||
|
||||
if (!request.SuppressHttpError && response.HasHttpError)
|
||||
if (!request.SuppressHttpError && response.HasHttpError && (request.SuppressHttpErrorStatusCodes == null || !request.SuppressHttpErrorStatusCodes.Contains(response.StatusCode)))
|
||||
{
|
||||
if (request.LogHttpError)
|
||||
{
|
||||
_logger.Warn("HTTP Error - {0}", response);
|
||||
}
|
||||
|
||||
if ((int)response.StatusCode == 429)
|
||||
{
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using NzbDrone.Common.EnvironmentInfo;
|
||||
using NzbDrone.Common.Extensions;
|
||||
@ -15,6 +16,7 @@ public HttpRequest(string url, HttpAccept httpAccept = null)
|
||||
AllowAutoRedirect = true;
|
||||
StoreRequestCookie = true;
|
||||
IgnorePersistentCookies = false;
|
||||
LogHttpError = true;
|
||||
Cookies = new Dictionary<string, string>();
|
||||
|
||||
if (!RuntimeInfo.IsProduction)
|
||||
@ -34,10 +36,12 @@ public HttpRequest(string url, HttpAccept httpAccept = null)
|
||||
public byte[] ContentData { get; set; }
|
||||
public string ContentSummary { get; set; }
|
||||
public bool SuppressHttpError { get; set; }
|
||||
public IEnumerable<HttpStatusCode> SuppressHttpErrorStatusCodes { get; set; }
|
||||
public bool UseSimplifiedUserAgent { get; set; }
|
||||
public bool AllowAutoRedirect { get; set; }
|
||||
public bool ConnectionKeepAlive { get; set; }
|
||||
public bool LogResponseContent { get; set; }
|
||||
public bool LogHttpError { get; set; }
|
||||
public Dictionary<string, string> Cookies { get; private set; }
|
||||
public bool IgnorePersistentCookies { get; set; }
|
||||
public bool StoreRequestCookie { get; set; }
|
||||
|
@ -19,6 +19,7 @@ public class HttpRequestBuilder
|
||||
public Dictionary<string, string> Segments { get; private set; }
|
||||
public HttpHeader Headers { get; private set; }
|
||||
public bool SuppressHttpError { get; set; }
|
||||
public bool LogHttpError { get; set; }
|
||||
public bool UseSimplifiedUserAgent { get; set; }
|
||||
public bool AllowAutoRedirect { get; set; }
|
||||
public bool ConnectionKeepAlive { get; set; }
|
||||
@ -41,6 +42,7 @@ public HttpRequestBuilder(string baseUrl)
|
||||
Headers = new HttpHeader();
|
||||
Cookies = new Dictionary<string, string>();
|
||||
FormData = new List<HttpFormData>();
|
||||
LogHttpError = true;
|
||||
}
|
||||
|
||||
public HttpRequestBuilder(bool useHttps, string host, int port, string urlBase = null)
|
||||
@ -100,6 +102,7 @@ protected virtual void Apply(HttpRequest request)
|
||||
{
|
||||
request.Method = Method;
|
||||
request.SuppressHttpError = SuppressHttpError;
|
||||
request.LogHttpError = LogHttpError;
|
||||
request.UseSimplifiedUserAgent = UseSimplifiedUserAgent;
|
||||
request.AllowAutoRedirect = AllowAutoRedirect;
|
||||
request.ConnectionKeepAlive = ConnectionKeepAlive;
|
||||
|
@ -74,17 +74,21 @@ protected void GivenFailedDownload()
|
||||
Mocker.GetMock<IQBittorrentProxy>()
|
||||
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<TorrentSeedConfiguration>(), It.IsAny<QBittorrentSettings>()))
|
||||
.Throws<InvalidOperationException>();
|
||||
|
||||
Mocker.GetMock<IQBittorrentProxy>()
|
||||
.Setup(s => s.AddTorrentFromFile(It.IsAny<string>(), It.IsAny<byte[]>(), It.IsAny<TorrentSeedConfiguration>(), It.IsAny<QBittorrentSettings>()))
|
||||
.Throws<InvalidOperationException>();
|
||||
}
|
||||
|
||||
protected void GivenSuccessfulDownload()
|
||||
{
|
||||
Mocker.GetMock<IQBittorrentProxy>()
|
||||
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<TorrentSeedConfiguration>(), It.IsAny<QBittorrentSettings>()))
|
||||
.Setup(s => s.AddTorrentFromFile(It.IsAny<string>(), It.IsAny<byte[]>(), It.IsAny<TorrentSeedConfiguration>(), It.IsAny<QBittorrentSettings>()))
|
||||
.Callback(() =>
|
||||
{
|
||||
var torrent = new QBittorrentTorrent
|
||||
{
|
||||
Hash = "HASH",
|
||||
Hash = "CBC2F069FE8BB2F544EAE707D75BCD3DE9DCF951",
|
||||
Name = _title,
|
||||
Size = 1000,
|
||||
Progress = 1.0,
|
||||
@ -138,6 +142,10 @@ protected virtual void GivenTorrents(List<QBittorrentTorrent> torrents)
|
||||
.Setup(s => s.GetTorrentFiles(torrent.Hash.ToLower(), It.IsAny<QBittorrentSettings>()))
|
||||
.Returns(new List<QBittorrentTorrentFile> { new QBittorrentTorrentFile { Name = torrent.Name } });
|
||||
}
|
||||
|
||||
Mocker.GetMock<IQBittorrentProxy>()
|
||||
.Setup(s => s.IsTorrentLoaded(It.IsAny<string>(), It.IsAny<QBittorrentSettings>()))
|
||||
.Returns<string, QBittorrentSettings>((hash, s) => torrents.Any(v => v.Hash.ToLower() == hash));
|
||||
}
|
||||
|
||||
private void GivenTorrentFiles(string hash, List<QBittorrentTorrentFile> files)
|
||||
|
@ -89,6 +89,15 @@ protected override string AddFromMagnetLink(RemoteMovie remoteMovie, string hash
|
||||
if (!addHasSetShareLimits && setShareLimits)
|
||||
{
|
||||
Proxy.SetTorrentSeedingConfiguration(hash.ToLower(), remoteMovie.SeedConfiguration, Settings);
|
||||
|
||||
try
|
||||
{
|
||||
Proxy.SetTorrentSeedingConfiguration(hash.ToLower(), remoteMovie.SeedConfiguration, Settings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warn(ex, "Failed to set the torrent seed criteria for {0}.", hash);
|
||||
}
|
||||
}
|
||||
|
||||
if (moveToTop)
|
||||
@ -137,9 +146,16 @@ protected override string AddFromTorrentFile(RemoteMovie remoteMovie, string has
|
||||
}
|
||||
|
||||
if (!addHasSetShareLimits && setShareLimits)
|
||||
{
|
||||
try
|
||||
{
|
||||
Proxy.SetTorrentSeedingConfiguration(hash.ToLower(), remoteMovie.SeedConfiguration, Settings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warn(ex, "Failed to set the torrent seed criteria for {0}.", hash);
|
||||
}
|
||||
}
|
||||
|
||||
if (moveToTop)
|
||||
{
|
||||
@ -171,15 +187,17 @@ protected override string AddFromTorrentFile(RemoteMovie remoteMovie, string has
|
||||
|
||||
protected bool WaitForTorrent(string hash)
|
||||
{
|
||||
var count = 5;
|
||||
var count = 10;
|
||||
|
||||
while (count != 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
Proxy.GetTorrentProperties(hash.ToLower(), Settings);
|
||||
if (Proxy.IsTorrentLoaded(hash.ToLower(), Settings))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ public interface IQBittorrentProxy
|
||||
string GetVersion(QBittorrentSettings settings);
|
||||
QBittorrentPreferences GetConfig(QBittorrentSettings settings);
|
||||
List<QBittorrentTorrent> GetTorrents(QBittorrentSettings settings);
|
||||
bool IsTorrentLoaded(string hash, QBittorrentSettings settings);
|
||||
QBittorrentTorrentProperties GetTorrentProperties(string hash, QBittorrentSettings settings);
|
||||
List<QBittorrentTorrentFile> GetTorrentFiles(string hash, QBittorrentSettings settings);
|
||||
|
||||
@ -40,7 +41,6 @@ public interface IQBittorrentProxySelector
|
||||
|
||||
public class QBittorrentProxySelector : IQBittorrentProxySelector
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly ICached<Tuple<IQBittorrentProxy, Version>> _proxyCache;
|
||||
private readonly Logger _logger;
|
||||
|
||||
@ -53,7 +53,6 @@ public QBittorrentProxySelector(QBittorrentProxyV1 proxyV1,
|
||||
ICacheManager cacheManager,
|
||||
Logger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_proxyCache = cacheManager.GetCache<Tuple<IQBittorrentProxy, Version>>(GetType());
|
||||
_logger = logger;
|
||||
|
||||
|
@ -97,6 +97,23 @@ public List<QBittorrentTorrent> GetTorrents(QBittorrentSettings settings)
|
||||
return response;
|
||||
}
|
||||
|
||||
public bool IsTorrentLoaded(string hash, QBittorrentSettings settings)
|
||||
{
|
||||
var request = BuildRequest(settings).Resource($"/query/propertiesGeneral/{hash}");
|
||||
request.LogHttpError = false;
|
||||
|
||||
try
|
||||
{
|
||||
ProcessRequest(request, settings);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public QBittorrentTorrentProperties GetTorrentProperties(string hash, QBittorrentSettings settings)
|
||||
{
|
||||
var request = BuildRequest(settings).Resource($"/query/propertiesGeneral/{hash}");
|
||||
@ -295,15 +312,14 @@ private string ProcessRequest(HttpRequestBuilder requestBuilder, QBittorrentSett
|
||||
|
||||
var request = requestBuilder.Build();
|
||||
request.LogResponseContent = true;
|
||||
request.SuppressHttpErrorStatusCodes = new[] { HttpStatusCode.Forbidden };
|
||||
|
||||
HttpResponse response;
|
||||
try
|
||||
{
|
||||
response = _httpClient.Execute(request);
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Forbidden)
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
_logger.Debug("Authentication required, logging in.");
|
||||
|
||||
@ -313,11 +329,11 @@ private string ProcessRequest(HttpRequestBuilder requestBuilder, QBittorrentSett
|
||||
|
||||
response = _httpClient.Execute(request);
|
||||
}
|
||||
else
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
throw new DownloadClientException("Failed to connect to qBittorrent, check your settings.", ex);
|
||||
}
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
throw new DownloadClientException("Failed to connect to qBittorrent, please check your settings.", ex);
|
||||
|
@ -101,6 +101,24 @@ public List<QBittorrentTorrent> GetTorrents(QBittorrentSettings settings)
|
||||
return response;
|
||||
}
|
||||
|
||||
public bool IsTorrentLoaded(string hash, QBittorrentSettings settings)
|
||||
{
|
||||
var request = BuildRequest(settings).Resource("/api/v2/torrents/properties")
|
||||
.AddQueryParam("hash", hash);
|
||||
request.LogHttpError = false;
|
||||
|
||||
try
|
||||
{
|
||||
ProcessRequest(request, settings);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public QBittorrentTorrentProperties GetTorrentProperties(string hash, QBittorrentSettings settings)
|
||||
{
|
||||
var request = BuildRequest(settings).Resource("/api/v2/torrents/properties")
|
||||
@ -141,7 +159,7 @@ public void AddTorrentFromUrl(string torrentUrl, TorrentSeedConfiguration seedCo
|
||||
|
||||
if (seedConfiguration != null)
|
||||
{
|
||||
AddTorrentSeedingFormParameters(request, seedConfiguration, settings);
|
||||
AddTorrentSeedingFormParameters(request, seedConfiguration);
|
||||
}
|
||||
|
||||
var result = ProcessRequest(request, settings);
|
||||
@ -176,7 +194,7 @@ public void AddTorrentFromFile(string fileName, byte[] fileContent, TorrentSeedC
|
||||
|
||||
if (seedConfiguration != null)
|
||||
{
|
||||
AddTorrentSeedingFormParameters(request, seedConfiguration, settings);
|
||||
AddTorrentSeedingFormParameters(request, seedConfiguration);
|
||||
}
|
||||
|
||||
var result = ProcessRequest(request, settings);
|
||||
@ -225,17 +243,17 @@ public Dictionary<string, QBittorrentLabel> GetLabels(QBittorrentSettings settin
|
||||
return Json.Deserialize<Dictionary<string, QBittorrentLabel>>(ProcessRequest(request, settings));
|
||||
}
|
||||
|
||||
private void AddTorrentSeedingFormParameters(HttpRequestBuilder request, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
|
||||
private void AddTorrentSeedingFormParameters(HttpRequestBuilder request, TorrentSeedConfiguration seedConfiguration, bool always = false)
|
||||
{
|
||||
var ratioLimit = seedConfiguration.Ratio.HasValue ? seedConfiguration.Ratio : -2;
|
||||
var seedingTimeLimit = seedConfiguration.SeedTime.HasValue ? (long)seedConfiguration.SeedTime.Value.TotalMinutes : -2;
|
||||
|
||||
if (ratioLimit != -2)
|
||||
if (ratioLimit != -2 || always)
|
||||
{
|
||||
request.AddFormParameter("ratioLimit", ratioLimit);
|
||||
}
|
||||
|
||||
if (seedingTimeLimit != -2)
|
||||
if (seedingTimeLimit != -2 || always)
|
||||
{
|
||||
request.AddFormParameter("seedingTimeLimit", seedingTimeLimit);
|
||||
}
|
||||
@ -247,7 +265,7 @@ public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration
|
||||
.Post()
|
||||
.AddFormParameter("hashes", hash);
|
||||
|
||||
AddTorrentSeedingFormParameters(request, seedConfiguration, settings);
|
||||
AddTorrentSeedingFormParameters(request, seedConfiguration, true);
|
||||
|
||||
try
|
||||
{
|
||||
@ -336,15 +354,14 @@ private string ProcessRequest(HttpRequestBuilder requestBuilder, QBittorrentSett
|
||||
|
||||
var request = requestBuilder.Build();
|
||||
request.LogResponseContent = true;
|
||||
request.SuppressHttpErrorStatusCodes = new[] { HttpStatusCode.Forbidden };
|
||||
|
||||
HttpResponse response;
|
||||
try
|
||||
{
|
||||
response = _httpClient.Execute(request);
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
if (ex.Response.StatusCode == HttpStatusCode.Forbidden)
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Forbidden)
|
||||
{
|
||||
_logger.Debug("Authentication required, logging in.");
|
||||
|
||||
@ -354,11 +371,11 @@ private string ProcessRequest(HttpRequestBuilder requestBuilder, QBittorrentSett
|
||||
|
||||
response = _httpClient.Execute(request);
|
||||
}
|
||||
else
|
||||
}
|
||||
catch (HttpException ex)
|
||||
{
|
||||
throw new DownloadClientException("Failed to connect to qBittorrent, check your settings.", ex);
|
||||
}
|
||||
}
|
||||
catch (WebException ex)
|
||||
{
|
||||
throw new DownloadClientException("Failed to connect to qBittorrent, please check your settings.", ex);
|
||||
|
Loading…
Reference in New Issue
Block a user