[IMP] doc/backend: add example for JSON-RPC interaction
This commit is contained in:
parent
d32d120a0a
commit
7be175bad6
|
@ -1682,15 +1682,13 @@ XML-RPC Library
|
|||
---------------
|
||||
|
||||
The following example is a Python program that interacts with an Odoo
|
||||
server with the library xmlrpclib.
|
||||
|
||||
::
|
||||
server with the library ``xmlrpclib``::
|
||||
|
||||
import xmlrpclib
|
||||
|
||||
root = 'http://%s:%d/xmlrpc/' % (HOST, PORT)
|
||||
|
||||
uid = xmlrpclib.ServerProxy(root + 'common').login(db, username, password)
|
||||
uid = xmlrpclib.ServerProxy(root + 'common').login(DB, USER, PASS)
|
||||
print "Logged in as %s (uid: %d)" % (USER, uid)
|
||||
|
||||
# Create a new idea
|
||||
|
@ -1700,7 +1698,7 @@ server with the library xmlrpclib.
|
|||
'description' : 'This is another idea of mine',
|
||||
'inventor_id': uid,
|
||||
}
|
||||
idea_id = sock.execute(db, uid, password, 'idea.idea', 'create', args)
|
||||
idea_id = sock.execute(DB, uid, PASS, 'idea.idea', 'create', args)
|
||||
|
||||
.. exercise:: Add a new service to the client
|
||||
|
||||
|
@ -1750,8 +1748,52 @@ server with the library xmlrpclib.
|
|||
'course_id' : course_id,
|
||||
})
|
||||
|
||||
.. note:: there are also a number of high-level APIs in various languages to
|
||||
access Odoo systems without *explicitly* going through XML-RPC e.g.
|
||||
JSON-RPC Library
|
||||
----------------
|
||||
|
||||
The following example is a Python program that interacts with an Odoo server
|
||||
with the libraries ``urllib2`` and ``json``::
|
||||
|
||||
import json
|
||||
import random
|
||||
import urllib2
|
||||
|
||||
def json_rpc(url, method, params):
|
||||
data = {
|
||||
"jsonrpc": "2.0",
|
||||
"method": method,
|
||||
"params": params,
|
||||
"id": random.randint(0, 1000000000),
|
||||
}
|
||||
req = urllib2.Request(url=url, data=json.dumps(data), headers={
|
||||
"Content-Type":"application/json",
|
||||
})
|
||||
reply = json.load(urllib2.urlopen(req))
|
||||
if reply.get("error"):
|
||||
raise Exception(reply["error"])
|
||||
return reply["result"]
|
||||
|
||||
def call(url, service, method, *args):
|
||||
return json_rpc(url, "call", {"service": service, "method": method, "args": args})
|
||||
|
||||
# log in the given database
|
||||
url = "http://%s:%s/jsonrpc" % (HOST, PORT)
|
||||
uid = call(url, "common", "login", DB, USER, PASS)
|
||||
|
||||
# create a new idea
|
||||
args = {
|
||||
'name' : 'Another idea',
|
||||
'description' : 'This is another idea of mine',
|
||||
'inventor_id': uid,
|
||||
}
|
||||
idea_id = call(url, "object", "execute", DB, uid, PASS, 'idea.idea', 'create', args)
|
||||
|
||||
Examples can be easily adapted from XML-RPC to JSON-RPC.
|
||||
|
||||
.. note::
|
||||
|
||||
There are a number of high-level APIs in various languages to access Odoo
|
||||
systems without *explicitly* going through XML-RPC or JSON-RPC, such as:
|
||||
|
||||
* https://github.com/akretion/ooor
|
||||
* https://github.com/syleam/openobject-library
|
||||
|
|
Loading…
Reference in New Issue