Files
Emmanuele Sassi 04c7b07d0e Primo commit
2021-09-16 16:24:59 +02:00

63 lines
2.0 KiB
Lua

--
-- EEEEEEEEEE GGGGGG TTTTTTTTTTTTTT
-- EEEEEEEEEE GGGGGGGGGG TTTTTTTTTTTTTT
-- EEEE GGGG GGGG TTTT
-- EEEE GGGG TTTT
-- EEEEEEE GGGG GGGGGGG TTTT
-- EEEEEEE GGGG GGGGGGG TTTT
-- EEEE GGGG GGGG TTTT
-- EEEE GGGG GGGG TTTT
-- EEEEEEEEEE GGGGGGGGGG TTTT
-- EEEEEEEEEE GGGGGG TTTT
--
-- by EgalTech s.r.l.
-- Libreria delle funzioni REST by EgalTech s.r.l. 2020/08/13
-- Tabella per definizione modulo
local RESTLib = {}
-- Include
require( 'EgtBase')
ltn12 = require( 'ltn12')
http = require( 'socket.http')
JSON = require( 'JSON')
-- funzione get di chiamata REST
function RESTLib.get(path)
local resp = {}
-- Get info from website
--answ, status, body = http.request{url = path, content_type = 'application/json', sink = ltn12.sink.file(io.stdout)}
local body, code, headers = http.request{url = path, content_type = 'application/json', sink = ltn12.sink.table(resp)}
-- Verify errors
if code == 200 then
-- concateno le stringhe ricevute
local TempData = table.concat(resp)
-- e le decodifico da JSON a tabella lua
return JSON:decode(TempData) -- decode example
else
print("Error: ".. (code or '') )
return {}
end
end
--
-- funzione set di chiamata REST
function RESTLib.set(path, value)
local resp = JSON:encode(value)
-- Get info from website
local body, code, headers = http.request{url = path, content_type = 'application/json', source = ltn12.source.string(resp), method = "POST",
headers = {["Content-Type"] = "application/json",
["content-length"] = string.len(resp)}}
-- Verify errors
if code == 200 then
return true
else
print("Error: ".. (code or '') )
return false
end
end
--
---------------------------------------------------------------------
return RESTLib