Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F175002967
flua REST API client for PDU
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
rmoeller
Wed, Oct 7, 1:43 PM
2026-10-07 13:43:19 (UTC+0)
Size
2 KB
Referenced Files
None
Subscribers
None
flua REST API client for PDU
View Options
#!/usr/libexec/flua
local fetch <const> = require('fetch')
local ucl <const> = require('ucl')
local function encode(s)
local function hex(char)
return string.format('%%%02X', string.byte(char))
end
s = string.gsub(s, '\n', '\r\n')
s = string.gsub(s, '([^%w %-%_%.%~])', hex)
s = string.gsub(s, ' ', '+')
return s
end
local function http_query_string(params)
local query_parts <const> = {}
for name, value in pairs(params) do
if value == true then
table.insert(query_parts, encode(name))
else
table.insert(query_parts, table.concat({
encode(name), encode(tostring(value))
}, '='))
end
end
return table.concat(query_parts, '&')
end
local function field(name, value)
return {name=name, value=value}
end
function PduApiClient:request(method, path, body, query)
local url = self.baseUrl .. path
if query ~= nil then
url = url .. '?' .. http_query_string(query)
end
local headers <const> = {field('Accept', 'application/json')}
local content
if body ~= nil then
table.insert(headers, field('Content-Type', 'application/json'))
content = ucl.to_json(body, true)
end
if self.token ~= nil then
table.insert(headers, field('authorization', 'bearer ' .. self.token))
end
local f <close> = assert(fetch.xrequest(url, method, self.flags, headers,
nil, nil, nil, content))
local parser <const> = ucl.parser()
parser:parse_string(f:read('*a'))
return parser:get_object()
end
function PduApiClient:login(username, password)
local response <const> = self:request('POST', '/sys/login', {
userLogin = {
usr = username,
pwd = password,
}
})
if response.errCode ~= 0 then
error('Login failed: ' .. ucl.to_json(response))
end
self.token = response.token
if self.token == nil or self.token == '' then
error('Login failed (invalid token): ' .. ucl.to_json(response))
end
return response
end
local client <const> = assert(PduApiClient{baseUrl='https://192.168.1.118/api'})
client:login('admin', io.read('*a'))
--print(ucl.to_json(client:request('GET', '/sys/top_banner')))
print(ucl.to_json(client:request('PUT', '/overview/outlet/enable', {
outletEnConfs = {
outlet_id = 6,
outlet_enable = true,
}
})))
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
40423331
Default Alt Text
flua REST API client for PDU (2 KB)
Attached To
Mode
P721 flua REST API client for PDU
Attached
Detach File
Event Timeline
Log In to Comment