Author: |
Babatope Ajepe |
License: |
LGPL-3 |
Branch: |
12.0 |
Repository: |
latera/odoo-restful |
Dependencies: |
base,
restful,
and
web |
Languages: |
HTML (14, 2.3%),
Markdown (100, 16.1%),
Python (435, 70.0%),
XML (50, 8.1%),
and
reStructuredText (22, 3.5%) |
Other branches: |
13.0 |
<h3>Odoo RESTful API(restful)</h3>
<p>This is an HTTP framework that only cares about generating an HTTP response for each HTTP
request.
In other to use this module, a basic understating of Odoo RPC interface is required(though not that neccessary) especially when dealing with Many2many and One2many relationship. The implementation sits on the existing Odoo RPC features, data structures and format when creating or delecting Odoo's records are still applicable. I will be demostrating the usage using python request library.</p>
<h4>Access token request</h4>
<p>An access token is required in other to be able to perform any operations and ths token once generated should alway be send a long side any subsequents request.
```python
import requests</p>
<p>headers = {
'charset':'utf-8'
}</p>
<p>query = {
'login': 'admin',
'password': 'admin',
'db': 'demo<em>db'
}
base</em>url = 'http://theninnercicle.com.ng'</p>
<p>req = requests.post('{}/api/auth/token'.format(base_url), data=data, headers=headers)
content = req.json()</p>
<p>headers['access-token'] = content['access_token'] # add the access token to the header
print(headers)
```</p>
<h3>To delete acccess-token</h3>
<p><code>python
req = requests.delete('{}/api/auth/token'.format(base_url), params=query, headers=headers)
</code></p>
<h3>[GET]</h3>
<p>```python
import requests</p>
<p>headers = {
'charset': 'utf-8',
'access-token': 'access_token'
}
model = 'res.partner'</p>
<h1>You can get object by its id</h1>
<p>id = 100
req = requests.get('{}/api/{}/{}'.format(base_url, model, id), headers=headers)
print(req.json())</p>
<h1>You can make queries</h1>
<p>headers['content-type'] = 'application/x-www-form-urlencoded'
query = {
'domain': "[('supplier','=',True),('parent<em>id','=', False)]",
'order': ['name asc', 'id desc'], # or 'name asc, id desc'
'limit': 10,
'offset': 0,
'fields': "['name', 'supplier', 'parent</em>id']"
}</p>
<h1>You can ommit unnessesary query options</h1>
<p>query = {
'domain': "[('supplier','=',True),('parent_id','=', False)]",
'limit': 10
}</p>
<h1>You can also use JSON-like domains</h1>
<p>query = {
'domain': "{'id':100, 'parent<em>id!':true}",
'limit': 10
}
req = requests.get('{}/api/{}/'.format(base</em>url, model), headers=headers, params=query)
print(req.json())
```</p>
<h3>[POST]</h3>
<p><code>python
model = 'res.partner'
data = {
'name': 'Babatope Ajepe',
'country_id': 105,
'child_ids': [
{
'name': 'Contact',
'type': 'contact'
},
{
'name': 'Invoice',
'type': 'invoice'
}
],
'category_id': [{'id': 9}, {'id': 10}]
}
req = requests.post('{}/api/{}/'.format(base_url, model), headers=headers, data=data)
print(req.json())
</code></p>
<h3>[PUT]</h3>
<p><code>python
model = 'res.partner'
id = 100
data = {
'name': 'Babatope Ajepe',
'country_id': 103,
'category_id': [{'id': 9}]
}
req = requests.put('{}/api/{}/{}'.format(base_url, model, id), headers=headers, data=data)
print(req.json())
</code></p>
<h3>[DELETE]</h3>
<p><code>python
model = 'res.partner'
id = 100
req = requests.delete('{}/api/{}/{}'.format(base_url, model, id), headers=headers)
print(req.status_code)
</code></p>