From 370e3ca06bb9567f789968e839233b05691b11fb Mon Sep 17 00:00:00 2001 From: pcjones Date: Wed, 4 Sep 2024 19:39:15 +0200 Subject: [PATCH] Fix warnings --- .../Controllers/SearchController.cs | 2 +- UmlautAdaptarr/Models/IpInfo.cs | 18 ++++++------- UmlautAdaptarr/Models/SearchItem.cs | 2 +- UmlautAdaptarr/Providers/ArrClientBase.cs | 2 ++ UmlautAdaptarr/Services/HttpProxyService.cs | 10 +++---- .../Services/TitleMatchingService.cs | 10 +++---- UmlautAdaptarr/Utilities/Helper.cs | 26 +++++++++---------- .../Utilities/ServicesExtensions.cs | 18 +++++-------- 8 files changed, 41 insertions(+), 47 deletions(-) diff --git a/UmlautAdaptarr/Controllers/SearchController.cs b/UmlautAdaptarr/Controllers/SearchController.cs index 90259d5..63f2f03 100644 --- a/UmlautAdaptarr/Controllers/SearchController.cs +++ b/UmlautAdaptarr/Controllers/SearchController.cs @@ -23,7 +23,7 @@ namespace UmlautAdaptarr.Controllers return NotFound($"{domain} is not a valid URL."); } - var initialSearchResult = await PerformSingleSearchRequest(domain, queryParameters) as ContentResult; + ContentResult? initialSearchResult = await PerformSingleSearchRequest(domain, queryParameters) as ContentResult; if (initialSearchResult == null) { return null; diff --git a/UmlautAdaptarr/Models/IpInfo.cs b/UmlautAdaptarr/Models/IpInfo.cs index 10142c0..6abaf27 100644 --- a/UmlautAdaptarr/Models/IpInfo.cs +++ b/UmlautAdaptarr/Models/IpInfo.cs @@ -5,29 +5,29 @@ namespace UmlautAdaptarr.Utilities; public class IpInfo { [JsonPropertyName("ip")] - public string Ip { get; set; } + public string? Ip { get; set; } [JsonPropertyName("hostname")] - public string Hostname { get; set; } + public string? Hostname { get; set; } [JsonPropertyName("city")] - public string City { get; set; } + public string? City { get; set; } [JsonPropertyName("region")] - public string Region { get; set; } + public string? Region { get; set; } [JsonPropertyName("country")] - public string Country { get; set; } + public string? Country { get; set; } [JsonPropertyName("loc")] - public string Loc { get; set; } + public string? Loc { get; set; } [JsonPropertyName("org")] - public string Org { get; set; } + public string? Org { get; set; } [JsonPropertyName("postal")] - public string Postal { get; set; } + public string? Postal { get; set; } [JsonPropertyName("timezone")] - public string Timezone { get; set; } + public string? Timezone { get; set; } } \ No newline at end of file diff --git a/UmlautAdaptarr/Models/SearchItem.cs b/UmlautAdaptarr/Models/SearchItem.cs index 4e3239b..83e2d55 100644 --- a/UmlautAdaptarr/Models/SearchItem.cs +++ b/UmlautAdaptarr/Models/SearchItem.cs @@ -166,7 +166,7 @@ namespace UmlautAdaptarr.Models } } - private IEnumerable GenerateVariations(string? title, string mediaType) + private static IEnumerable GenerateVariations(string? title, string mediaType) { if (title == null) { diff --git a/UmlautAdaptarr/Providers/ArrClientBase.cs b/UmlautAdaptarr/Providers/ArrClientBase.cs index e192fae..8d2b9b8 100644 --- a/UmlautAdaptarr/Providers/ArrClientBase.cs +++ b/UmlautAdaptarr/Providers/ArrClientBase.cs @@ -5,7 +5,9 @@ namespace UmlautAdaptarr.Providers; public abstract class ArrClientBase : IArrApplication { +#pragma warning disable CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. public string InstanceName; +#pragma warning restore CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. public abstract Task> FetchAllItemsAsync(); public abstract Task FetchItemByExternalIdAsync(string externalId); public abstract Task FetchItemByTitleAsync(string title); diff --git a/UmlautAdaptarr/Services/HttpProxyService.cs b/UmlautAdaptarr/Services/HttpProxyService.cs index 0444883..b50064b 100644 --- a/UmlautAdaptarr/Services/HttpProxyService.cs +++ b/UmlautAdaptarr/Services/HttpProxyService.cs @@ -11,8 +11,8 @@ namespace UmlautAdaptarr.Services private readonly int _proxyPort = 5006; // TODO move to appsettings.json private readonly IHttpClientFactory _clientFactory; private readonly HashSet _knownHosts = []; - private readonly object _hostsLock = new object(); - + private readonly object _hostsLock = new(); + private static readonly string[] newLineSeparator = ["\r\n"]; public HttpProxyService(ILogger logger, IHttpClientFactory clientFactory) { @@ -123,7 +123,7 @@ namespace UmlautAdaptarr.Services { var headers = new Dictionary(); var headerString = Encoding.ASCII.GetString(buffer, 0, length); - var lines = headerString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); + var lines = headerString.Split(newLineSeparator, StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines.Skip(1)) // Skip the request line { var colonIndex = line.IndexOf(':'); @@ -137,7 +137,7 @@ namespace UmlautAdaptarr.Services return headers; } - private (string host, int port) ParseTargetInfo(string requestLine) + private static (string host, int port) ParseTargetInfo(string requestLine) { var parts = requestLine.Split(' ')[1].Split(':'); return (parts[0], int.Parse(parts[1])); @@ -150,7 +150,7 @@ namespace UmlautAdaptarr.Services await Task.WhenAll(clientToTargetTask, targetToClientTask); } - private async Task RelayStream(NetworkStream input, NetworkStream output) + private static async Task RelayStream(NetworkStream input, NetworkStream output) { byte[] buffer = new byte[8192]; int bytesRead; diff --git a/UmlautAdaptarr/Services/TitleMatchingService.cs b/UmlautAdaptarr/Services/TitleMatchingService.cs index 60f4d21..4d1101e 100644 --- a/UmlautAdaptarr/Services/TitleMatchingService.cs +++ b/UmlautAdaptarr/Services/TitleMatchingService.cs @@ -69,11 +69,11 @@ namespace UmlautAdaptarr.Services public void FindAndReplaceForBooksAndAudio(SearchItem searchItem, XElement? titleElement, string originalTitle) { var authorMatch = FindBestMatch(searchItem.AuthorMatchVariations, originalTitle.NormalizeForComparison(), originalTitle); - var titleMatch = FindBestMatch(searchItem.TitleMatchVariations, originalTitle.NormalizeForComparison(), originalTitle); + var (foundMatch, bestStart, bestEndInOriginal) = FindBestMatch(searchItem.TitleMatchVariations, originalTitle.NormalizeForComparison(), originalTitle); - if (authorMatch.foundMatch && titleMatch.foundMatch) + if (authorMatch.foundMatch && foundMatch) { - int matchEndPositionInOriginal = Math.Max(authorMatch.bestEndInOriginal, titleMatch.bestEndInOriginal); + int matchEndPositionInOriginal = Math.Max(authorMatch.bestEndInOriginal, bestEndInOriginal); // Check and adjust for immediate following delimiter char[] delimiters = [' ', '-', '_', '.']; @@ -103,7 +103,7 @@ namespace UmlautAdaptarr.Services } - private (bool foundMatch, int bestStart, int bestEndInOriginal) FindBestMatch(string[] variations, string normalizedOriginal, string originalTitle) + private static (bool foundMatch, int bestStart, int bestEndInOriginal) FindBestMatch(string[] variations, string normalizedOriginal, string originalTitle) { bool found = false; int bestStart = int.MaxValue; @@ -131,7 +131,7 @@ namespace UmlautAdaptarr.Services } // Maps an index from the normalized string back to a corresponding index in the original string - private int MapNormalizedIndexToOriginal(string normalizedOriginal, string originalTitle, int normalizedIndex) + private static int MapNormalizedIndexToOriginal(string normalizedOriginal, string originalTitle, int normalizedIndex) { // Count non-special characters up to the given index in the normalized string int nonSpecialCharCount = 0; diff --git a/UmlautAdaptarr/Utilities/Helper.cs b/UmlautAdaptarr/Utilities/Helper.cs index ebd867a..002310e 100644 --- a/UmlautAdaptarr/Utilities/Helper.cs +++ b/UmlautAdaptarr/Utilities/Helper.cs @@ -38,21 +38,19 @@ public static class Helper private static async Task GetPublicIpAddressInfoAsync() { - using (var client = new HttpClient()) - { - client.Timeout = TimeSpan.FromSeconds(10); + using var client = new HttpClient(); + client.Timeout = TimeSpan.FromSeconds(10); - try - { - var response = await client.GetAsync("https://ipinfo.io/json"); - response.EnsureSuccessStatusCode(); - var content = await response.Content.ReadAsStringAsync(); - return JsonSerializer.Deserialize(content); - } - catch - { - return null; - } + try + { + var response = await client.GetAsync("https://ipinfo.io/json"); + response.EnsureSuccessStatusCode(); + var content = await response.Content.ReadAsStringAsync(); + return JsonSerializer.Deserialize(content); + } + catch + { + return null; } } } diff --git a/UmlautAdaptarr/Utilities/ServicesExtensions.cs b/UmlautAdaptarr/Utilities/ServicesExtensions.cs index 3e46e4e..7037172 100644 --- a/UmlautAdaptarr/Utilities/ServicesExtensions.cs +++ b/UmlautAdaptarr/Utilities/ServicesExtensions.cs @@ -35,17 +35,15 @@ public static class ServicesExtensions where TService : class, TInterface where TInterface : class { - try { if (builder.Services == null) throw new ArgumentNullException(nameof(builder), "Service collection is null."); - var singleInstance = builder.Configuration.GetSection(sectionName).Get(); var singleHost = (string?)typeof(TOptions).GetProperty("Host")?.GetValue(singleInstance, null); - // If we have no Single Instance , we try to parse for a Array + // If we have no Single Instance, we try to parse for an Array var optionsArray = singleHost == null ? builder.Configuration.GetSection(sectionName).Get() : @@ -59,8 +57,7 @@ public static class ServicesExtensions foreach (var option in optionsArray) { - - GlobalInstanceOptionsValidator validator = new GlobalInstanceOptionsValidator(); + GlobalInstanceOptionsValidator validator = new GlobalInstanceOptionsValidator(); var results = validator.Validate(option as GlobalInstanceOptions); @@ -100,7 +97,7 @@ public static class ServicesExtensions } else { - Logger.LogWarning((prop.PropertyType + "No Support")); + Logger.LogWarning(prop.PropertyType + "No Support"); } } @@ -110,9 +107,9 @@ public static class ServicesExtensions return builder; } - catch (Exception e) + catch (Exception ex) { - Console.WriteLine("Error while Init UmlautAdaptrr"); + Console.WriteLine($"Error in AddServicesWithOptions: {ex.Message}"); throw; } @@ -133,11 +130,8 @@ public static class ServicesExtensions { if (builder.Services == null) throw new ArgumentNullException(nameof(builder), "Service collection is null."); - var options = builder.Configuration.GetSection(sectionName).Get(); - if (options == null) - throw new InvalidOperationException( + var options = builder.Configuration.GetSection(sectionName).Get() ?? throw new InvalidOperationException( $"{typeof(TService).Name} options could not be loaded from Configuration or ENV Variable."); - builder.Services.Configure(builder.Configuration.GetSection(sectionName)); builder.Services.AddSingleton();