Package: cwclientlib
SummaryA Python library to easily buildCubicWebclients:executeRQLqueries remotely (usingrqlcontroller),access instances that requires authentication (usingsignedrequest).It also provides a simple command line tool (cwrql) to execute simple requests.Requirementsclient side:requests(>= 2.0)server side:CubicWeb (>= 3.18.3) with the cubesrqlcontrollerandsignedrequestConfigurationcwclientlibimplements acwproxy_for(instance)function that
will build aCWProxyfor the given instance, reading
authentication credentials from a configuration file (can be a ini
file, json or yaml). The default configuration file name is~/.config/cwclientlibrc(using the ini file format), but this can be
changed using theCWCLCONFenvironment variable. For example:david@perseus:~$cat~/.config/cwclientlibrc[cwo]url=https://www.cubicweb.org/token-id=my_cwo_tokensecret=<my-secret>[elo]url=https://www.logilab.orgtoken-id=my_elo_tokensecret=<my-secret>makes it possible to write:david@perseus:~$cwrqlcwo"Any N,S WHERE P eid 1251664, P name N, P summary S"projmanaprojectmanagementtooldavid@perseus:~$cwrql-vejsonexport-jcwo"Any P WHERE P eid 1251664"[{"description":"It reads project descriptions [...]","modification_date":"2015/02/13 18:12:40","icon_format":null,"description_format":"text/rest","summary":"a project management tool","downloadurl":"http://download.logilab.org/pub/projman","cwuri":"http://www.logilab.org/873","__cwetype__":"Project","eid":1251664,"creation_date":"2006/09/28 17:44:38","homepage":null,"debian_source_package":null,"name":"projman"}]or:fromcwclientlibimportcwproxy_forclient=cwproxy_for('cwo')# or client = cwproxy_for('https://www.cubicweb.org/')query='Any X WHERE X is Ticket, X concerns P, P name "cwclientlib"'resp=client.rql(query)data=resp.json()Note that the config file may contain credentials, so its permissions
must be readable only by the user (checked on posix platforms only).Using signed requestsOnce the cubesignedrequestis added, in the WebUI:View aCWUserand click the actionadd an AuthTokenGive an identifier to the token and make it enabledUse the token identifier and the token in your source codeConfigurationYou can define url and credentials for commonly used cubicweb
endpoints in a config file. By default, on Linux, it will be a ini
file located at$HOME/.config/cwclientlibrcbut you may define theCWCLCONFenvironmentvariable to specify it.  This config file can
also be a YAML (file name must end with .yaml) or a JSON file (.json).The file will look like:[cwo]url=https://www.cubicweb.org/token-id=my token idsecret=<my secret>Command line toolscwclientlib comes with 3 simple command-line tools allowing to easily
request a cubicweb application from a shell:cwrqlto make RQL queries:david@perseus:~$cwrql-hUsage:cwrql[options](url|instance_id)rqlquery[rqlquery2]...Options:-h,--helpshowthishelpmessageandexit-j,--jsonproduceJSONdata-vVID,--vid=VIDvidtouse(defaultisjsonexport)-S,--no-ssldoNOTverifysslservercertificate;ignoredif--caisgiven-cCA,--ca=CABundleCAtousetoverifyservercertificate-w,--rqliouserqliodavid@perseus:~$cwrqlcwo"Any VN, VS WHERE V version_of P,
> P name 'cwclientlib', V num VN, V in_state S, S name VS"0.2.1published0.3.0dev0.2.0published0.1.0publishedcwgetto make any king of GET request (ie. call a specific cubicweb controller):david@perseus:~$cwgetcwo/testconfig/1251730\vid=apycot.get_configurationenvironment=4209277[{"pylint_threshold":"7","install":"python_setup","pycoverage_threshold":"70"}]cwshellto connect to a cubicweb endopint and start an interactive
python shell with a few additional builtinsrqlandclient. This shell also provides RQL auto-completion:david@perseus:~$cwshellcwoYouareconnectedtohttps://www.cubicweb.org>>>client.execute('Any X WHERE X is P
Patch               Plan                Project             ProjectEnvironment
>>> rql('AnyP,NWHEREXisProject,XnameP,Vversion_ofX,Vin_stateS,VnumN,Sname"ready"')
[[u'cubicweb-pyramid', u'0.2.0'], [u'cubicweb-simplefacet', u'0.3.2']]>>>Available extra builtins:client:is the CWProxy instance connected to the cubicweb endpoint.rql:shortcut forclient.execute().Python examplesSimple read only query:fromcwclientlibimportcwproxyclient=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 withsignedrequestwith
credentials read from the config file:fromcwclientlibimportcwproxy_forclient=cwproxy_for('cwo')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 an entity, authenticating withsignedrequestbuilding the
authentifier by hand:fromcwclientlibimportcwproxyauth=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 withsignedrequest:fromioimportBytesIOfromcwclientlibimportcwproxy_forclient=cwproxy_for('cwo')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()