From 8416afe79c4ad2e6bc57449e049a418c11fba6f8 Mon Sep 17 00:00:00 2001 From: Alessandro <4512372+Ale32bit@users.noreply.github.com> Date: Fri, 8 Feb 2019 21:28:58 +0100 Subject: [PATCH] WIP HTTP Module --- modules/http.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 modules/http.lua diff --git a/modules/http.lua b/modules/http.lua new file mode 100644 index 0000000..bd4150b --- /dev/null +++ b/modules/http.lua @@ -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, +} +