mirror of
https://github.com/C9Glax/tranga.git
synced 2025-02-24 00:00:13 +01:00
74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
|
|
namespace API.Schema.LibraryConnectors;
|
|
|
|
public class Komga : LibraryConnector
|
|
{
|
|
public Komga(string baseUrl, string auth) : base(TokenGen.CreateToken(typeof(Komga), baseUrl), LibraryType.Komga,
|
|
baseUrl, auth)
|
|
{
|
|
}
|
|
|
|
public Komga(string baseUrl, string username, string password)
|
|
: this(baseUrl, Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}")))
|
|
{
|
|
}
|
|
|
|
protected override void UpdateLibraryInternal()
|
|
{
|
|
foreach (KomgaLibrary lib in GetLibraries())
|
|
NetClient.MakePost($"{BaseUrl}/api/v1/libraries/{lib.id}/scan", "Basic", Auth);
|
|
}
|
|
|
|
internal override bool Test()
|
|
{
|
|
foreach (KomgaLibrary lib in GetLibraries())
|
|
if (NetClient.MakePost($"{BaseUrl}/api/v1/libraries/{lib.id}/scan", "Basic", Auth))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches all libraries available to the user
|
|
/// </summary>
|
|
/// <returns>Array of KomgaLibraries</returns>
|
|
private IEnumerable<KomgaLibrary> GetLibraries()
|
|
{
|
|
Stream data = NetClient.MakeRequest($"{BaseUrl}/api/v1/libraries", "Basic", Auth);
|
|
if (data == Stream.Null)
|
|
{
|
|
return Array.Empty<KomgaLibrary>();
|
|
}
|
|
JsonArray? result = JsonSerializer.Deserialize<JsonArray>(data);
|
|
if (result is null)
|
|
{
|
|
return Array.Empty<KomgaLibrary>();
|
|
}
|
|
|
|
HashSet<KomgaLibrary> ret = new();
|
|
|
|
foreach (JsonNode? jsonNode in result)
|
|
{
|
|
var jObject = (JsonObject?)jsonNode;
|
|
string libraryId = jObject!["id"]!.GetValue<string>();
|
|
string libraryName = jObject["name"]!.GetValue<string>();
|
|
ret.Add(new KomgaLibrary(libraryId, libraryName));
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
private struct KomgaLibrary
|
|
{
|
|
public string id { get; }
|
|
// ReSharper disable once UnusedAutoPropertyAccessor.Local
|
|
public string name { get; }
|
|
|
|
public KomgaLibrary(string id, string name)
|
|
{
|
|
this.id = id;
|
|
this.name = name;
|
|
}
|
|
}
|
|
} |