From 953403f952a48921b6195de75567d23c76df99c4 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Thu, 1 Dec 2022 12:00:48 -0500 Subject: [PATCH] Command api refactor --- .../WebServer/Middleware/CommandsApiRouter.bs | 36 +++++++++---------- src/www/index.html | 24 +++++++------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/components/WebServer/Middleware/CommandsApiRouter.bs b/src/components/WebServer/Middleware/CommandsApiRouter.bs index b2a2e519..6ba380b1 100644 --- a/src/components/WebServer/Middleware/CommandsApiRouter.bs +++ b/src/components/WebServer/Middleware/CommandsApiRouter.bs @@ -5,35 +5,31 @@ namespace Http function new() super() - m.Get("/api/command/logout", function(context as object) as boolean - response = context.response - server = context.server - - if Invidious.Logout() - server.task.top.login = false - end if - - response.Default(204, "OK") - return true - end function) - - m.Get("/api/command/play", function(context as object) as boolean + m.Post("/api/command", function(context as object) as boolean request = context.request response = context.response server = context.server - if request.query["v"] = invalid - response.Default(400, "Missing arg 'v'") + payload = request.Json() + if payload = invalid + response.Default(400, "Invalid payload") return true end if - server.task.top.command = `play:${request.query["v"]}` - if request.query["redirect"] = "true" - ' 302 to prevent redirection to current host permanently - response.Redirect(`${InvidiousSettings.GetSelectedInstance()}/watch?v=${request.query["v"]}`, 302) - else + if payload.command = "logout" + if Invidious.Logout() + server.task.top.login = false + end if response.Default(204, "OK") + return true + else if payload.command = "play" + videoId = payload.videoId + server.task.top.command = `play:${videoId}` + response.Default(204, "OK") + return true end if + + response.Default(400, `Invalid command: ${payload.command}`) return true end function) end function diff --git a/src/www/index.html b/src/www/index.html index 48ade1c2..46096d65 100644 --- a/src/www/index.html +++ b/src/www/index.html @@ -82,7 +82,7 @@ } function Logout() { - fetch('/api/command/logout') + PostJson('/api/command', { command: "logout" }) .then(function (response) { window.location.reload(); }) @@ -100,7 +100,7 @@ alert("Please set video id") return } - fetch(`/api/command/play?v=${v}`) + PostJson('/api/command', { command: "play", videoId: v }) .catch(function (err) { alert(err) }); @@ -210,14 +210,7 @@ ${JSON.stringify(window.appState.device, null, 2)} .filter(a => !!a) .map(a => a.innerText); - fetch('/invidious/instances', - { - headers: { - 'Content-Type': 'application/json' - }, - method: "POST", - body: JSON.stringify(instances) - }) + PostJson('/invidious/instances', instances) .then(function (response) { alert("Instances updated. Restart Playlet to reflect changes.") window.location.reload(); @@ -227,6 +220,17 @@ ${JSON.stringify(window.appState.device, null, 2)} }); } + function PostJson(url, payload) { + return fetch(url, + { + headers: { + 'Content-Type': 'application/json' + }, + method: "POST", + body: JSON.stringify(payload) + }) + } + function Initialize() { fetch("/api/state") .then(function (response) {