local http = require("requests")
local hook = {
MAX_RESPONSE_OUTLEN = 255,
downloadUrlToFile = downloadUrlToFile,
request = http.request
}
function downloadUrlToFile(url, filename, callback)
local file
print("downloadUrlToFile", url, filename, callback or "no callback")
return hook.downloadUrlToFile(url, filename, function(id, state)
print("downloadUrlToFile", "(callback)", url, filename, id, state)
if state == 6 then
repeat
-- устраняем баг, когда иногда не открывает файл с первого раза
file = io.open(filename, "rb")
until file
local response = file:read("*a")
file:close()
print("downloadUrlToFile", "(response)", url, filename)
print("downloadUrlToFile", "(response data)", response:len() > hook.MAX_RESPONSE_OUTLEN and "(trunc)" or "", response:sub(1, hook.MAX_RESPONSE_OUTLEN))
end
callback(id, state)
end)
end
function http.request(method, url, args)
print("request", method, url, args or "no args")
local response = hook.request(method, url, args)
print("request", "(response)", method, url, args or "no args")
print("request", "(response text)", response.text:len() > hook.MAX_RESPONSE_OUTLEN and "(trunc)" or "", response.text:sub(1, hook.MAX_RESPONSE_OUTLEN))
return response
end