Changed the Creation Job API to a single Endpoint /v2/Job/Create/<Type>

Added and implemented GET /v2/Job/Types
Implemented /v2/Job/<jobId>
Implemented /v2/Job/<jobId>/StartNow
Implemented /v2/Job/<jobId>/Cancel
Implemented /v2/Job/<jobId>/SetInterval
This commit is contained in:
Glax 2024-04-22 00:00:35 +02:00
parent 6d48a100ca
commit 49cfff8a2f
4 changed files with 95 additions and 81 deletions

View File

@ -13,7 +13,7 @@ public abstract class Job : GlobalBase
public string id => GetId(); public string id => GetId();
internal IEnumerable<Job>? subJobs { get; private set; } internal IEnumerable<Job>? subJobs { get; private set; }
public string? parentJobId { get; init; } public string? parentJobId { get; init; }
public enum JobType : byte { DownloadChapterJob, DownloadNewChaptersJob, UpdateMetaDataJob } public enum JobType : byte { DownloadChapterJob, DownloadNewChaptersJob, UpdateMetaDataJob, MonitorManga }
public JobType jobType; public JobType jobType;

View File

@ -31,10 +31,8 @@ public partial class Server : GlobalBase, IDisposable
new ("GET", @"/v2/Jobs/Running", GetV2JobsRunning), new ("GET", @"/v2/Jobs/Running", GetV2JobsRunning),
new ("GET", @"/v2/Jobs/Waiting", GetV2JobsWaiting), new ("GET", @"/v2/Jobs/Waiting", GetV2JobsWaiting),
new ("GET", @"/v2/Jobs/Monitoring", GetV2JobsMonitoring), new ("GET", @"/v2/Jobs/Monitoring", GetV2JobsMonitoring),
new ("POST", @"/v2/Jobs/Create/Monitor/(^[-A-Za-z0-9+/]*={0,3}$)", PostV2JobsCreateMonitorInternalId), new ("Get", @"/v2/Job/Types", GetV2JobTypes),
new ("POST", @"/v2/Jobs/Create/DownloadNewChapters/(^[-A-Za-z0-9+/]*={0,3}$)", PostV2JobsCreateDownloadNewChaptersInternalId), new ("POST", @"/v2/Job/Create/([a-zA-Z]+)>", PostV2JobsCreateType),
new ("POST", @"/v2/Jobs/Create/UpdateMetadata", PostV2JobsCreateUpdateMetadata),
new ("POST", @"/v2/Jobs/Create/UpdateMetadata/(^[-A-Za-z0-9+/]*={0,3}$)", PostV2JobsCreateUpdateMetadataInternalId),
new ("GET", @"/v2/Job/([a-zA-Z\.]+-[-A-Za-z0-9+/]*={0,3}(?:-[0-9]+)?)", GetV2JobJobId), new ("GET", @"/v2/Job/([a-zA-Z\.]+-[-A-Za-z0-9+/]*={0,3}(?:-[0-9]+)?)", GetV2JobJobId),
new ("DELETE", @"/v2/Job/([a-zA-Z\.]+-[-A-Za-z0-9+/]*={0,3}(?:-[0-9]+)?)", DeleteV2JobJobId), new ("DELETE", @"/v2/Job/([a-zA-Z\.]+-[-A-Za-z0-9+/]*={0,3}(?:-[0-9]+)?)", DeleteV2JobJobId),
new ("GET", @"/v2/Job/([a-zA-Z\.]+-[-A-Za-z0-9+/]*={0,3}(?:-[0-9]+)?)/Progress", GetV2JobJobIdProgress), new ("GET", @"/v2/Job/([a-zA-Z\.]+-[-A-Za-z0-9+/]*={0,3}(?:-[0-9]+)?)/Progress", GetV2JobJobIdProgress),

View File

