From 0710ed215d2f5b887900d06aeaf7bff95561cee0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 30 Apr 2017 10:39:38 +0530 Subject: [PATCH] Implement versioning for the cdb API --- src/calibre/db/cli/main.py | 2 +- src/calibre/srv/cdb.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/calibre/db/cli/main.py b/src/calibre/db/cli/main.py index b0ad31242f..e5704e3c91 100644 --- a/src/calibre/db/cli/main.py +++ b/src/calibre/db/cli/main.py @@ -193,7 +193,7 @@ class DBCtx(object): def remote_run(self, name, m, *args): from mechanize import HTTPError, Request 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: url += '?' + urlencode({'library_id':self.library_id}) rq = Request(url, data=msgpack_dumps(args), diff --git a/src/calibre/srv/cdb.py b/src/calibre/srv/cdb.py index 7a000ee81a..aafacc7e87 100644 --- a/src/calibre/srv/cdb.py +++ b/src/calibre/srv/cdb.py @@ -14,14 +14,18 @@ from calibre.utils.serialize import MSGPACK_MIME, msgpack_loads, json_loads receive_data_methods = {'GET', 'POST'} -@endpoint('/cdb/run/{which}', postprocess=msgpack_or_json, methods=receive_data_methods) -def cdb_run(ctx, rd, which): +@endpoint('/cdb/run/{which}/{version=0}', postprocess=msgpack_or_json, methods=receive_data_methods) +def cdb_run(ctx, rd, which, version): try: m = module_for_cmd(which) except ImportError: raise HTTPNotFound('No module named: {}'.format(which)) if not getattr(m, 'readonly', False): 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() ct = rd.inheaders.get('Content-Type', all=True) try: