One of the great advantage of Odoo as an ERP application is its feasibility to communicate with external devices and software. By communication, we mean the process of sending data, retrieving data, authenticating identities or verifying the entities.
Odoo is very well comfortable in working with external entities which have proper APIs.These external entities may include biometric devices, web applications, factory equipment or whatever tools up to your imagination. Thus connecting Odoo with our day-to-day devices or applications not only speed up the processing but also greatly paves the way to the boosting of the business.
APIs may vary from device to device or application to application, but there exist some things in common with all the APIs. Let’s see what are they,
URL
The URL, expanded as Uniform Resource Locator is used to identify a specific resource on a network. The URL is a unique property for each resource so that only that entity is invoked when the URL is called. If we are communicating with a web application, mostly the URL be a domain name like http://www.example.com or something like that. And when it comes to devices, the URLs can be IP addresses and they look somewhat like http://192.168.2.73. However, the purpose is the same for the URL in both cases no matter they appear to be an IP or a Domain. The URL is the location where the driver who handles the requests from external entities is running.
Parameters
Parameters or Arguments can be conditions, filtrations, authentication credentials, resource ids or any kind of expressions or a method call. The driver which’s running on the provided URL receives these requests, then processes it and applies the parameters provided.
Requests
Talking about requests is as simple as a request. Requests mean small queries to the URL for retrieving the data, sending the data, authentication request or a verification.
As we have discussed some common factors in all APIs, now let’s get to the playground and start doing some coding. Here I’m requesting some notes from my website hosted at http://www.example.com and then writing them to a model named notes.note
import urllib3
import json
class NotesNote(models.Model):
_name = 'notes.note'
partner_id = fields.Many2one('res.partner', name="Note By")
note = fields.Text(name="Note")
time_date = fields.Datetime(string="Time")
def get_notes(self):
http = urllib3.PoolManager()
URL = 'http://www.example.com'
root = '/notes/note' # the path where the request handler is located
datas = http.request('GET', URL+root)
datas = json.loads(datas.data.decode('utf-8')) #parses the response to a compatible form
for data in datas:
self.env['notes.note'].create({
'partner_id': self.env['res.partner.search'].search([('notes_user_id', '=', data['user_id'])]).id,
'note': data['note'],
'time_date': data['time']
})
In this example, we imported the urllib3 and json packages of python and then created a request to our URL and specified the route of the data. Then we extracted the response data and converted it to a JSON object using json.loads() method. Finally, we created a record for each of the data we received from the external application.