2
0

Handle unauthorized kavita

This commit is contained in:
Glax 2024-02-28 02:25:17 +01:00
parent bee6e7ba37
commit 72ae124418
2 changed files with 19 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer; using JsonSerializer = System.Text.Json.JsonSerializer;
@ -8,7 +9,7 @@ public class Kavita : LibraryConnector
{ {
public Kavita(GlobalBase clone, string baseUrl, string username, string password) : 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}"; 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() HttpClient client = new()
{ {
@ -40,16 +41,24 @@ public class Kavita : LibraryConnector
try try
{ {
HttpResponseMessage response = client.Send(requestMessage); HttpResponseMessage response = client.Send(requestMessage);
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(response.Content.ReadAsStream()); logger?.WriteLine($"Kavita | GetToken {requestMessage.RequestUri} -> {response.StatusCode}");
if (result is not null) if (response.IsSuccessStatusCode)
return result["token"]!.GetValue<string>(); {
JsonObject? result = JsonSerializer.Deserialize<JsonObject>(response.Content.ReadAsStream());
if (result is not null)
return result["token"]!.GetValue<string>();
}
else
{
logger?.WriteLine($"Kavita | {response.Content}");
}
} }
catch (HttpRequestException e) 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."); logger?.WriteLine("Kavita | Did not receive token.");
throw new Exception("Kavita: Did not receive token."); return "";
} }
public override void UpdateLibrary() public override void UpdateLibrary()

View File

@ -23,6 +23,8 @@ public abstract class LibraryConnector : GlobalBase
Log($"Creating libraryConnector {Enum.GetName(libraryType)}"); Log($"Creating libraryConnector {Enum.GetName(libraryType)}");
if (!baseUrlRex.IsMatch(baseUrl)) if (!baseUrlRex.IsMatch(baseUrl))
throw new ArgumentException("Base url does not match pattern"); 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.baseUrl = baseUrlRex.Match(baseUrl).Value;
this.auth = auth; this.auth = auth;
this.libraryType = libraryType; this.libraryType = libraryType;