local effil = require('effil')
local callback = {
ok = function(response)
if response.status_code ~= 200 then
return sampAddChatMessage('error, code '..response.status_code, -1)
end
local status, data = pcall(decodeJson, response.text)
if not status or not data then
return sampAddChatMessage('error decoding json!', -1)
end
local text = {}
for field, value in pairs(data) do
table.insert(text, field..'\t'..value)
end
sampShowDialog(891, 'IP INFO', table.concat(text, '\n'), 'X', nil, 4)
end,
error = function(response)
sampAddChatMessage('error: '..response, -1)
end
}
function main()
while not isSampAvailable() do wait(0) end
sampRegisterChatCommand('ipinfo', function()
asyncHttpRequest('GET', 'http://ip-api.com/json/', nil, callback.ok, callback.error)
end)
wait(-1)
end
function asyncHttpRequest(method, url, args, resolve, reject)
local request_thread = effil.thread(function (method, url, args)
local requests = require 'requests'
local result, response = pcall(requests.request, method, url, args)
if result then
response.json, response.xml = nil, nil
return true, response
else
return false, response
end
end)(method, url, args)
if not resolve then resolve = function() end end
if not reject then reject = function() end end
lua_thread.create(function()
local runner = request_thread
while true do
local status, err = runner:status()
if not err then
if status == 'completed' then
local result, response = runner:get()
if result then
resolve(response)
else
reject(response)
end
return
elseif status == 'canceled' then
return reject(status)
end
else
return reject(err)
end
wait(0)
end
end)
end