local lanes = require("lanes").configure()
function async_http_request(method, url, args, resolve, reject)
if not _G["lanes.async_http"] then
local linda = lanes.linda()
local lane_gen = lanes.gen("*", {package = {path = package.path, cpath = package.cpath}}, function()
local requests = require("requests")
while true do
local key, val = linda:receive(50 / 1000, "request")
if key == "request" then
local ok, result = pcall(requests.request, val.method, val.url, val.args)
if ok then
result.json, result.xml = nil, nil
linda:send("response", result)
else linda:send("error", result) end
end
end
end)
_G["lanes.async_http"] = {lane = lane_gen(), linda = linda}
end
local lanes_http = _G["lanes.async_http"]
lanes_http.linda:send("request", {method = method, url = url, args = args})
lua_thread.create(function(linda)
while true do
local key, val = linda:receive(0, "response", "error")
if key == "response" then return resolve(val)
elseif key == "error" then return reject(val) end
wait(0)
end
end, lanes_http.linda)
end
async_http_request("POST", "https://test.police-assistant.ru", {params = {name = "test"}}, function(response) print(response.text) end, function(err) print(err) end)