Web service – Openerplib

Nous pouvons aussi utiliser à partir d'un client Python, la librairie "openerlib" pour appeler des web services OpenERP.

Il faut au préalable installer la librairie "openerplib" :

sudo easy_install openerp-client-lib

Voici un exemple d'appel:
import openerplib

connection = openerplib.get_connection(hostname="/<adresse serveur OERP>:", database="<nom BD>", login="<logon>", password="<password>")
user_model = connection.get_model("res.partner")
 

ids = user_model.search([("name", "like", "A%")])

for i in ids :
    user_info = user_model.read(i, ["name"])
    print user_info["name"]

Attention, la méthode "browse" ne fonctionne pas avec "openerplib" (cf lien ) :

Pour preuve, le programme python suivant :
import openerplib

connection = openerplib.get_connection(hostname="xxx", database="xxx",
    login="xxx", password="xxx")
user_model = connection.get_model("res.partner")

ids = user_model.search([('name','like','C')])

lb = user_model.browse(ids)

renvoie l'erreur suivante :

Traceback (most recent call last):
  File "/tmp/pb browse.py", line 10, in <module>
    lb = user_model.browse(ids)
  File "build/bdist.linux-x86_64/egg/openerplib/main.py", line 313, in proxy
    args, kw)
  File "build/bdist.linux-x86_64/egg/openerplib/main.py", line 180, in proxy
    result = self.connector.send(self.service_name, method, *args)
  File "build/bdist.linux-x86_64/egg/openerplib/main.py", line 83, in send
    return getattr(service, method)(*args)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1224, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1578, in __request
    verbose=self.__verbose
  File "/usr/lib/python2.7/xmlrpclib.py", line 1264, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1297, in single_request
    return self.parse_response(response)
  File "/usr/lib/python2.7/xmlrpclib.py", line 1473, in parse_response
    return u.close()
  File "/usr/lib/python2.7/xmlrpclib.py", line 793, in close
    raise Fault(**self._stack[0])
Fault: <Fault cannot marshal <class 'openerp.osv.orm.browse_record_list'> objects: 'Traceback (most recent call last):n  File "/opt/openerp/openerp-7.0-20130825-231127/openerp/service/wsgi_server.py", line 83, in xmlrpc_returnn    response = xmlrpclib.dumps((result,), methodresponse=1, allow_none=False, encoding=None)n  File "/usr/lib/python2.7/xmlrpclib.py", line 1085, in dumpsn    data = m.dumps(params)n  File "/usr/lib/python2.7/xmlrpclib.py", line 632, in dumpsn    dump(v, write)n  File "/usr/lib/python2.7/xmlrpclib.py", line 652, in __dumpn    raise TypeError, "cannot marshal %s objects" % type(value)nTypeError: cannot marshal <class 'openerp.osv.orm.browse_record_list'> objectsn'>

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *