mirror of
https://github.com/koreader/koreader
synced 2024-11-10 01:10:34 +00:00
request from async http client only accept one callback
and error should be checked in the callback
This commit is contained in:
parent
56159480f2
commit
d7faba5b5c
@ -22,7 +22,7 @@ function HTTPClient:removeHeader(header)
|
||||
self.headers[header] = nil
|
||||
end
|
||||
|
||||
function HTTPClient:request(request, response_callback, error_callback)
|
||||
function HTTPClient:request(request, response_callback)
|
||||
request.on_headers = function(headers)
|
||||
for header, value in pairs(self.headers) do
|
||||
headers[header] = value
|
||||
@ -36,15 +36,15 @@ function HTTPClient:request(request, response_callback, error_callback)
|
||||
UIManager.INPUT_TIMEOUT = self.INPUT_TIMEOUT
|
||||
self.input_timeouts = self.input_timeouts + 1
|
||||
local turbo = require("turbo")
|
||||
-- disable success and warning logs
|
||||
turbo.log.categories.success = false
|
||||
local res = coroutine.yield(
|
||||
turbo.async.HTTPClient():fetch(request.url, request))
|
||||
turbo.log.categories.warning = false
|
||||
local client = turbo.async.HTTPClient({verify_ca = "none"})
|
||||
local res = coroutine.yield(client:fetch(request.url, request))
|
||||
-- reset INPUT_TIMEOUT to nil when all HTTP requests are fullfilled.
|
||||
self.input_timeouts = self.input_timeouts - 1
|
||||
UIManager.INPUT_TIMEOUT = self.input_timeouts > 0 and self.INPUT_TIMEOUT or nil
|
||||
if res.error and error_callback then
|
||||
error_callback(res)
|
||||
elseif response_callback then
|
||||
if response_callback then
|
||||
response_callback(res)
|
||||
end
|
||||
end)
|
||||
|
@ -9,13 +9,9 @@ describe("HTTP client module", function()
|
||||
local function response_callback(res)
|
||||
requests = requests - 1
|
||||
if requests == 0 then UIManager:quit() end
|
||||
assert(not res.error, "error occurs")
|
||||
assert(res.body)
|
||||
end
|
||||
local function error_callback(res)
|
||||
requests = requests - 1
|
||||
if requests == 0 then UIManager:quit() end
|
||||
assert(false, "error occurs")
|
||||
end
|
||||
local async_client = HTTPClient:new()
|
||||
it("should get response from async GET request", function()
|
||||
UIManager:quit()
|
||||
@ -27,7 +23,7 @@ describe("HTTP client module", function()
|
||||
for _, url in ipairs(urls) do
|
||||
async_client:request({
|
||||
url = url,
|
||||
}, response_callback, error_callback)
|
||||
}, response_callback)
|
||||
end
|
||||
UIManager:runForever()
|
||||
end)
|
||||
|
Loading…
Reference in New Issue
Block a user