2
0
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:
chrox 2015-03-07 11:19:39 +08:00
parent 56159480f2
commit d7faba5b5c
2 changed files with 8 additions and 12 deletions

View File

@ -22,7 +22,7 @@ function HTTPClient:removeHeader(header)
self.headers[header] = nil self.headers[header] = nil
end end
function HTTPClient:request(request, response_callback, error_callback) function HTTPClient:request(request, response_callback)
request.on_headers = function(headers) request.on_headers = function(headers)
for header, value in pairs(self.headers) do for header, value in pairs(self.headers) do
headers[header] = value headers[header] = value
@ -36,15 +36,15 @@ function HTTPClient:request(request, response_callback, error_callback)
UIManager.INPUT_TIMEOUT = self.INPUT_TIMEOUT UIManager.INPUT_TIMEOUT = self.INPUT_TIMEOUT
self.input_timeouts = self.input_timeouts + 1 self.input_timeouts = self.input_timeouts + 1
local turbo = require("turbo") local turbo = require("turbo")
-- disable success and warning logs
turbo.log.categories.success = false turbo.log.categories.success = false
local res = coroutine.yield( turbo.log.categories.warning = false
turbo.async.HTTPClient():fetch(request.url, request)) 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. -- reset INPUT_TIMEOUT to nil when all HTTP requests are fullfilled.
self.input_timeouts = self.input_timeouts - 1 self.input_timeouts = self.input_timeouts - 1
UIManager.INPUT_TIMEOUT = self.input_timeouts > 0 and self.INPUT_TIMEOUT or nil UIManager.INPUT_TIMEOUT = self.input_timeouts > 0 and self.INPUT_TIMEOUT or nil
if res.error and error_callback then if response_callback then
error_callback(res)
elseif response_callback then
response_callback(res) response_callback(res)
end end
end) end)

View File

@ -9,13 +9,9 @@ describe("HTTP client module", function()
local function response_callback(res) local function response_callback(res)
requests = requests - 1 requests = requests - 1
if requests == 0 then UIManager:quit() end if requests == 0 then UIManager:quit() end
assert(not res.error, "error occurs")
assert(res.body) assert(res.body)
end 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() local async_client = HTTPClient:new()
it("should get response from async GET request", function() it("should get response from async GET request", function()
UIManager:quit() UIManager:quit()
@ -27,7 +23,7 @@ describe("HTTP client module", function()
for _, url in ipairs(urls) do for _, url in ipairs(urls) do
async_client:request({ async_client:request({
url = url, url = url,
}, response_callback, error_callback) }, response_callback)
end end
UIManager:runForever() UIManager:runForever()
end) end)