Tranga-Website/Tranga/LibraryManager.cs

120 lines
4.3 KiB
C#
Raw Normal View History

using System.Net;
using System.Net.Http.Headers;
using Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Tranga.LibraryManagers;
namespace Tranga;
public abstract class LibraryManager
{
public enum LibraryType : byte
{
Komga = 0,
Kavita = 1
}
2023-06-03 16:24:14 +02:00
public LibraryType libraryType { get; }
public string baseUrl { get; }
2023-06-03 21:26:29 +02:00
public string auth { get; } //Base64 encoded, if you use your password everywhere, you have problems
protected Logger? logger;
/// <param name="baseUrl">Base-URL of Komga instance, no trailing slashes(/)</param>
/// <param name="auth">Base64 string of username and password (username):(password)</param>
/// <param name="logger"></param>
/// <param name="libraryType"></param>
2023-06-03 16:24:14 +02:00
protected LibraryManager(string baseUrl, string auth, Logger? logger, LibraryType libraryType)
{
this.baseUrl = baseUrl;
this.auth = auth;
this.logger = logger;
2023-06-03 16:24:14 +02:00
this.libraryType = libraryType;
}
public abstract void UpdateLibrary();
public void AddLogger(Logger newLogger)
{
this.logger = newLogger;
}
protected static class NetClient
{
2023-06-03 21:26:29 +02:00
public static Stream MakeRequest(string url, string authScheme, string auth, Logger? logger)
{
2023-06-03 21:26:29 +02:00
HttpClient client = new();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authScheme, auth);
HttpRequestMessage requestMessage = new ()
{
Method = HttpMethod.Get,
RequestUri = new Uri(url)
};
HttpResponseMessage response = client.Send(requestMessage);
2023-06-07 00:31:27 +02:00
logger?.WriteLine("LibraryManager", $"GET {url} -> {(int)response.StatusCode}: {response.ReasonPhrase}");
if(response.StatusCode is HttpStatusCode.Unauthorized && response.RequestMessage!.RequestUri!.AbsoluteUri != url)
2023-06-03 21:26:29 +02:00
return MakeRequest(response.RequestMessage!.RequestUri!.AbsoluteUri, authScheme, auth, logger);
else if (response.IsSuccessStatusCode)
return response.Content.ReadAsStream();
else
return Stream.Null;
}
2023-06-03 21:26:29 +02:00
public static bool MakePost(string url, string authScheme, string auth, Logger? logger)
{
2023-06-03 21:26:29 +02:00
HttpClient client = new()
{
DefaultRequestHeaders =
{
{ "Accept", "application/json" },
2023-06-03 21:26:29 +02:00
{ "Authorization", new AuthenticationHeaderValue(authScheme, auth).ToString() }
}
};
HttpRequestMessage requestMessage = new ()
{
Method = HttpMethod.Post,
RequestUri = new Uri(url)
};
HttpResponseMessage response = client.Send(requestMessage);
2023-06-07 00:31:27 +02:00
logger?.WriteLine("LibraryManager", $"POST {url} -> {(int)response.StatusCode}: {response.ReasonPhrase}");
if(response.StatusCode is HttpStatusCode.Unauthorized && response.RequestMessage!.RequestUri!.AbsoluteUri != url)
2023-06-03 21:26:29 +02:00
return MakePost(response.RequestMessage!.RequestUri!.AbsoluteUri, authScheme, auth, logger);
else if (response.IsSuccessStatusCode)
return true;
else
return false;
}
}
public class LibraryManagerJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(LibraryManager));
}
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);
if (jo["libraryType"]!.Value<Int64>() == (Int64)LibraryType.Komga)
return jo.ToObject<Komga>(serializer)!;
if (jo["libraryType"]!.Value<Int64>() == (Int64)LibraryType.Kavita)
return jo.ToObject<Kavita>(serializer)!;
throw new Exception();
}
public override bool CanWrite => false;
/// <summary>
/// Don't call this
/// </summary>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
throw new Exception("Dont call this");
}
}
}