-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREST.lua
78 lines (72 loc) · 2.15 KB
/
REST.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- require "module-loader"
METHODS =
{
HEAD = "HEAD";
GET = "GET";
PUT = "PUT";
DELETE = "DELETE";
PATCH= "PATCH";
POST= "POST";
}
local OS = love.system.getOS()
REST = {}
--Providing autocomplete
REST.head = function(url, requestHeader, methodOrOnLoad, onLoad)end
REST.get = function(url, requestHeader, onLoad)end
REST.post = function(url, requestHeader, data, onLoad)end
REST.put = function(url, requestHeader, data, onLoad)end
REST.patch = function(url, requestHeader, data, onLoad)end
REST.delete = function(url, requestHeader, data, onLoad)end
REST.retrieve = function(dt)end
--End autocomplete
--This was meant to make it compatible out-of-the-box with the rest of the world, not only Lua, so the decision was to make
--Arrays start by index 0
function _TABLE_TO_JSON(data, isRecursive)
if(type(data) == "table") then
local nData = '{'
if(not isRecursive) then
nData = "\'"..nData
end
local isFirst = true
for key, value in pairs(data) do
local k = key
if(tonumber(k)) then k = tonumber(k) - 1; end
if(isFirst) then
isFirst = false
else
nData = nData..", "
end
if(type(value) == "table") then
nData = nData..'"'..k..'"'..' : '.._TABLE_TO_JSON(value, true)
elseif(tonumber(value) or type(value) == "boolean") then
nData = nData..'"'..k..'"'..' : '..tostring(value)
else
nData = nData..'"'..k..'"'..' : "'..value..'"'
end
end
nData = nData..'}'
if(not isRecursive) then
nData = nData.."\'"
end
data = nData
end
return data
end
if(OS == "Web") then
requireFromLib("js", "js")
REST = require "rest-lib.js-rest"
elseif(OS == "Windows") then
REST = require "rest-lib.win-rest"
else
REST = require "rest-lib.default-rest"
end
if(REST.start ~= nil) then
REST.start()
--Auto-destroy start for not causing any problem
REST.start = nil
end
local function UPDATE(dt)
return REST.retrieveFunction(dt)
end
REST.isDebug = true;
REST.retrieve = UPDATE;