2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/httpclient.lua
2015-04-28 11:06:38 +08:00

39 lines
1.2 KiB
Lua

local UIManager = require("ui/uimanager")
local DEBUG = require("dbg")
local HTTPClient = {
input_timeouts = 0,
}
function HTTPClient:new()
local o = {}
setmetatable(o, self)
self.__index = self
return o
end
function HTTPClient:request(request, response_callback)
request.connect_timeout = 10
request.request_timeout = 20
UIManager:initLooper()
UIManager.looper:add_callback(function()
-- avoid endless waiting for input
UIManager:setInputTimeout()
self.input_timeouts = self.input_timeouts + 1
local turbo = require("turbo")
-- disable success and warning logs
turbo.log.categories.success = false
turbo.log.categories.warning = false
local client = turbo.async.HTTPClient({verify_ca = false})
local res = coroutine.yield(client:fetch(request.url, request))
self.input_timeouts = self.input_timeouts - 1
-- reset INPUT_TIMEOUT to nil when all HTTP requests are fullfilled.
if self.input_timeouts == 0 then UIManager:resetInputTimeout() end
if response_callback then
response_callback(res)
end
end)
end
return HTTPClient