From 9ed25538837962ea03c2004901f5d3fc575bd870 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 3 Jun 2023 21:20:31 +0300 Subject: [PATCH] Fixed: (RadarrImport) Treat redirects as HTTP errors --- .../ImportLists/Radarr/RadarrImport.cs | 52 +++++++++++-------- .../ImportLists/Radarr/RadarrV3Proxy.cs | 17 +++++- 2 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/NzbDrone.Core/ImportLists/Radarr/RadarrImport.cs b/src/NzbDrone.Core/ImportLists/Radarr/RadarrImport.cs index 637474ac9..59d7d9fff 100644 --- a/src/NzbDrone.Core/ImportLists/Radarr/RadarrImport.cs +++ b/src/NzbDrone.Core/ImportLists/Radarr/RadarrImport.cs @@ -80,53 +80,63 @@ namespace NzbDrone.Core.ImportLists.Radarr // Return early if there is not an API key if (Settings.ApiKey.IsNullOrWhiteSpace()) { - return new - { - devices = new List() - }; + return new { options = new List() }; } Settings.Validate().Filter("ApiKey").ThrowOnError(); if (action == "getProfiles") { - var devices = _radarrV3Proxy.GetProfiles(Settings); + var profiles = _radarrV3Proxy.GetProfiles(Settings); + + if (profiles == null) + { + return new { options = new List() }; + } return new { - options = devices.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase) - .Select(d => new - { - Value = d.Id, - Name = d.Name - }) + options = profiles.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase) + .Select(d => new + { + Value = d.Id, + Name = d.Name + }) }; } if (action == "getTags") { - var devices = _radarrV3Proxy.GetTags(Settings); + var tags = _radarrV3Proxy.GetTags(Settings); + + if (tags == null) + { + return new { options = new List() }; + } return new { - options = devices.OrderBy(d => d.Label, StringComparer.InvariantCultureIgnoreCase) - .Select(d => new - { - Value = d.Id, - Name = d.Label - }) + options = tags.OrderBy(d => d.Label, StringComparer.InvariantCultureIgnoreCase) + .Select(d => new + { + Value = d.Id, + Name = d.Label + }) }; } if (action == "getRootFolders") { - Settings.Validate().Filter("ApiKey").ThrowOnError(); + var remoteRootFolders = _radarrV3Proxy.GetRootFolders(Settings); - var remoteRootfolders = _radarrV3Proxy.GetRootFolders(Settings); + if (remoteRootFolders == null) + { + return new { options = new List() }; + } return new { - options = remoteRootfolders.OrderBy(d => d.Path, StringComparer.InvariantCultureIgnoreCase) + options = remoteRootFolders.OrderBy(d => d.Path, StringComparer.InvariantCultureIgnoreCase) .Select(d => new { Value = d.Path, diff --git a/src/NzbDrone.Core/ImportLists/Radarr/RadarrV3Proxy.cs b/src/NzbDrone.Core/ImportLists/Radarr/RadarrV3Proxy.cs index 888297a31..b6c03b5ed 100644 --- a/src/NzbDrone.Core/ImportLists/Radarr/RadarrV3Proxy.cs +++ b/src/NzbDrone.Core/ImportLists/Radarr/RadarrV3Proxy.cs @@ -63,6 +63,12 @@ namespace NzbDrone.Core.ImportLists.Radarr return new ValidationFailure("ApiKey", "API Key is invalid"); } + if (ex.Response.HasHttpRedirect) + { + _logger.Error(ex, "Radarr returned redirect and is invalid"); + return new ValidationFailure("BaseUrl", "Radarr URL is invalid, are you missing a URL base?"); + } + _logger.Error(ex, "Unable to connect to import list."); return new ValidationFailure(string.Empty, $"Unable to connect to import list: {ex.Message}. Check the log surrounding this error for details."); } @@ -84,11 +90,18 @@ namespace NzbDrone.Core.ImportLists.Radarr var baseUrl = settings.BaseUrl.TrimEnd('/'); - var request = new HttpRequestBuilder(baseUrl).Resource(resource).Accept(HttpAccept.Json) - .SetHeader("X-Api-Key", settings.ApiKey).Build(); + var request = new HttpRequestBuilder(baseUrl).Resource(resource) + .Accept(HttpAccept.Json) + .SetHeader("X-Api-Key", settings.ApiKey) + .Build(); var response = _httpClient.Get(request); + if ((int)response.StatusCode > 299) + { + throw new HttpException(response); + } + var results = JsonConvert.DeserializeObject>(response.Content); return results;