Web services are a set of tools available over the internet or intranet networks which use the standardised messaging system to transfer data between applications or systems. Web services allow interaction between different systems or applications using standard libraries such as HTML, XML, WSDL, and SOAP. Web services are not limited to a single programming language.
Odoo web services can be of two types –> XML-RPC and JSON-RPC
XML-RPC
XML-RPC allows connecting programs running on different programming languages and systems working on different operating systems. XML-RPC can be used with programming languages like Python, Java, C, C++, PHP, Perl, Ruby, .Net etc. Here I'll be discussing how to use xml-rpc in Python
Connecting With Database
To connect with an Odoo database we have to define URL of Odoo server, a name of the database used, username and password. A simple way to check whether authentication is correct or not is to check the version of Odoo running. If authentication is correct we will need the user id to access data. It can be read using “uid = common. Authenticate (dB, user, pwd, {})”
Example: To connect to a local host and read the version of Odoo service running
import xmlrpclib
url = 'http://localhost:8069' #url of Odoo server
db = 'db_name'
user = 'User_name'
pwd = 'user_password'
common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
common.version()
Reading Records
Records of a model can be read using the search function.first, we need to define a model.
models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(url))
To search for record in a model,
models.execute_kw(db, uid, pwd,'model.name','search',[[[any_domain]]])
Here domain is a mandatory field. You should keep it empty(like this [[[]]] ) if no domain is required. Also, we can keep a limit on the number of records we need to get using limit.
Example: Print ids of 3 records from model account.invoice with origin field equals false
import xmlrpclib
url = 'http://localhost:8069'
db = 'db_name'
user = 'User_name'
pwd = 'user_password'
common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
invoices = models.execute_kw(db, uid, pwd,'account.invoice','search',[[['origin', '=', False]]], {'limit' :3})
print invoices
Note: To read or write a record the logged user should have permission for reading and write respectively
To get count we can use search_count instead of search for getting the number of records in our search.
To read a record we can use read instead of search and we need to define all fields in the model which we need.
models.execute_kw(db, uid, pwd,'model.name','read',[ids],{'fields':['field_1', 'field_2']})
we can also use search_read which is equivalent to search followed by read
models.execute_kw(db, uid, pwd,'model.name','search_read',[[[domain]]],{'fields':['field_1', 'field_2']})
Example: Print name and state of all invoices against customer agrolait
import xmlrpclib
url = 'http://localhost:8069'
db = 'db_name'
user = 'User_name'
pwd = 'user_password'
common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
ids = models.execute_kw(db, uid, pwd,'account.invoice','search',[[['name', '=', 'agrolait']]])
invoice_names = models.execute_kw(db, uid, pwd,'account.invoice','read',[ids],{'fields':['name', 'state']})
print invoice_names
Managing Records
To create, update or unlink a record we can use create(), write(), and unlink() respectively
models.execute_kw(db, uid, pwd, 'model.name','create', [{'field_name':'field_value'}])
models.execute_kw(db, uid, pwd, 'model.name', 'write', [[id]],[{'field_name':'field_value'}])
models.execute_kw(db, uid, pwd,'model.name','unlink',[[id]])
Example: Create a customer named “test user”, update his name to “User 23”. And finally delete his record.
import xmlrpclib
url = 'http://localhost:8069'
db = 'db_name'
user = 'User_name'
pwd = 'user_password'
common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(url))
models.execute_kw(db, uid, password, 'res.users', 'create', [{'name': "New User", 'login': "new_user",}])
# cr_cus will have id of new record created
models.execute_kw(db, uid, pwd,'res.users ','update', [cr_cus], {'name': “User 223”})
models.execute_kw(db, uid, pwd,'res.users ','unlink',[[cr_cus]])
Note: When creating records of a model we might want to define all required fields(if they are not given by default in Odoo). In the above example, we need to define login for the user.
Json-RPC
Similar to XML-RPC we can interact with Odoo server using Json-RPC. General working is to establish a connection between two peers. During connection, a peer may invoke methods from other by sending the request. And receiver will reply with a response.
In Odoo Json-RPC can be implemented using standard python libraries(like json, random, urllib2) or using package jsonrpclib.
To connect with Odoo server using jsonrpclib we can use
url = “host url with port”
server = jsonrpclib.Server(url)