WIP HTTP Module

This commit is contained in:
Alessandro 2019-02-08 21:28:58 +01:00 committed by GitHub
parent 30c521abde
commit 8416afe79c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 0 deletions

28
modules/http.lua Normal file
View File

@ -0,0 +1,28 @@
if not node then
error("Node.lua not found", 2)
end
function get(url)
return node.promise(function(resolve, reject)
local ok, err = http.request(url)
if not ok then
return reject(err)
end
local ev
repeat
ev = {os.pullEvent()}
until ev[1] == "http_success" or ev[1] == "http_failure" and ev[2] == url
if ev[1] == "http_success" then
return resolve(ev[3])
elseif ev[1] == "http_failure" then
return reject(ev[3])
end
end)
end
return {
get = get,
}