Enable Dark Mode!
creating-api-for-external-entities-in-odoo-erp.png
By: Muhammed Nishad

Creating API For External Entities In Odoo

Technical

Odoo is known for its highly customizable and flexible characteristics. The ERP can be customized any way the business organization wants them to be. Odoo can be customized to use with almost every device or application that have a proper API. But in some cases, one needs to connect the Odoo ERP with some other devices.

For example, say, one needs to connect their e-commerce software which is running on PHP or Java. And they need to access Odoo ’s data and make some changes or trigger an action. Under such cases, one can create APIs on Odoo for such devices or applications.

Take this condition as an example, we have a mobile application that is used by our employees who is working on the client site. And they need to update some project status or need to log their timesheets. Here, Odoo should be able to handle requests from the mobile client. This is where one needs to work on APIs for external entities.

When we create an API to communicate with external entities, we have to be aware of some factors. They are:

Data Format
The format we use to send and receive data should be both sender and receiver compatible. Most systems use JSON to represent their data. Some systems use XML to communicate. Both the options are available in Odoo.

Authentication Credentials
Providing Authentication Credentials in the proper way is as important as providing the right credentials.

Now let’s get into some coding. Let’s create a class first. And then add a method that is triggered when a call occurs.

Here this method will return all the partners details if no parameter is passed and if one parameter is passed. it will return the data of the partner who has that id.

from odoo import http
from odoo.http import request
class PartnerProvider(http.HttpRequest):
   @http.route(['/odootest/partners'], type='json', auth='public', methods=['POST'])
   def send_users(self, **kwargs):
       if len(kwargs) == 0:
           cr = self.env.cr
           query = """
           SELECT name, mobile, city FROM res_partner WHERE active = true
           """
           cr.execute(query)
           partners = cr.dictfetchall()
           return {
               'partners': partners,
           }
       elif len(kwargs) == 1:
           cr = self.env.cr
           query = """
                       SELECT name, mobile, city FROM res_partner WHERE active = true AND id = %s
                       """ %kwargs['id'] # Assuming that the data from the client contains the id of the partner
           cr.execute(query)
           partner = cr.dictfetchall()
           return {
               'partners': partner,
           }
In the fourth line, we defined a route where the external entities can call and in the next line, we added the callback for that rout. And in kwargs variable, we received the request from the client and then performed a query to find the user according to the parameter and then returned the user’s name, mobile number, and city


If you need any assistance in odoo, we are online, please chat with us.



1
Comments

Anand shukla

Thanks for sharing this information ...!!

15/03/2019

-

4:26AM



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message