@ -1,6 +1,7 @@
using System.Net; using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Tranga.Jobs; using Tranga.Jobs;
using Tranga.MangaConnectors;
namespace Tranga.Server; namespace Tranga.Server;
@ -32,24 +33,55 @@ public partial class Server
.Select(job => job.id)); .Select(job => job.id));
} }
private ValueTuple<HttpStatusCode, object?> PostV2JobsCreateMonitorInternalId(GroupCollection groups, Dictionary<string, string> requestParameters) private ValueTuple<HttpStatusCode, object?> GetV2JobTypes(GroupCollection groups, Dictionary<string, string> requestParameters)
{ {
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented"); return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, Enum.GetNames(typeof(Job.JobType)));
} }
private ValueTuple<HttpStatusCode, object?> PostV2JobsCreateDownloadNewChaptersInternalId(GroupCollection groups, Dictionary<string, string> requestParameters) private ValueTuple<HttpStatusCode, object?> PostV2JobsCreateType(GroupCollection groups, Dictionary<string, string> requestParameters)
{ {
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented"); if (groups.Count < 1 ||
} !Enum.TryParse(groups[1].Value, true, out Job.JobType jobType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"Manga with ID: '{groups[1].Value}' does not exist.");
}
private ValueTuple<HttpStatusCode, object?> PostV2JobsCreateUpdateMetadata(GroupCollection groups, Dictionary<string, string> requestParameters) string? connectorStr, mangaId;
{ MangaConnector? connector;
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented"); Manga? manga;
} switch (jobType)
{
private ValueTuple<HttpStatusCode, object?> PostV2JobsCreateUpdateMetadataInternalId(GroupCollection groups, Dictionary<string, string> requestParameters) case Job.JobType.MonitorManga:
{ if(!requestParameters.TryGetValue("connector", out connectorStr) ||
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented"); !_parent.TryGetConnector(connectorStr, out connector) ||
connector is null)
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, "Connector Parameter missing, or is not a valid connector.");
if(!requestParameters.TryGetValue("internalId", out mangaId) ||
!_parent.TryGetPublicationById(mangaId, out manga) ||
manga is null)
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, "InternalId Parameter missing, or is not a valid ID.");
if(!requestParameters.TryGetValue("interval", out string? intervalStr) ||
!TimeSpan.TryParse(intervalStr, out TimeSpan interval))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.InternalServerError, "Interval Parameter missing, or is not in correct format.");
requestParameters.TryGetValue("language", out string? language);
_parent.jobBoss.AddJob(new DownloadNewChapters(this, connector, (Manga)manga, true, interval, language));
break;
case Job.JobType.UpdateMetaDataJob:
if(!requestParameters.TryGetValue("connector", out connectorStr) ||
!_parent.TryGetConnector(connectorStr, out connector) ||
connector is null)
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, "Connector Parameter missing, or is not a valid connector.");
if(!requestParameters.TryGetValue("internalId", out mangaId) ||
!_parent.TryGetPublicationById(mangaId, out manga) ||
manga is null)
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, "InternalId Parameter missing, or is not a valid ID.");
_parent.jobBoss.AddJob(new UpdateMetadata(this, connector, (Manga)manga));
break;
case Job.JobType.DownloadNewChaptersJob: //TODO
case Job.JobType.DownloadChapterJob: //TODO
default: return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.MethodNotAllowed, $"JobType {Enum.GetName(jobType)} is not supported.");
}
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "How'd you get here.");
} }
private ValueTuple<HttpStatusCode, object?> GetV2JobJobId(GroupCollection groups, Dictionary<string, string> requestParameters) private ValueTuple<HttpStatusCode, object?> GetV2JobJobId(GroupCollection groups, Dictionary<string, string> requestParameters)
@ -58,14 +90,22 @@ public partial class Server
!_parent.jobBoss.TryGetJobById(groups[1].Value, out Job? job) || !_parent.jobBoss.TryGetJobById(groups[1].Value, out Job? job) ||
job is null) job is null)
{ {
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.BadRequest, $"Job with ID: '{groups[1].Value}' does not exist."); return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"Job with ID: '{groups[1].Value}' does not exist.");
} }
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, job); return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, job);
} }
private ValueTuple<HttpStatusCode, object?> DeleteV2JobJobId(GroupCollection groups, Dictionary<string, string> requestParameters) private ValueTuple<HttpStatusCode, object?> DeleteV2JobJobId(GroupCollection groups, Dictionary<string, string> requestParameters)
{ {
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented"); if (groups.Count < 1 ||
!_parent.jobBoss.TryGetJobById(groups[1].Value, out Job? job) ||
job is null)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"Job with ID: '{groups[1].Value}' does not exist.");
}
_parent.jobBoss.RemoveJob(job);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
} }
private ValueTuple<HttpStatusCode, object?> GetV2JobJobIdProgress(GroupCollection groups, Dictionary<string, string> requestParameters) private ValueTuple<HttpStatusCode, object?> GetV2JobJobIdProgress(GroupCollection groups, Dictionary<string, string> requestParameters)
@ -82,12 +122,26 @@ public partial class Server
private ValueTuple<HttpStatusCode, object?> PostV2JobJobIdStartNow(GroupCollection groups, Dictionary<string, string> requestParameters) private ValueTuple<HttpStatusCode, object?> PostV2JobJobIdStartNow(GroupCollection groups, Dictionary<string, string> requestParameters)
{ {
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented"); if (groups.Count < 1 ||
!_parent.jobBoss.TryGetJobById(groups[1].Value, out Job? job) ||
job is null)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"Job with ID: '{groups[1].Value}' does not exist.");
}
_parent.jobBoss.AddJobs(job.ExecuteReturnSubTasks(_parent.jobBoss));
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
} }
private ValueTuple<HttpStatusCode, object?> PostV2JobJobIdCancel(GroupCollection groups, Dictionary<string, string> requestParameters) private ValueTuple<HttpStatusCode, object?> PostV2JobJobIdCancel(GroupCollection groups, Dictionary<string, string> requestParameters)
{ {
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented"); if (groups.Count < 1 ||
!_parent.jobBoss.TryGetJobById(groups[1].Value, out Job? job) ||
job is null)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"Job with ID: '{groups[1].Value}' does not exist.");
}
job.Cancel();
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
} }
} }

