-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhttp_client.lua
55 lines (48 loc) · 1.9 KB
/
http_client.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
-- author: https://github.com/tjurczyk/arkadia
HttpClient = HttpClient or {
handlers = {}
}
local successHandlerCallback = function(url, callback, handlers)
return function(_, rurl, response)
if rurl ~= url then
return true
end
killAnonymousEventHandler(handlers.errorHandler)
if callback then
callback(yajl.to_value(response))
end
end
end
local errorHandlerCallback = function(url, errorCallback, handlers)
return function(_, response, rurl)
if rurl ~= url then
return true
end
local msg = string.format("Error while calling %s - %s", url, response)
killAnonymousEventHandler(handlers.successHandler)
if errorCallback then
errorCallback(response, msg)
end
end
end
function HttpClient:registerHandlers(method, url, callback, errorCallback)
local handlers = {}
handlers.successHandler = registerAnonymousEventHandler(string.format("sys%sHttpDone", method), successHandlerCallback(url, callback, handlers), true)
handlers.errorHandler = registerAnonymousEventHandler(string.format("sys%sHttpError", method), errorHandlerCallback(url, errorCallback, handlers), true)
end
function HttpClient:post(url, payload, headers, callback, errorCallback)
self:registerHandlers("Post", url, callback, errorCallback)
postHTTP(yajl.to_string(payload), url, headers)
end
function HttpClient:put(url, payload, headers, callback, errorCallback)
self:registerHandlers("Put", url, callback, errorCallback)
putHTTP(yajl.to_string(payload), url, headers)
end
function HttpClient:get(url, headers, callback, errorCallback)
self:registerHandlers("Get", url, callback, errorCallback)
getHTTP(url, headers)
end
function HttpClient:delete(url, headers, callback, errorCallback)
self:registerHandlers("Delete", url, callback, errorCallback)
deleteHTTP(url, headers)
end