From 72ae12441864a7feb6cdbe7ee1c0fc72fdd2eb1e Mon Sep 17 00:00:00 2001 From: Glax Date: Wed, 28 Feb 2024 02:25:17 +0100 Subject: [PATCH] Handle unauthorized kavita --- Tranga/LibraryConnectors/Kavita.cs | 25 +++++++++++++------- Tranga/LibraryConnectors/LibraryConnector.cs | 2 ++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Tranga/LibraryConnectors/Kavita.cs b/Tranga/LibraryConnectors/Kavita.cs index 106c90e..3fb8983 100644 --- a/Tranga/LibraryConnectors/Kavita.cs +++ b/Tranga/LibraryConnectors/Kavita.cs @@ -1,4 +1,5 @@ using System.Text.Json.Nodes; +using Logging; using Newtonsoft.Json; using JsonSerializer = System.Text.Json.JsonSerializer; @@ -8,7 +9,7 @@ public class Kavita : LibraryConnector { public Kavita(GlobalBase clone, string baseUrl, string username, string password) : - base(clone, baseUrl, GetToken(baseUrl, username, password), LibraryType.Kavita) + base(clone, baseUrl, GetToken(baseUrl, username, password, clone.logger), LibraryType.Kavita) { } @@ -22,7 +23,7 @@ public class Kavita : LibraryConnector return $"Kavita {baseUrl}"; } - private static string GetToken(string baseUrl, string username, string password) + private static string GetToken(string baseUrl, string username, string password, Logger? logger = null) { HttpClient client = new() { @@ -40,16 +41,24 @@ public class Kavita : LibraryConnector try { HttpResponseMessage response = client.Send(requestMessage); - JsonObject? result = JsonSerializer.Deserialize(response.Content.ReadAsStream()); - if (result is not null) - return result["token"]!.GetValue(); + logger?.WriteLine($"Kavita | GetToken {requestMessage.RequestUri} -> {response.StatusCode}"); + if (response.IsSuccessStatusCode) + { + JsonObject? result = JsonSerializer.Deserialize(response.Content.ReadAsStream()); + if (result is not null) + return result["token"]!.GetValue(); + } + else + { + logger?.WriteLine($"Kavita | {response.Content}"); + } } catch (HttpRequestException e) { - Console.WriteLine($"Unable to retrieve token:\n\r{e}"); + logger?.WriteLine($"Kavita | Unable to retrieve token:\n\r{e}"); } - Console.WriteLine("Kavita: Did not receive token."); - throw new Exception("Kavita: Did not receive token."); + logger?.WriteLine("Kavita | Did not receive token."); + return ""; } public override void UpdateLibrary() diff --git a/Tranga/LibraryConnectors/LibraryConnector.cs b/Tranga/LibraryConnectors/LibraryConnector.cs index 5bd7097..0a66890 100644 --- a/Tranga/LibraryConnectors/LibraryConnector.cs +++ b/Tranga/LibraryConnectors/LibraryConnector.cs @@ -23,6 +23,8 @@ public abstract class LibraryConnector : GlobalBase Log($"Creating libraryConnector {Enum.GetName(libraryType)}"); if (!baseUrlRex.IsMatch(baseUrl)) throw new ArgumentException("Base url does not match pattern"); + if(auth == "") + throw new ArgumentNullException(nameof(auth), "Auth can not be empty"); this.baseUrl = baseUrlRex.Match(baseUrl).Value; this.auth = auth; this.libraryType = libraryType;