A Python library to easily build CubicWeb clients
Project description
Summary
A Python library to easily build CubicWeb clients:
execute RQL queries remotely (using rqlcontroller),
access instances that requires authentication (using signedrequest).
Requirements
client side:
requests (>= 2.0)
server side:
CubicWeb (>= 3.18.3) with the cubes rqlcontroller and signedrequest
Using signed requests
Once the cube signedrequest is added, in the WebUI:
View a CWUser and click the action add an AuthToken
Give an identifier to the token and make it enabled
Use the token identifier and the token in your source code
Using Kerberos
Just make sure Python-Kerberos and Requests-Kerberos are installed.
Examples
Simple read only query:
from cwclientlib import cwproxy
client = cwproxy.CWProxy('http://www.cubicweb.org/')
query = 'Any X WHERE X is Ticket, X concerns P, P name "cwclientlib"'
resp = client.rql(query)
data = resp.json()
Creating an entity, authenticating with signedrequest:
from cwclientlib import cwproxy
auth = cwproxy.SignedRequestAuth('my token', '6ed44d82172211e49d9777269ec78bae')
client = cwproxy.CWProxy('https://www.cubicweb.org/', auth)
queries = [('INSERT CWUser U: U login %(l)s, U upassword %(p)s',
{'l': 'Babar', 'p': 'cubicweb rulez & 42'}), ]
resp = client.rqlio(queries)
data = resp.json()
Creating a file entity, authenticating with signedrequest:
from io import BytesIO
from cwclientlib import cwproxy
auth = cwproxy.SignedRequestAuth('my token', '6ed44d82172211e49d9777269ec78bae')
client = cwproxy.CWProxy('https://www.cubicweb.org/', auth)
queries = [('INSERT File F: F data %(content)s, F data_name %(fname)s',
{'content': BytesIO('some binary data'), 'fname': 'toto.bin'})]
resp = client.rqlio(queries)
data = resp.json()
Using builders helpers, authenticating with kerberos:
from cwclientlib import cwproxy, builders
from requests_kerberos import HTTPKerberosAuth, OPTIONAL
auth = HTTPKerberosAuth(mutual_authentication=OPTIONAL)
client = cwproxy.CWProxy('https://www.cubicweb.org/', auth)
queries = [builders.create_entity('CWUser', login='Babar', password='secret'),
builders.build_trinfo('__r0', 'disable', 'not yet activated'),
]
resp = client.rqlio(queries)
data = resp.json()