using System.Text.Json; using System.Text.Json.Nodes; using Logging; namespace Tranga.LibraryManagers; public class Kavita : LibraryManager { public Kavita(string baseUrl, string apiKey, Logger? logger) : base(baseUrl, apiKey, logger) { this.libraryType = LibraryType.Kavita; } public override void UpdateLibrary() { logger?.WriteLine(this.GetType().ToString(), $"Updating Libraries"); foreach (KavitaLibrary lib in GetLibraries()) NetClient.MakePost($"{baseUrl}/api/Library/scan?libraryId={lib.id}", auth, logger); } /// /// Fetches all libraries available to the user /// /// Array of KavitaLibrary private IEnumerable GetLibraries() { logger?.WriteLine(this.GetType().ToString(), $"Getting Libraries"); Stream data = NetClient.MakeRequest($"{baseUrl}/api/Library", auth, logger); if (data == Stream.Null) { logger?.WriteLine(this.GetType().ToString(), $"No libraries returned"); return Array.Empty(); } JsonArray? result = JsonSerializer.Deserialize(data); if (result is null) { logger?.WriteLine(this.GetType().ToString(), $"No libraries returned"); return Array.Empty(); } HashSet ret = new(); foreach (JsonNode? jsonNode in result) { var jObject = (JsonObject?)jsonNode; string libraryId = jObject!["id"]!.GetValue(); string libraryName = jObject!["name"]!.GetValue(); ret.Add(new KavitaLibrary(libraryId, libraryName)); } return ret; } private struct KavitaLibrary { public string id { get; } public string name { get; } public KavitaLibrary(string id, string name) { this.id = id; this.name = name; } } }