2023-05-20 14:07:38 +02:00
|
|
|
|
using System.Net.Http.Headers;
|
2023-05-20 12:53:19 +02:00
|
|
|
|
using System.Text.Json.Nodes;
|
2023-05-20 14:07:38 +02:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
2023-05-20 12:53:19 +02:00
|
|
|
|
|
|
|
|
|
namespace Tranga;
|
|
|
|
|
|
|
|
|
|
public class Komga
|
|
|
|
|
{
|
2023-05-20 14:07:38 +02:00
|
|
|
|
[System.Text.Json.Serialization.JsonRequired]public string baseUrl { get; }
|
|
|
|
|
[System.Text.Json.Serialization.JsonRequired]public string auth { get; }
|
2023-05-20 12:53:19 +02:00
|
|
|
|
|
2023-05-20 14:07:38 +02:00
|
|
|
|
public Komga(string baseUrl, string username, string password)
|
2023-05-20 12:53:19 +02:00
|
|
|
|
{
|
|
|
|
|
this.baseUrl = baseUrl;
|
2023-05-20 14:07:38 +02:00
|
|
|
|
this.auth = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{username}:{password}"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[JsonConstructor]
|
|
|
|
|
public Komga(string baseUrl, string auth)
|
|
|
|
|
{
|
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
|
this.auth = auth;
|
2023-05-20 12:53:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public KomgaLibrary[] GetLibraries()
|
|
|
|
|
{
|
2023-05-20 14:07:38 +02:00
|
|
|
|
Stream data = NetClient.MakeRequest($"{baseUrl}/api/v1/libraries", auth);
|
2023-05-20 12:53:19 +02:00
|
|
|
|
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.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool UpdateLibrary(string libraryId)
|
|
|
|
|
{
|
2023-05-20 14:07:38 +02:00
|
|
|
|
return NetClient.MakePost($"{baseUrl}/api/v1/libraries/{libraryId}/scan", auth);
|
2023-05-20 12:53:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public struct KomgaLibrary
|
|
|
|
|
{
|
|
|
|
|
public string id { get; }
|
|
|
|
|
public string name { get; }
|
|
|
|
|
|
|
|
|
|
public KomgaLibrary(string id, string name)
|
|
|
|
|
{
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class NetClient
|
|
|
|
|
{
|
2023-05-20 14:07:38 +02:00
|
|
|
|
public static Stream MakeRequest(string url, string auth)
|
2023-05-20 12:53:19 +02:00
|
|
|
|
{
|
|
|
|
|
HttpClient client = new();
|
2023-05-20 14:07:38 +02:00
|
|
|
|
HttpRequestMessage requestMessage = new HttpRequestMessage
|
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Get,
|
|
|
|
|
RequestUri = new Uri(url),
|
|
|
|
|
Headers =
|
|
|
|
|
{
|
|
|
|
|
{ "Accept", "application/json" },
|
|
|
|
|
{ "Authorization", new AuthenticationHeaderValue("Basic", auth).ToString() }
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-05-20 12:53:19 +02:00
|
|
|
|
HttpResponseMessage response = client.Send(requestMessage);
|
|
|
|
|
Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null;
|
|
|
|
|
return resultString;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 14:07:38 +02:00
|
|
|
|
public static bool MakePost(string url, string auth)
|
2023-05-20 12:53:19 +02:00
|
|
|
|
{
|
|
|
|
|
HttpClient client = new();
|
2023-05-20 14:07:38 +02:00
|
|
|
|
HttpRequestMessage requestMessage = new HttpRequestMessage
|
|
|
|
|
{
|
|
|
|
|
Method = HttpMethod.Post,
|
|
|
|
|
RequestUri = new Uri(url),
|
|
|
|
|
Headers =
|
|
|
|
|
{
|
|
|
|
|
{ "Accept", "application/json" },
|
|
|
|
|
{ "Authorization", new AuthenticationHeaderValue("Basic", auth).ToString() }
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-05-20 12:53:19 +02:00
|
|
|
|
HttpResponseMessage response = client.Send(requestMessage);
|
|
|
|
|
return response.IsSuccessStatusCode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|