View File

@ -218,40 +218,33 @@ Returns all Monitoring Jobs.
List of JobIds. List of JobIds.
</details> </details>
### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Jobs/Create/Monitor/<internalId>` ### <sub>![GET](https://img.shields.io/badge/GET-0f0)</sub> `/v2/Job/Types`
Creates a Monitoring-Job for the specified Manga at the specified Interval. Returns the valid Job-Types.
<details>
<summary>Request</summary>
`internalId` is returned in the response of
* [GET /v2/Connector/*ConnectorName*/GetManga](#-v2connectorconnectornamegetmanga)
* [GET /v2/Job/*jobId*](#-v2jobjobid)
| Parameter | Value |
|-----------|--------------------------------------------------------|
| interval | Interval at which the Job is re-run in HH:MM:SS format |
</details>
<details> <details>
<summary>Returns</summary> <summary>Returns</summary>
[Job](Types.md#job) List of strings.
| StatusCode | Meaning |
|------------|--------------------------------------------|
| 404 | Manga with `internalId` could not be found |
| 500 | Error parsing interval |
</details> </details>
### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Jobs/Create/DownloadNewChapters/<internalId>` ### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Job/Create/<Type>`
Creates a Job to check for new Chapters and Download new ones of the specified Manga. Creates a Job.
<details> <details>
<summary>Request</summary> <summary>Request</summary>
`Type` is returned in the response of [GET /v2/Job/Types](#-v2jobtypes)
| Parameter | Value |
|------------|---------------------------------------------------------------------------------------------------|
| connector | Name of the connector to use |
| internalId | Manga ID |
| *interval* | Interval at which the Job is re-run in HH:MM:SS format<br />Only for MonitorManga, UpdateMetadata |
| *language* | Translated language<br />Only for MonitorManga, DownloadNewChapters and DownloadChapter |
`internalId` is returned in the response of `internalId` is returned in the response of
* [GET /v2/Connector/*ConnectorName*/GetManga](#-v2connectorconnectornamegetmanga) * [GET /v2/Connector/*ConnectorName*/GetManga](#-v2connectorconnectornamegetmanga)
* [GET /v2/Job/*jobId*](#-v2jobjobid) * [GET /v2/Job/*jobId*](#-v2jobjobid)
@ -262,41 +255,10 @@ Creates a Job to check for new Chapters and Download new ones of the specified M
[Job](Types.md#job) [Job](Types.md#job)
| StatusCode | Meaning | | StatusCode | Meaning |
|------------|--------------------------------------------| |------------|------------------------------------------|
| 404 | Manga with `internalId` could not be found | | 404 | Parameter missing or could not be found. |
</details> | 500 | Error parsing interval |
### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Jobs/Create/UpdateMetadata`
Creates a Job to update the Metadata of all Manga.
<details>
<summary>Returns</summary>
[Job](Types.md#job)
</details>
### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Jobs/Create/UpdateMetadata/<internalId>`
Updates the Metadata of the specified Manga.
<details>
<summary>Request</summary>
`internalId` is returned in the response of
* [GET /v2/Connector/*ConnectorName*/GetManga](#-v2connectorconnectornamegetmanga)
* [GET /v2/Job/*jobId*](#-v2jobjobid)
</details>
<details>
<summary>Returns</summary>
[Job](Types.md#job)
| StatusCode | Meaning |
|------------|--------------------------------------------|
| 404 | Manga with `internalId` could not be found |
</details> </details>
### <sub>![GET](https://img.shields.io/badge/GET-0f0)</sub> `/v2/Job/<jobId>` ### <sub>![GET](https://img.shields.io/badge/GET-0f0)</sub> `/v2/Job/<jobId>`