Commit b11ef997 authored by Julien Cristau's avatar Julien Cristau
Browse files

Version the API

Lets us extend it later without breaking clients.
parent df054c34f122
......@@ -9,7 +9,7 @@ Sample usage
Users of this service must perform a HTTP POST request to its endpoint,
that is the base url of the CubicWeb application instance appended with
the "rqlio" url path.
the "rqlio/1.0" url path.
The posted data must use the application/json MIME type, and contain a list of
pairs of the form `(rql_string, rql_arguments)`, where:
......@@ -50,7 +50,7 @@ Python client example using python-requests::
{'u': '__r0', 'g': '__r1'})
]
resp = requests.post('https://myinstance.example.com/rqlio'),
resp = requests.post('https://myinstance.example.com/rqlio/1.0'),
data=json.dumps(args),
headers={'Content-Type': 'application/json'})
assert resp.status_code == 200
......
......@@ -43,7 +43,7 @@ class RqlIOTc(CubicWebServerTC):
('SET U in_group G WHERE U eid %(u)s, G eid %(g)s',
{'u': '__r0', 'g': '__r1'})]
queries = json.dumps(args)
a = requests.post(req.build_url('rqlio'), data=queries, headers={'Content-Type': 'application/json'})
a = requests.post(req.build_url('rqlio/1.0'), data=queries, headers={'Content-Type': 'application/json'})
self.assertEqual(a.status_code, 200)
# anonymous never get anything because we don't even
# try to execute stuff
......@@ -54,7 +54,7 @@ class RqlIOTc(CubicWebServerTC):
r = requests.Session()
a = r.get(req.base_url() + "?__login=toto&__password=toto")
a = r.post(req.build_url('rqlio'), data=queries, headers={'Content-Type': 'application/json'})
a = r.post(req.build_url('rqlio/1.0'), data=queries, headers={'Content-Type': 'application/json'})
# remote call fails because we're not allowed to SET U in_group G
self.assertEqual({'reason': 'You are not allowed to perform add operation '
'on relation CWUser in_group CWGroup'},
......@@ -64,7 +64,7 @@ class RqlIOTc(CubicWebServerTC):
# now, as an admin
r = requests.Session()
a = r.get(req.base_url() + "?__login=admin&__password=gingkow")
a = r.post(req.build_url('rqlio'), data=queries, headers={'Content-Type': 'application/json'})
a = r.post(req.build_url('rqlio/1.0'), data=queries, headers={'Content-Type': 'application/json'})
self.assertEqual('pachyderms',
self.execute('Any N WHERE U in_group G, U login "Babar", '
......
......@@ -19,12 +19,12 @@
import re
from cubicweb.predicates import ExpectedValuePredicate, match_http_method
from cubicweb.predicates import ExpectedValuePredicate, match_http_method, match_form_params
from cubicweb.uilib import exc_message
from cubicweb.utils import json, json_dumps
from cubicweb.web import RemoteCallFailed, DirectResponse
from cubicweb.web.controller import Controller
from cubicweb.web.views.urlrewrite import rgx_action, SchemaBasedRewriter
ARGRE = re.compile('__r\d+$')
......@@ -73,11 +73,16 @@ class RqlIOController(Controller):
"""
__regid__ = 'rqlio'
__select__ = match_http_method('POST') & match_request_content_type('application/json')
__select__ = (match_http_method('POST') & match_request_content_type('application/json') &
match_form_params('version'))
def publish(self, rset=None):
self._cw.ajax_request = True
self._cw.set_content_type('application/json')
if self._cw.form['version'] != '1.0':
raise RemoteCallFailed('unknown rqlio version %r', self._cw.form['version'])
try:
args = json.load(self._cw.content)
except ValueError as exc:
......@@ -117,3 +122,8 @@ class RqlIOController(Controller):
rewrite_args(args, output)
output.append(self._cw.execute(rql, args))
return output
class RQLIORewriter(SchemaBasedRewriter):
rules = [
(re.compile('/rqlio/(?P<version>.+)$'), rgx_action(controller='rqlio', formgroups=('version',)))
]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment