Skip to content

Commit

Permalink
[IMP] doc/backend: add example for JSON-RPC interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
rco-odoo committed Sep 8, 2014
1 parent d32d120 commit 7be175b
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions doc/howtos/backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7be175b

Please sign in to comment.