local effil = require 'effil'
uriFormat = function(params)
local uri = ''
local prefix = ''
for k, v in pairs(params) do
uri = uri .. prefix .. tostring(k) .. '=' .. tostring(v)
prefix = '&'
end
return uri
end
do
local _thread_worker = function(method, url, args)
local requests = require 'requests'
local success, result = pcall(requests.request, method, url, args)
if success then
result.json, result.xml = nil, nil
return { true, result }
else
return { false, result }
end
end
makeRequest = function(method, url, args, successHandler, failHandler)
local request_thread_runner = effil.thread(_thread_worker)
lua_thread.create(function(method, url, args, successHandler, failHandler)
local request_thread = request_thread_runner(method, url, args)
local status, err
while not err and status ~= 'completed' do
status, err = request_thread:status()
wait(0) end
if err then
return failHandler(0, err)
else
if status ~= 'completed' then
return failHandler(1, status)
end
local retval = request_thread:get()
local request_success, request_retval = retval[1], retval[2]
if not request_success then
return failHandler(2, request_retval)
end
request_retval.json = function(self)
return decodeJson(self.text)
end
return successHandler(request_retval)
end
end, method, url, args, successHandler, failHandler)
end
end