Skip to content

Commit

Permalink
add api support for additional channels
Browse files Browse the repository at this point in the history
  • Loading branch information
memo33 committed Oct 18, 2024
1 parent 26a8053 commit 14398d4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
26 changes: 26 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ POST /plugins.remove?profile=id ["<pkg1>", "<pkg2>", …]
GET /variants.list?profile=id
POST /variants.reset?profile=id ["<label1>", "<label2>", …]
GET /channels.list?profile=id
POST /channels.set?profile=id ["<url1>", "<url2>", …]
GET /update?profile=id (websocket)
GET /server.status
Expand Down Expand Up @@ -221,6 +224,29 @@ Example:
curl -X POST -d '["nightmode"]' http://localhost:51515/variants.reset?profile=<id>
```

## channels.list

Get the ordered list of configured channel URLs.

Synopsis: `GET /channels.list?profile=id`

Returns: `["<url1>", "<url2>", …]`

## channels.set

Overwrite the list of channel URLs. If empty, the default channel is added instead.

Synopsis: `POST /channels.set?profile=id ["<url1>", "<url2">, …]`

Returns:
- 400 `/error/bad-request`
- 200 `{"$type": "/result", "ok": true}`

Example:
```sh
curl -X POST -d '["url"]' http://localhost:51515/channels.set?profile=<id>
```

## update

Opening a websocket at `/update` triggers the update process.
Expand Down
26 changes: 25 additions & 1 deletion src/main/scala/sc4pac/api/api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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] = {
Expand Down

0 comments on commit 14398d4

Please sign in to comment.