local _thread_worker = function(url)
local requests = require 'requests'
local success, result = pcall(requests.request, 'GET', url, nil)
if success then
result.json, result.xml = nil, nil
return { true, result }
else
return { false, result }
end
end
makeRequest = function(url, params, successHandler, failHandler)
-- format params
if params and type(params) == 'table' and next(params) then
url = url .. '?'
local temp = ''
for k, v in pairs(params) do
url = url .. temp .. tostring(k) .. '=' .. tostring(v)
temp = '&'
end
end
local request_thread_runner = effil.thread(_thread_worker)
lua_thread.create(function(url, successHandler, failHandler)
local request_thread = request_thread_runner(url)
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
return successHandler(request_retval)
end
end, url, successHandler, failHandler)
end