Skip to content

Commit

Permalink
New: Add minimum version checks for applications
Browse files Browse the repository at this point in the history
  • Loading branch information
mynameisbogdan committed May 26, 2023
1 parent b3bc92e commit dcb19a6
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 15 deletions.
7 changes: 3 additions & 4 deletions src/NzbDrone.Core/Applications/Lidarr/Lidarr.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
using Newtonsoft.Json.Linq;
using NLog;
Expand Down Expand Up @@ -49,10 +48,10 @@ public override ValidationResult Test()
{
failures.AddIfNotNull(_lidarrV1Proxy.TestConnection(BuildLidarrIndexer(testIndexer, DownloadProtocol.Usenet), Settings));
}
catch (WebException ex)
catch (Exception ex)
{
_logger.Error(ex, "Unable to send test message");
failures.AddIfNotNull(new ValidationFailure("BaseUrl", "Unable to complete application test, cannot connect to Lidarr"));
failures.AddIfNotNull(new ValidationFailure("BaseUrl", $"Unable to complete application test, cannot connect to Lidarr. {ex.Message}"));
}

return new ValidationResult(failures);
Expand All @@ -61,7 +60,7 @@ public override ValidationResult Test()
public override List<AppIndexerMap> GetIndexerMappings()
{
var indexers = _lidarrV1Proxy.GetIndexers(Settings)
.Where(i => i.Implementation == "Newznab" || i.Implementation == "Torznab");
.Where(i => i.Implementation is "Newznab" or "Torznab");

var mappings = new List<AppIndexerMap>();

Expand Down
15 changes: 14 additions & 1 deletion src/NzbDrone.Core/Applications/Lidarr/LidarrV1Proxy.cs
Expand Up @@ -23,8 +23,11 @@ public interface ILidarrV1Proxy

public class LidarrV1Proxy : ILidarrV1Proxy
{
private static Version MinimumApplicationVersion => new (1, 0, 2, 0);

private const string AppApiRoute = "/api/v1";
private const string AppIndexerApiRoute = $"{AppApiRoute}/indexer";

private readonly IHttpClient _httpClient;
private readonly Logger _logger;

Expand Down Expand Up @@ -102,7 +105,17 @@ public ValidationFailure TestConnection(LidarrIndexer indexer, LidarrSettings se

try
{
Execute<LidarrIndexer>(request);
var applicationVersion = _httpClient.Post<LidarrIndexer>(request).Headers.GetSingleValue("X-Application-Version");

if (applicationVersion == null)
{
return new ValidationFailure(string.Empty, "Failed to fetch Lidarr version");
}

if (new Version(applicationVersion) < MinimumApplicationVersion)
{
return new ValidationFailure(string.Empty, $"Lidarr version should be at least {MinimumApplicationVersion.ToString(3)}. Version reported is {applicationVersion}", applicationVersion);
}
}
catch (HttpException ex)
{
Expand Down
7 changes: 3 additions & 4 deletions src/NzbDrone.Core/Applications/Radarr/Radarr.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
using Newtonsoft.Json.Linq;
using NLog;
Expand Down Expand Up @@ -49,10 +48,10 @@ public override ValidationResult Test()
{
failures.AddIfNotNull(_radarrV3Proxy.TestConnection(BuildRadarrIndexer(testIndexer, DownloadProtocol.Usenet), Settings));
}
catch (WebException ex)
catch (Exception ex)
{
_logger.Error(ex, "Unable to send test message");
failures.AddIfNotNull(new ValidationFailure("BaseUrl", "Unable to complete application test, cannot connect to Radarr"));
failures.AddIfNotNull(new ValidationFailure("BaseUrl", $"Unable to complete application test, cannot connect to Radarr. {ex.Message}"));
}

return new ValidationResult(failures);
Expand All @@ -61,7 +60,7 @@ public override ValidationResult Test()
public override List<AppIndexerMap> GetIndexerMappings()
{
var indexers = _radarrV3Proxy.GetIndexers(Settings)
.Where(i => i.Implementation == "Newznab" || i.Implementation == "Torznab");
.Where(i => i.Implementation is "Newznab" or "Torznab");

var mappings = new List<AppIndexerMap>();

Expand Down
28 changes: 27 additions & 1 deletion src/NzbDrone.Core/Applications/Radarr/RadarrV3Proxy.cs
Expand Up @@ -23,8 +23,12 @@ public interface IRadarrV3Proxy

public class RadarrV3Proxy : IRadarrV3Proxy
{
private static Version MinimumApplicationV4Version => new (4, 0, 4, 0);
private static Version MinimumApplicationV3Version => new (3, 1, 1, 0);

private const string AppApiRoute = "/api/v3";
private const string AppIndexerApiRoute = $"{AppApiRoute}/indexer";

private readonly IHttpClient _httpClient;
private readonly Logger _logger;

Expand Down Expand Up @@ -102,7 +106,29 @@ public ValidationFailure TestConnection(RadarrIndexer indexer, RadarrSettings se

try
{
Execute<RadarrIndexer>(request);
var applicationVersion = _httpClient.Post<RadarrIndexer>(request).Headers.GetSingleValue("X-Application-Version");

if (applicationVersion == null)
{
return new ValidationFailure(string.Empty, "Failed to fetch Radarr version");
}

var version = new Version(applicationVersion);

if (version.Major == 3)
{
if (version < MinimumApplicationV3Version)
{
return new ValidationFailure(string.Empty, $"Radarr version should be at least {MinimumApplicationV3Version.ToString(3)}. Version reported is {applicationVersion}", applicationVersion);
}
}
else
{
if (version < MinimumApplicationV4Version)
{
return new ValidationFailure(string.Empty, $"Radarr version should be at least {MinimumApplicationV4Version.ToString(3)}. Version reported is {applicationVersion}", applicationVersion);
}
}
}
catch (HttpException ex)
{
Expand Down
7 changes: 3 additions & 4 deletions src/NzbDrone.Core/Applications/Sonarr/Sonarr.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
using Newtonsoft.Json.Linq;
using NLog;
Expand Down Expand Up @@ -49,10 +48,10 @@ public override ValidationResult Test()
{
failures.AddIfNotNull(_sonarrV3Proxy.TestConnection(BuildSonarrIndexer(testIndexer, DownloadProtocol.Usenet), Settings));
}
catch (WebException ex)
catch (Exception ex)
{
_logger.Error(ex, "Unable to send test message");
failures.AddIfNotNull(new ValidationFailure("BaseUrl", "Unable to complete application test, cannot connect to Sonarr"));
failures.AddIfNotNull(new ValidationFailure("BaseUrl", $"Unable to complete application test, cannot connect to Sonarr. {ex.Message}"));
}

return new ValidationResult(failures);
Expand All @@ -61,7 +60,7 @@ public override ValidationResult Test()
public override List<AppIndexerMap> GetIndexerMappings()
{
var indexers = _sonarrV3Proxy.GetIndexers(Settings)
.Where(i => i.Implementation == "Newznab" || i.Implementation == "Torznab");
.Where(i => i.Implementation is "Newznab" or "Torznab");

var mappings = new List<AppIndexerMap>();

Expand Down
15 changes: 14 additions & 1 deletion src/NzbDrone.Core/Applications/Sonarr/SonarrV3Proxy.cs
Expand Up @@ -23,8 +23,11 @@ public interface ISonarrV3Proxy

public class SonarrV3Proxy : ISonarrV3Proxy
{
private static Version MinimumApplicationVersion => new (3, 0, 5, 0);

private const string AppApiRoute = "/api/v3";
private const string AppIndexerApiRoute = $"{AppApiRoute}/indexer";

private readonly IHttpClient _httpClient;
private readonly Logger _logger;

Expand Down Expand Up @@ -102,7 +105,17 @@ public ValidationFailure TestConnection(SonarrIndexer indexer, SonarrSettings se

try
{
Execute<SonarrIndexer>(request);
var applicationVersion = _httpClient.Post<SonarrIndexer>(request).Headers.GetSingleValue("X-Application-Version");

if (applicationVersion == null)
{
return new ValidationFailure(string.Empty, "Failed to fetch Sonarr version");
}

if (new Version(applicationVersion) < MinimumApplicationVersion)
{
return new ValidationFailure(string.Empty, $"Sonarr version should be at least {MinimumApplicationVersion.ToString(3)}. Version reported is {applicationVersion}", applicationVersion);
}
}
catch (HttpException ex)
{
Expand Down

0 comments on commit dcb19a6

Please sign in to comment.