-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHTTPClient.lua
67 lines (59 loc) · 1.66 KB
/
HTTPClient.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
--[[
HTTPClient wrapper
@author ikubicki
]]
class 'HTTPClient'
function HTTPClient:new(options)
if not options then
options = {}
end
self.options = options
return self
end
function HTTPClient:get(url, success, error)
local client = net.HTTPClient({timeout=10000})
client:request(self:url(url), self:requestOptions(success, error, 'GET'))
end
function HTTPClient:post(url, data, success, error)
local client = net.HTTPClient({timeout=10000})
client:request(self:url(url), self:requestOptions(success, error, 'POST', data))
end
function HTTPClient:put(url, data, success, error)
local client = net.HTTPClient({timeout=10000})
client:request(self:url(url), self:requestOptions(success, error, 'PUT', data))
end
function HTTPClient:delete(url, success, error)
local client = net.HTTPClient({timeout=10000})
client:request(self:url(url), self:requestOptions(success, error, 'DELETE'))
end
function HTTPClient:url(url)
if (string.sub(url, 0, 4) == 'http') then
return url
end
if not self.options.baseUrl then
self.options.baseUrl = 'http://localhost'
end
return self.options.baseUrl .. tostring(url)
end
function HTTPClient:requestOptions(success, error, method, data)
if (error == nil) then
error = function (error)
QuickApp:error(json.encode(error))
end
end
if (method == nil) then
method = 'GET'
end
local options = {
checkCertificate = false,
method = method
}
if (data ~= nil) then
options.data = data
end
return {
options = options,
success = success,
error = error
}
end