Implement versioning for the cdb API

This commit is contained in:
Kovid Goyal 2017-04-30 10:39:38 +05:30
parent 1f1fe6b024
commit 0710ed215d
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 7 additions and 3 deletions

View File

@ -193,7 +193,7 @@ class DBCtx(object):
def remote_run(self, name, m, *args): def remote_run(self, name, m, *args):
from mechanize import HTTPError, Request from mechanize import HTTPError, Request
from calibre.utils.serialize import msgpack_loads, msgpack_dumps from calibre.utils.serialize import msgpack_loads, msgpack_dumps
url = self.url + '/cdb/run/' + name url = self.url + '/cdb/run/{}/{}'.format(name, getattr(m, 'version', 0))
if self.library_id: if self.library_id:
url += '?' + urlencode({'library_id':self.library_id}) url += '?' + urlencode({'library_id':self.library_id})
rq = Request(url, data=msgpack_dumps(args), rq = Request(url, data=msgpack_dumps(args),

View File

@ -14,14 +14,18 @@ from calibre.utils.serialize import MSGPACK_MIME, msgpack_loads, json_loads
receive_data_methods = {'GET', 'POST'} receive_data_methods = {'GET', 'POST'}
@endpoint('/cdb/run/{which}', postprocess=msgpack_or_json, methods=receive_data_methods) @endpoint('/cdb/run/{which}/{version=0}', postprocess=msgpack_or_json, methods=receive_data_methods)
def cdb_run(ctx, rd, which): def cdb_run(ctx, rd, which, version):
try: try:
m = module_for_cmd(which) m = module_for_cmd(which)
except ImportError: except ImportError:
raise HTTPNotFound('No module named: {}'.format(which)) raise HTTPNotFound('No module named: {}'.format(which))
if not getattr(m, 'readonly', False): if not getattr(m, 'readonly', False):
ctx.check_for_write_access(rd) ctx.check_for_write_access(rd)
if getattr(m, 'version', 0) != int(version):
raise HTTPNotFound(('The module {} is not available in version: {}.'
'Make sure the version of calibre used for the'
' server and calibredb match').format(which, version))
raw = rd.read() raw = rd.read()
ct = rd.inheaders.get('Content-Type', all=True) ct = rd.inheaders.get('Content-Type', all=True)
try: try: