JSON-RPC
JSON(Javascript Object Notation) is a format used for data interchange mainly in API’s
and other applications. The main advantage of using json is that it is easy to write and
parse through the contents. It can easily be read and interpreted. RPC (Remote Procedure
Call) is used to make a remote call to a python method.
Json-rpc in odoo is an API method used to connect remotely to odoo server. It is an RPC
encoded with JSON. It is similar to XML-RPC, but it's much lighter than XML-RPC and does
not require a response for sending data to the server.
Connection to Odoo:
Let us see how we can connect to Odoo using the json-rpc method.
Search/ Read Records:
Json-rpc can also be used to fetch data from the database. We can use it to read/search
out data from our database.
We’ll use the same method we used above for reading the records.
import json
import random
import urllib.request
host = 'localhost'
port = 8015
database = 'DemoDB'
user = 'admin'
password = 'admin'
def json_rpc(url, method, params):
data = {
"jsonrpc": "2.0",
"method": method,
"params": params,
"id": random.randint(0, 1000000000),
}
req = urllib.request.Request(url=url, data=json.dumps(data).encode(), headers={
"Content-Type":"application/json",
})
reply = json.loads(urllib.request.urlopen(req).read().decode('UTF-8'))
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})
url = "http://%s:%s/jsonrpc" % (host, port)
uid = call(url, "common", "login", database, user, password)
print(uid)
task_ids = call(url, "object", "execute", database, uid, password, "project.task", "read", [1])
print("Tasks: ", task_ids)
We change the arguments to the call method in order to search/ read records from the
database.
url: specify the json-rpc url with the host and port number
service: set the service as object
method: set the method as execute
args: pass the arguments in the below order:
- database: database to which connection should be established
- uid: user id of the logged in user
- password: password of the user
- model_name: name of the model (in dot notation) from which the data
should be read.
- method: specify the method as read
- ids: list of id’s of which data should be read
Output:
Similarly we can search the details of multiple records by passing multiple ids as listed
in the last argument.
Create Records:
Similar way we can use json-rpc to create records into the database. For that, we need to
change the argument list we provided in the search method slightly.
Let us check how we can implement that.
url: Specify the json-rpc url with the host and port number
service: Set the service as object
method: Set the method as execute
*args: Pass the arguments in the below order:
- database: Database to which connection should be established
- uid: User id of the logged-in user
- password: Password of the user
- model_name: Name of the model (in dot notation) from which the
data should be created.
- method: Specify the method as create
- args: Argument list to create the record in model
project_args = {
'name': 'JSON_RPC Project 3'
}
project_id = call(url, "object", "execute", database, uid, password, "project.project", "create", project_args)
#Create task only if project is created
if project_id:
task_args = {
'name': 'JSON_RPC Task 1',
'project_id': project_id
}
task_id = call(url, "object", "execute", database, uid, password, "project.task", "create", task_args)
On successful execution of the above code, we can see in the frontend for the new records
created. A new project with the name can be seen in the screenshot below along with the
new task created under that particular project.
Update Records:
Next we’ll check on how we can update an existing record in the database using the
json-rpc method.
For that, we use the same method we used to create a record, with a change in the method
specified and a recordset id for which the value needs to be updated.
The arguments are:
url: specify the json-rpc url with the host and port number
service: set the service as object
method: set the method as execute
*args: pass the arguments in the below order:
- database: database to which connection should be established
- uid: user id of the logged-in user
- password: password of the user
- model_name: name of the model (in dot notation) from which the
data should be updated.
- method: specify the method as write
- [id]: record id for which the value needs to be updated
- args: argument list to update the record in model
project_args = {
'name': 'JSON_RPC Project Updated'
}
project_id = call(url, "object", "execute", database, uid, password, "project.project", "write",[id], project_args)
The screenshot below shows the updated project with the new name supplied through the
json-rpc data.
We can see the update occurred on the record on checking the chatter.
Update Records:
With the help of JSON-RPC we can unlink a record from the database. For that, we need to
change the method to unlink.
The arguments are:
url: specify the json-rpc url with the host and port number
service: set the service as object
method: set the method as execute
*args: pass the arguments in the below order:
- database: database to which connection should be established
- uid: user id of the logged-in user
- password: password of the user
- model_name: name of the model (in dot notation) from which the data
should be unlinked.
- method: specify the method as unlink
- [ids]: record ids for which the value needs to be unlinked
project_id = call(url, "object", "execute", database, uid, password, "project.project", "unlink", [ids])
On successful execution the record(s) specified in the ids list will be removed from the
database.
Calling Method:
Odoo RPC Library:
Like JSON-RPC, OdooRPC is a python module that enables connection to Odoo servers using
RPC.
Installation:
pip3 install odoorpc
Connection to Odoo database:
The odoorpc module uses Odoo class to connect to the database, ie, it acts as an entry
point.
import odoorpc
odoo = odoorpc.ODOO('localhost', port=8015) #add your host and odoo running port
odoo.login('database_name', 'username', 'password')
The above code will log in using the credentials into the database provided if the
credentials are accepted. Else will return RPCError: Access Denied.
After a successful connection to a database, you’ll be able to create, read, update and
delete data’s using the odoorpc.
Next, let us check on how we can create a new database using odoorpc.