Odoo is well known for its robust features and rich set of APIs. Odoo offers complete flexibility and security in it’s APIs just like it’s the user interface. All the security measures taken in odoo is maintained in it’s API’s too. Odoo APIs works with it’s ORM (Object Relational Mapping). The API users can access the ORM with their access rights.
Odoo provides two types of prebuilt APIs. They are the XMLRPC API and JSONRPC API. In this blog, we’re going to discuss odoo’s JSONRPC API. The JSONRPC API can be used in different programming languages like Java, JavaScript, PHP, etc. In this blog, we are going to take a look at how we can use the odoo JSONRPC API in python3. In this blog, we will create a python file named odoo_api_login.py. And then we will write a simple program to accept username, password, host URL, host port and database name. Then we will create a login request to odoo and print the response.
Let’s create a python file using nano editor
cybrosys@cybrosys:~$ nano odoo_api_login.py
It will create an empty Python file if not already existing, I added the below code to the file.
import json
import random
import urllib.request
Import getpass
host = input(“Please enter the host URL”)
port = input(“Please provide the host PORT”)
database = input(“Please provide the database name which you want to access”)
user = input(“Please enter the username”)
password = getpass.getpass(prompt=“Please enter the password”)
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)
In the first 4 lines of the code, we imported necessary packages for our program
json - this package is used to handle JSON files (like converting dict to json, json to dict).
random - a package that helps to generate random numbers between a range,
urllib.request - the request class form urllib in order to generate the request,
getpass - order to hide the password,
Then we accepted all the five parameters that we need to connect to odoo.
1. Host URL
2. Host Port
3. Database Name
4. Username
5. Password
After that, we created a function named json_rpc that accepts URL, request method and parameters. Make a request and return the response if success, else raise Exception.
We created another function named call which accepts the URL, service type, request method, and arguments and calls the json_rpc function. Finally, we printed what the function returned. The function will return an integer user id on success because we used the login service here.