2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/plugins/kosync.koplugin/KOSyncClient.lua
contrun 57188311da
kosync: send progress as a string to the server (#8758)
Currently the progress sent to the server can be either a string or an int (depending on whetther the document has pages).

The following are both payload sent from koreader to the server.

```
{"percentage":0.005,"device":"device_name","device_id":"B78EA04ACC3A453DBA220D720C0BE102","document":"348e34463a44ba68659fc6fe814a6778","progress":3}
```
where document `348e34463a44ba68659fc6fe814a6778` is a pdf file.

```
{"percentage":1,"device":"device_name","device_id":"B78EA04ACC3A453DBA220D720C0BE102","document":"4eb484b229696cb39cd8fe5495aa1bbe","progress":"\/body\/DocFragment[30]\/body\/p\/img.0"}
```
where document `4eb484b229696cb39cd8fe5495aa1bbe` is an epub file.

This may add extra work to the backend server. A few commits were added to my personal fork of [kosyncsrv](https://github.com/yeeac/kosyncsrv) (a kosync backend server). kosyncsrv initially tries to decode progress as a string. It then failed on document with pages (in which, progress is just integer page number). I then change the field's type, only to [revert it later](8a642e31a0).

I believe it is more appropriate for us to fix the progress type to string.
2022-02-20 16:08:56 +01:00

166 lines
4.7 KiB
Lua

local UIManager = require("ui/uimanager")
local DEBUG = require("dbg")
local KOSyncClient = {
service_spec = nil,
custom_url = nil,
}
function KOSyncClient:new(o)
if o == nil then o = {} end
setmetatable(o, self)
self.__index = self
if o.init then o:init() end
return o
end
function KOSyncClient:init()
require("socket.http").TIMEOUT = 1
local Spore = require("Spore")
self.client = Spore.new_from_spec(self.service_spec, {
base_url = self.custom_url,
})
package.loaded['Spore.Middleware.GinClient'] = {}
require('Spore.Middleware.GinClient').call = function(_, req)
req.headers['accept'] = "application/vnd.koreader.v1+json"
end
package.loaded['Spore.Middleware.KOSyncAuth'] = {}
require('Spore.Middleware.KOSyncAuth').call = function(args, req)
req.headers['x-auth-user'] = args.username
req.headers['x-auth-key'] = args.userkey
end
package.loaded['Spore.Middleware.AsyncHTTP'] = {}
require('Spore.Middleware.AsyncHTTP').call = function(args, req)
-- disable async http if Turbo looper is missing
if not UIManager.looper then return end
req:finalize()
local result
require("httpclient"):new():request({
url = req.url,
method = req.method,
body = req.env.spore.payload,
on_headers = function(headers)
for header, value in pairs(req.headers) do
if type(header) == 'string' then
headers:add(header, value)
end
end
end,
}, function(res)
result = res
-- Turbo HTTP client uses code instead of status
-- change to status so that Spore can understand
result.status = res.code
coroutine.resume(args.thread)
end)
return coroutine.create(function() coroutine.yield(result) end)
end
end
function KOSyncClient:register(username, password)
self.client:reset_middlewares()
self.client:enable('Format.JSON')
self.client:enable("GinClient")
local ok, res = pcall(function()
return self.client:register({
username = username,
password = password,
})
end)
if ok then
return res.status == 201, res.body
else
DEBUG(ok, res)
return false, res.body
end
end
function KOSyncClient:authorize(username, password)
self.client:reset_middlewares()
self.client:enable('Format.JSON')
self.client:enable("GinClient")
self.client:enable("KOSyncAuth", {
username = username,
userkey = password,
})
local ok, res = pcall(function()
return self.client:authorize()
end)
if ok then
return res.status == 200, res.body
else
DEBUG("err:", res)
return false, res.body
end
end
function KOSyncClient:update_progress(
username,
password,
document,
progress,
percentage,
device,
device_id,
callback)
self.client:reset_middlewares()
self.client:enable('Format.JSON')
self.client:enable("GinClient")
self.client:enable("KOSyncAuth", {
username = username,
userkey = password,
})
local co = coroutine.create(function()
local ok, res = pcall(function()
return self.client:update_progress({
document = document,
progress = tostring(progress),
percentage = percentage,
device = device,
device_id = device_id,
})
end)
if ok then
callback(res.status == 200, res.body)
else
DEBUG("err:", res)
callback(false, res.body)
end
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager:setInputTimeout() end
end
function KOSyncClient:get_progress(
username,
password,
document,
callback)
self.client:reset_middlewares()
self.client:enable('Format.JSON')
self.client:enable("GinClient")
self.client:enable("KOSyncAuth", {
username = username,
userkey = password,
})
local co = coroutine.create(function()
local ok, res = pcall(function()
return self.client:get_progress({
document = document,
})
end)
if ok then
callback(res.status == 200, res.body)
else
DEBUG("err:", res)
callback(false, res.body)
end
end)
self.client:enable("AsyncHTTP", {thread = co})
coroutine.resume(co)
if UIManager.looper then UIManager:setInputTimeout() end
end
return KOSyncClient