From cce4901a5d48b0ca9d973c3c51e7bb597afdf711 Mon Sep 17 00:00:00 2001 From: Glax Date: Mon, 22 Apr 2024 03:03:17 +0200 Subject: [PATCH] Implement all /v2/Settings --- Tranga/Server/v2Settings.cs | 71 ++++++++++++++++++++++++++++++------- docs/API_Calls_v2.md | 36 ++++++++++++------- 2 files changed, 82 insertions(+), 25 deletions(-) diff --git a/Tranga/Server/v2Settings.cs b/Tranga/Server/v2Settings.cs index 6615726..7f7ed5b 100644 --- a/Tranga/Server/v2Settings.cs +++ b/Tranga/Server/v2Settings.cs @@ -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 GetV2Settings(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + return new ValueTuple(HttpStatusCode.OK, settings); } private ValueTuple GetV2SettingsUserAgent(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + return new ValueTuple(HttpStatusCode.OK, settings.userAgent); } - private ValueTuple PostV2SettingsUserAgent(GroupCollection groups, Dictionary requestParameters) + private ValueTuple PostV2SettingsUserAgent(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + if (!requestParameters.TryGetValue("value", out string? userAgent)) + { + settings.UpdateUserAgent(null); + return new ValueTuple(HttpStatusCode.Accepted, null); + } + else + { + settings.UpdateUserAgent(userAgent); + return new ValueTuple(HttpStatusCode.OK, null); + } } private ValueTuple GetV2SettingsRateLimitTypes(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + return new ValueTuple(HttpStatusCode.OK, Enum.GetValues().ToDictionary(b =>(byte)b, b => Enum.GetName(b)) ); } private ValueTuple GetV2SettingsRateLimit(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + return new ValueTuple(HttpStatusCode.OK, settings.requestLimits); } private ValueTuple PostV2SettingsRateLimit(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + foreach (KeyValuePair kv in requestParameters) + { + if(!Enum.TryParse(kv.Key, out RequestType requestType) || + !int.TryParse(kv.Value, out int requestsPerMinute)) + return new ValueTuple(HttpStatusCode.InternalServerError, null); + settings.requestLimits[requestType] = requestsPerMinute; + settings.ExportSettings(); + } + return new ValueTuple(HttpStatusCode.OK, settings.requestLimits); } private ValueTuple GetV2SettingsRateLimitType(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + if(groups.Count < 1 || + !Enum.TryParse(groups[1].Value, out RequestType requestType)) + return new ValueTuple(HttpStatusCode.NotFound, $"RequestType {groups[1].Value}"); + return new ValueTuple(HttpStatusCode.OK, settings.requestLimits[requestType]); } private ValueTuple PostV2SettingsRateLimitType(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + if(groups.Count < 1 || + !Enum.TryParse(groups[1].Value, out RequestType requestType)) + return new ValueTuple(HttpStatusCode.NotFound, $"RequestType {groups[1].Value}"); + if (!requestParameters.TryGetValue("value", out string? requestsPerMinuteStr) || + !int.TryParse(requestsPerMinuteStr, out int requestsPerMinute)) + return new ValueTuple(HttpStatusCode.InternalServerError, "Errors parsing requestsPerMinute"); + settings.requestLimits[requestType] = requestsPerMinute; + return new ValueTuple(HttpStatusCode.OK, null); } private ValueTuple GetV2SettingsAprilFoolsMode(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + return new ValueTuple(HttpStatusCode.OK, settings.aprilFoolsMode); } private ValueTuple PostV2SettingsAprilFoolsMode(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + if (!requestParameters.TryGetValue("value", out string? trueFalseStr) || + !bool.TryParse(trueFalseStr, out bool trueFalse)) + return new ValueTuple(HttpStatusCode.InternalServerError, "Errors parsing 'value'"); + settings.UpdateAprilFoolsMode(trueFalse); + return new ValueTuple(HttpStatusCode.OK, null); } private ValueTuple PostV2SettingsDownloadLocation(GroupCollection groups, Dictionary requestParameters) { - return new ValueTuple(HttpStatusCode.NotImplemented, "Not Implemented"); + if (!requestParameters.TryGetValue("location", out string? folderPath)) + return new ValueTuple(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.OK, null); + } + catch (FormatException) + { + return new ValueTuple(HttpStatusCode.InternalServerError, "Error Parsing Parameter 'moveFiles'"); + } } } \ No newline at end of file diff --git a/docs/API_Calls_v2.md b/docs/API_Calls_v2.md index 8e7c9e6..af542e2 100644 --- a/docs/API_Calls_v2.md +++ b/docs/API_Calls_v2.md @@ -436,6 +436,12 @@ Sets the User Agent. If left empty, User Agent is reset to default.
Returns + +| StatusCode | Meaning | +|------------|-------------------| +| 202 | UserAgent Reset | +| 201 | UserAgent Updated | +
### ![GET](https://img.shields.io/badge/GET-0f0) `/v2/Settings/RateLimit/Types` @@ -445,7 +451,7 @@ Returns the configurable Rate-Limits.
Returns - List of Rate-Limit-Names. + Key-Value-Pairs of Values and RateLimit-Names.
### ![GET](https://img.shields.io/badge/GET-0f0) `/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
Returns - | StatusCode | Meaning | - |------------|--------------------------------| - | 404 | Rate-Limit-Name does not exist | + | StatusCode | Meaning | + |------------|------------------------------------------------| + | 500 | Error parsing RequestType or RequestsPerMinute |
### ![GET](https://img.shields.io/badge/GET-0f0) `/v2/Settings/RateLimit/` @@ -496,6 +502,10 @@ Returns the current Rate-Limit for the Request-Type. Returns Integer with Requests per Minute. + +| StatusCode | Meaning | +|------------|-----------------------------------------------| +| 404 | Error parsing RequestType | ### ![POST](https://img.shields.io/badge/POST-00f) `/v2/Settings/RateLimit/` @@ -547,7 +557,6 @@ Enables/Disables April-Fools-Mode. | StatusCode | Meaning | |------------|--------------------------------| - | 404 | Rate-Limit-Name does not exist | | 500 | Parsing Error | @@ -568,10 +577,11 @@ Updates the default Download-Location. Returns - | StatusCode | Meaning | - |------------|--------------------------| - | 200 | Successfully changed | - | 500 | Files could not be moved | + | StatusCode | Meaning | + |------------|---------------------------------| + | 200 | Successfully changed | + | 404 | Parameter 'location' is missing | + | 500 | Parsing Error | ## Library Connectors [^top](#top)