Implement all /v2/Settings

This commit is contained in:
Glax 2024-04-22 03:03:17 +02:00
parent 3adb103fc4
commit cce4901a5d
2 changed files with 82 additions and 25 deletions

View File

@ -1,5 +1,6 @@
using System.Net;
using System.Text.RegularExpressions;
using Tranga.MangaConnectors;
namespace Tranga.Server;
@ -7,56 +8,102 @@ public partial class Server
{
private ValueTuple<HttpStatusCode, object?> GetV2Settings(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, settings);
}
private ValueTuple<HttpStatusCode, object?> GetV2SettingsUserAgent(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, settings.userAgent);
}
private ValueTuple<HttpStatusCode, object?> PostV2SettingsUserAgent(GroupCollection groups, Dictionary<string, string> requestParameters)
private ValueTuple<HttpStatusCode, object?> PostV2SettingsUserAgent(GroupCollection groups, Dictionary<string, string?> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if (!requestParameters.TryGetValue("value", out string? userAgent))
{
settings.UpdateUserAgent(null);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.Accepted, null);
}
else
{
settings.UpdateUserAgent(userAgent);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}
}
private ValueTuple<HttpStatusCode, object?> GetV2SettingsRateLimitTypes(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, Enum.GetValues<RequestType>().ToDictionary(b =>(byte)b, b => Enum.GetName(b)) );
}
private ValueTuple<HttpStatusCode, object?> GetV2SettingsRateLimit(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, settings.requestLimits);
}
private ValueTuple<HttpStatusCode, object?> PostV2SettingsRateLimit(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
foreach (KeyValuePair<string, string> kv in requestParameters)
{
if(!Enum.TryParse(kv.Key, out RequestType requestType) ||
!int.TryParse(kv.Value, out int requestsPerMinute))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.InternalServerError, null);
settings.requestLimits[requestType] = requestsPerMinute;
settings.ExportSettings();
}
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, settings.requestLimits);
}
private ValueTuple<HttpStatusCode, object?> GetV2SettingsRateLimitType(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if(groups.Count < 1 ||
!Enum.TryParse(groups[1].Value, out RequestType requestType))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"RequestType {groups[1].Value}");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, settings.requestLimits[requestType]);
}
private ValueTuple<HttpStatusCode, object?> PostV2SettingsRateLimitType(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if(groups.Count < 1 ||
!Enum.TryParse(groups[1].Value, out RequestType requestType))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, $"RequestType {groups[1].Value}");
if (!requestParameters.TryGetValue("value", out string? requestsPerMinuteStr) ||
!int.TryParse(requestsPerMinuteStr, out int requestsPerMinute))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.InternalServerError, "Errors parsing requestsPerMinute");
settings.requestLimits[requestType] = requestsPerMinute;
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}
private ValueTuple<HttpStatusCode, object?> GetV2SettingsAprilFoolsMode(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, settings.aprilFoolsMode);
}
private ValueTuple<HttpStatusCode, object?> PostV2SettingsAprilFoolsMode(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if (!requestParameters.TryGetValue("value", out string? trueFalseStr) ||
!bool.TryParse(trueFalseStr, out bool trueFalse))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.InternalServerError, "Errors parsing 'value'");
settings.UpdateAprilFoolsMode(trueFalse);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}
private ValueTuple<HttpStatusCode, object?> PostV2SettingsDownloadLocation(GroupCollection groups, Dictionary<string, string> requestParameters)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotImplemented, "Not Implemented");
if (!requestParameters.TryGetValue("location", out string? folderPath))
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.NotFound, "Missing Parameter 'location'");
try
{
bool moveFiles = requestParameters.TryGetValue("moveFiles", out string? moveFilesStr) switch
{
false => true,
true => bool.Parse(moveFilesStr!)
};
settings.UpdateDownloadLocation(folderPath, moveFiles);
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.OK, null);
}
catch (FormatException)
{
return new ValueTuple<HttpStatusCode, object?>(HttpStatusCode.InternalServerError, "Error Parsing Parameter 'moveFiles'");
}
}
}

View File

@ -436,6 +436,12 @@ Sets the User Agent. If left empty, User Agent is reset to default.
<details>
<summary>Returns</summary>
| StatusCode | Meaning |
|------------|-------------------|
| 202 | UserAgent Reset |
| 201 | UserAgent Updated |
</details>
### <sub>![GET](https://img.shields.io/badge/GET-0f0)</sub> `/v2/Settings/RateLimit/Types`
@ -445,7 +451,7 @@ Returns the configurable Rate-Limits.
<details>
<summary>Returns</summary>
List of Rate-Limit-Names.
Key-Value-Pairs of Values and RateLimit-Names.
</details>
### <sub>![GET](https://img.shields.io/badge/GET-0f0)</sub> `/v2/Settings/RateLimit`
@ -467,19 +473,19 @@ Sets the Rate-Limits for all Requests. If left empty, resets to default Rate-Lim
For each Rate-Limit set as follows:
| Parameter | Value |
|------------------------------------|---------------------|
| [Type](#-v2settingsratelimittypes) | Requests per Minute |
| Parameter | Value |
|--------------------------------------|-----------------------|
| [Type](#-v2settingsratelimittypes) | Requests per Minute |
`Type` is returned by [GET /v2/Settings/RateLimit/Types](#-v2settingsratelimittypes)
`Type` is returned by [GET /v2/Settings/RateLimit/Types](#-v2settingsratelimittypes) and should be supplied as string
</details>
<details>
<summary>Returns</summary>
| StatusCode | Meaning |
|------------|--------------------------------|
| 404 | Rate-Limit-Name does not exist |
| StatusCode | Meaning |
|------------|------------------------------------------------|
| 500 | Error parsing RequestType or RequestsPerMinute |
</details>
### <sub>![GET](https://img.shields.io/badge/GET-0f0)</sub> `/v2/Settings/RateLimit/<Type>`
@ -496,6 +502,10 @@ Returns the current Rate-Limit for the Request-Type.
<summary>Returns</summary>
Integer with Requests per Minute.
| StatusCode | Meaning |
|------------|-----------------------------------------------|
| 404 | Error parsing RequestType |
</details>
### <sub>![POST](https://img.shields.io/badge/POST-00f)</sub> `/v2/Settings/RateLimit/<Type>`
@ -547,7 +557,6 @@ Enables/Disables April-Fools-Mode.
| StatusCode | Meaning |
|------------|--------------------------------|
| 404 | Rate-Limit-Name does not exist |
| 500 | Parsing Error |
</details>
@ -568,10 +577,11 @@ Updates the default Download-Location.
<summary>Returns</summary>
| StatusCode | Meaning |
|------------|--------------------------|
| 200 | Successfully changed |
| 500 | Files could not be moved |
| StatusCode | Meaning |
|------------|---------------------------------|
| 200 | Successfully changed |
| 404 | Parameter 'location' is missing |
| 500 | Parsing Error |
</details>
## Library Connectors <sup>[^top](#top)</sup>