2
0

Implement /v2/LibraryConnector/*

This commit is contained in:
Glax 2024-04-23 00:48:25 +02:00
parent a56555eee4
commit 0ced3a7dd9
3 changed files with 103 additions and 17 deletions

View File

@ -35,7 +35,8 @@ public partial class Server
private ValueTuple<HttpStatusCode, object?> GetV2JobTypes(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, Enum.GetNames(typeof(Job.JobType)));
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK,
Enum.GetValues<Job.JobType>().ToDictionary(b => (byte)b, b => Enum.GetName(b)));
}
private ValueTuple<HttpStatusCode, object?> PostV2JobCreateType(GroupCollection groups, Dictionary<string, string> requestParameters)
@ -43,7 +44,7 @@ public partial class Server
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.");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"JobType {groups[1].Value} does not exist.");
}
string? mangaId;

View File

@ -1,5 +1,6 @@
using System.Net;
using System.Text.RegularExpressions;
using Tranga.LibraryConnectors;
namespace Tranga.Server;
@ -7,31 +8,113 @@ public partial class Server
{
private ValueTuple<HttpStatusCode, object?> GetV2LibraryConnector(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, libraryConnectors);
}
private ValueTuple<HttpStatusCode, object?> GetV2LibraryConnectorTypes(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK,
Enum.GetValues<LibraryConnector.LibraryType>().ToDictionary(b => (byte)b, b => Enum.GetName(b)));
}
private ValueTuple<HttpStatusCode, object?> GetV2LibraryConnectorType(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 LibraryConnector.LibraryType libraryType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"LibraryType {groups[1].Value} does not exist.");
}
if(libraryConnectors.All(lc => lc.libraryType != libraryType))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"LibraryType {Enum.GetName(libraryType)} not configured.");
else
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, libraryConnectors.First(lc => lc.libraryType == libraryType));
}
private ValueTuple<HttpStatusCode, object?> PostV2LibraryConnectorType(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 LibraryConnector.LibraryType libraryType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"LibraryType {groups[1].Value} does not exist.");
}
if(!requestParameters.TryGetValue("URL", out string? url))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'url' missing.");
switch (libraryType)
{
case LibraryConnector.LibraryType.Kavita:
if(!requestParameters.TryGetValue("username", out string? username))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'username' missing.");
if(!requestParameters.TryGetValue("password", out string? password))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'password' missing.");
Kavita kavita = new (this, url, username, password);
libraryConnectors.RemoveWhere(lc => lc.libraryType == LibraryConnector.LibraryType.Kavita);
libraryConnectors.Add(kavita);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, kavita);
case LibraryConnector.LibraryType.Komga:
if(!requestParameters.TryGetValue("auth", out string? auth))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'auth' missing.");
Komga komga = new (this, url, auth);
libraryConnectors.RemoveWhere(lc => lc.libraryType == LibraryConnector.LibraryType.Komga);
libraryConnectors.Add(komga);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, komga);
default: return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.MethodNotAllowed, $"LibraryType {Enum.GetName(libraryType)} is not supported.");
}
}
private ValueTuple<HttpStatusCode, object?> PostV2LibraryConnectorTypeTest(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 LibraryConnector.LibraryType libraryType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"LibraryType {groups[1].Value} does not exist.");
}
if(!requestParameters.TryGetValue("URL", out string? url))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'url' missing.");
switch (libraryType)
{
case LibraryConnector.LibraryType.Kavita:
if(!requestParameters.TryGetValue("username", out string? username))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'username' missing.");
if(!requestParameters.TryGetValue("password", out string? password))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'password' missing.");
Kavita kavita = new (this, url, username, password);
return kavita.Test() switch
{
true => new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, kavita),
_ => new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.FailedDependency, kavita)
};
case LibraryConnector.LibraryType.Komga:
if(!requestParameters.TryGetValue("auth", out string? auth))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotAcceptable, "Parameter 'auth' missing.");
Komga komga = new (this, url, auth);
return komga.Test() switch
{
true => new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, komga),
_ => new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.FailedDependency, komga)
};
default: return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.MethodNotAllowed, $"LibraryType {Enum.GetName(libraryType)} is not supported.");
}
}
private ValueTuple<HttpStatusCode, object?> DeleteV2LibraryConnectorType(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 LibraryConnector.LibraryType libraryType))
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"LibraryType {groups[1].Value} does not exist.");
}
if(libraryConnectors.All(lc => lc.libraryType != libraryType))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"LibraryType {Enum.GetName(libraryType)} not configured.");
else
{
libraryConnectors.Remove(libraryConnectors.First(lc => lc.libraryType == libraryType));
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}
}
}

View File

@ -640,9 +640,9 @@ Returns the Library-Connector for the specified Type.
[LibraryConnector](Types.md#libraryconnector)
| StatusCode | Meaning |
|------------|---------------------------------------|
| 404 | Library Connector Type does not exist |
| StatusCode | Meaning |
|------------|----------------------------------------------------|
| 404 | Library Connector of specified Type does not exist |
</details>
### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/LibraryConnector/<Type>`
@ -681,6 +681,7 @@ Creates a Library-Connector of the specified Type.
| StatusCode | Meaning |
|------------|----------------------------------|
| 404 | Library Connector does not exist |
| 406 | Missing Parameter |
| 500 | Parsing Error |
</details>
@ -715,12 +716,13 @@ Tests a Library-Connector of the specified Type.
<details>
<summary>Returns</summary>
| StatusCode | Meaning |
|------------|---------------------------------------|
| 200 | Test successful |
| 404 | Library Connector Type does not exist |
| 408 | Test failed |
| 500 | Parsing Error |
| StatusCode | Meaning |
|------------|------------------------------------|
| 200 | Test successful |
| 404 | Library Connector does not exist |
| 406 | Missing Parameter |
| 424 | Test failed |
| 500 | Parsing Error |
</details>
### <sub>![DELETE](https://img.shields.io/badge/DELETE-f00)</sub> `/v2/LibraryConnector/<Type>`