From 14398d4667c2bc1f8143a6960c4ffffd699a4783 Mon Sep 17 00:00:00 2001 From: memo Date: Fri, 18 Oct 2024 17:49:21 +0200 Subject: [PATCH] add api support for additional channels --- api.md | 26 ++++++++++++++++++++++++++ src/main/scala/sc4pac/api/api.scala | 26 +++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/api.md b/api.md index 542bf03..a177054 100644 --- a/api.md +++ b/api.md @@ -20,6 +20,9 @@ POST /plugins.remove?profile=id ["", "", …] GET /variants.list?profile=id POST /variants.reset?profile=id ["", "", …] +GET /channels.list?profile=id +POST /channels.set?profile=id ["", "", …] + GET /update?profile=id (websocket) GET /server.status @@ -221,6 +224,29 @@ Example: curl -X POST -d '["nightmode"]' http://localhost:51515/variants.reset?profile= ``` +## channels.list + +Get the ordered list of configured channel URLs. + +Synopsis: `GET /channels.list?profile=id` + +Returns: `["", "", …]` + +## channels.set + +Overwrite the list of channel URLs. If empty, the default channel is added instead. + +Synopsis: `POST /channels.set?profile=id ["", ", …]` + +Returns: +- 400 `/error/bad-request` +- 200 `{"$type": "/result", "ok": true}` + +Example: +```sh +curl -X POST -d '["url"]' http://localhost:51515/channels.set?profile= +``` + ## update Opening a websocket at `/update` triggers the update process. diff --git a/src/main/scala/sc4pac/api/api.scala b/src/main/scala/sc4pac/api/api.scala index 5782ad2..4d35fd4 100644 --- a/src/main/scala/sc4pac/api/api.scala +++ b/src/main/scala/sc4pac/api/api.scala @@ -8,7 +8,7 @@ import zio.{ZIO, IO, URIO} import upickle.default as UP import sc4pac.JsonData as JD -import JD.bareModuleRw +import JD.{bareModuleRw, uriRw} class Api(options: sc4pac.cli.Commands.ServerOptions) { @@ -306,6 +306,30 @@ class Api(options: sc4pac.cli.Commands.ServerOptions) { } }, + // 200, 409 + Method.GET / "channels.list" -> handler { + wrapHttpEndpoint { + for { + pluginsData <- readPluginsOr409 + } yield jsonResponse(pluginsData.config.channels) + } + }, + + // 200, 400, 409 + Method.POST / "channels.set" -> handler { (req: Request) => + wrapHttpEndpoint { + for { + urls <- parseOr400[Seq[java.net.URI]](req.body, ErrorMessage.BadRequest("Malformed channel URLs.", "Pass channels as an array of strings.")) + pluginsData <- readPluginsOr409 + pluginsData2 = pluginsData.copy(config = pluginsData.config.copy(channels = + if (urls.nonEmpty) urls.distinct else Constants.defaultChannelUrls + )) + path <- JD.Plugins.pathURIO + _ <- JsonIo.write(path, pluginsData2, None)(ZIO.succeed(())) + } yield jsonOk + } + }, + ) def routes: Routes[ProfilesDir, Nothing] = {