From a8f0f1af152050a9cb0fac34d4b42adc30e4f59b Mon Sep 17 00:00:00 2001 From: glax Date: Sat, 26 Aug 2023 02:43:24 +0200 Subject: [PATCH] More API Requests --- Tranga/Jobs/JobBoss.cs | 5 ++- Tranga/Server.cs | 78 ++++++++++++++++++++++++++++++++++++------ Tranga/Tranga.cs | 9 ++++- 3 files changed, 77 insertions(+), 15 deletions(-) diff --git a/Tranga/Jobs/JobBoss.cs b/Tranga/Jobs/JobBoss.cs index 34a6bd6..f688613 100644 --- a/Tranga/Jobs/JobBoss.cs +++ b/Tranga/Jobs/JobBoss.cs @@ -1,11 +1,10 @@ -using System.Runtime.CompilerServices; -using Tranga.MangaConnectors; +using Tranga.MangaConnectors; namespace Tranga.Jobs; public class JobBoss : GlobalBase { - private HashSet jobs { get; init; } + public HashSet jobs { get; init; } private Dictionary> mangaConnectorJobQueue { get; init; } public JobBoss(GlobalBase clone) : base(clone) diff --git a/Tranga/Server.cs b/Tranga/Server.cs index 4109fc2..dba877d 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -3,6 +3,7 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using Newtonsoft.Json; +using Tranga.Jobs; using Tranga.MangaConnectors; namespace Tranga; @@ -86,24 +87,79 @@ public class Server : GlobalBase private void HandleGet(HttpListenerRequest request, Stream content, HttpListenerResponse response) { Dictionary requestVariables = GetRequestVariables(request.Url!.Query); - switch (request.Url!.LocalPath[1..]) + string? connectorName, title, internalId, jobId; + MangaConnector connector; + Publication publication; + Job job; + string path = Regex.Match(request.Url!.LocalPath, @"[A-z0-9]+(\/[A-z0-9]+)*").Value; + switch (path) { case "Connectors": SendResponse(HttpStatusCode.OK, response, _parent.GetConnectors().Select(connector => connector.name).ToArray()); break; case "Publications/FromConnector": - if (requestVariables.TryGetValue("connector", out string? connectorName) && - requestVariables.TryGetValue("title", out string? title)) + if (!requestVariables.TryGetValue("connector", out connectorName) || + !requestVariables.TryGetValue("title", out title) || + _parent.GetConnector(connectorName) is null) { - MangaConnector? connector = _parent.GetConnector(connectorName); - if (connector is null || title.Length < 4) - { - SendResponse(HttpStatusCode.BadRequest, response); - return; - } - SendResponse(HttpStatusCode.OK, response, connector.GetPublications(title)); - }else SendResponse(HttpStatusCode.BadRequest, response); + break; + } + connector = _parent.GetConnector(connectorName)!; + SendResponse(HttpStatusCode.OK, response, connector.GetPublications(title)); + break; + case "Publications/Chapters": + if(!requestVariables.TryGetValue("connector", out connectorName) || + !requestVariables.TryGetValue("internalId", out internalId) || + _parent.GetConnector(connectorName) is null || + _parent.GetPublicationById(internalId) is null) + { + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + connector = _parent.GetConnector(connectorName)!; + publication = (Publication)_parent.GetPublicationById(internalId)!; + SendResponse(HttpStatusCode.OK, response, connector.GetChapters(publication)); + break; + case "Tasks": + if (!requestVariables.TryGetValue("jobId", out jobId)) + { + if(!_parent._jobBoss.jobs.Any(jjob => jjob.id == jobId)) + SendResponse(HttpStatusCode.BadRequest, response); + else + SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs.First(jjob => jjob.id == jobId)); + break; + } + SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs); + break; + case "Tasks/Progress": + if (!requestVariables.TryGetValue("jobId", out jobId)) + { + if(!_parent._jobBoss.jobs.Any(jjob => jjob.id == jobId)) + SendResponse(HttpStatusCode.BadRequest, response); + else + SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs.First(jjob => jjob.id == jobId).progressToken); + break; + } + SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs.Select(jjob => jjob.progressToken)); + break; + case "Tasks/Running": + SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs.Where(jjob => jjob.progressToken.state is ProgressToken.State.Running)); + break; + case "Tasks/Waiting": + SendResponse(HttpStatusCode.OK, response, _parent._jobBoss.jobs.Where(jjob => jjob.progressToken.state is ProgressToken.State.Standby)); + break; + case "Settings": + SendResponse(HttpStatusCode.OK, response, settings); + break; + case "NotificationConnectors": + SendResponse(HttpStatusCode.OK, response, notificationConnectors); + break; + case "LibraryConnectors": + SendResponse(HttpStatusCode.OK, response, libraryConnectors); + break; + default: + SendResponse(HttpStatusCode.BadRequest, response); break; } } diff --git a/Tranga/Tranga.cs b/Tranga/Tranga.cs index 398879c..0034a70 100644 --- a/Tranga/Tranga.cs +++ b/Tranga/Tranga.cs @@ -7,7 +7,7 @@ namespace Tranga; public partial class Tranga : GlobalBase { public bool keepRunning; - private JobBoss _jobBoss; + public JobBoss _jobBoss; private Server server; private HashSet connectors; @@ -39,6 +39,13 @@ public partial class Tranga : GlobalBase return connectors; } + public Publication? GetPublicationById(string internalId) + { + if (cachedPublications.Exists(publication => publication.internalId == internalId)) + return cachedPublications.First(publication => publication.internalId == internalId); + return null; + } + private void StartJobBoss() { Thread t = new (() =>