Skip to content

Commit

Permalink
Add extension actions
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Feb 1, 2025
1 parent d7122c0 commit e26fe6a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lha/restEngine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ local REST_EXTENSIONS = {
extension:publishEvent('poll')
end
end,
action = {
['{index}(extension, index, requestJson)?method=POST'] = function(exchange, extension, index, arguments)
local actions = extension:getManifest('actions')
index = math.tointeger(index)
local action = index and actions and actions[index]
if action and action.method then
local method = extension[action.method]
if type(method) ~= 'function' then
HttpExchange.internalServerError(exchange, 'The action method is not available')
elseif extension:isActive() ~= action.active then
HttpExchange.badRequest(exchange, 'The extension active state does not match')
elseif (action.arguments and #action.arguments or 0) == #arguments then
HttpExchange.badRequest(exchange, 'The action arguments are invalid')
else
return method(extension, arguments)
end
else
HttpExchange.notFound(exchange)
end
return false
end
},
['test(extension)?method=POST'] = function(exchange, extension)
extension:publishEvent('test')
end,
Expand Down Expand Up @@ -366,7 +388,7 @@ local REST_THINGS = {
return false
end
end,
['{thingId}'] = {
['{thingId}'] = { -- TODO review routing
[''] = function(exchange)
local engine = exchange:getAttribute('engine')
local thingId = exchange:getAttribute('thingId')
Expand Down
32 changes: 32 additions & 0 deletions lha/schema-extension.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
{
"additionalProperties": false,
"properties": {
"actions": {
"title": "The extension actions",
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"title": "The name of the action",
"type": "string",
"required": true
},
"description": {
"title": "The description of the action",
"type": "string"
},
"method": {
"title": "The name of the extension method to trigger the action",
"type": "string",
"required": true
},
"arguments": {
"title": "The list of schema defining the arguments to pass to the method",
"type": "array"
},
"active": {
"default": true,
"title": "Indicates that the action is available when the extension is enabled",
"type": "boolean"
}
}
}
},
"config": {
"title": "The extension default configuration",
"type": "object"
Expand Down

0 comments on commit e26fe6a

Please sign in to comment.