Command api refactor

This commit is contained in:
Brahim Hadriche committed 2022-12-01 12:00:48 -05:00
1 parent 0409e61b5f
commit 953403f952
2 files changed
+30 -30

No files matched your search

@@ -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
+14 -10
View File
@@ -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) {