Search 1.9 billion lines of Odoo code on GitHub

odoo-restful

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&#39;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 = { &#39;charset&#39;:&#39;utf-8&#39; }</p> <p>query = { &#39;login&#39;: &#39;admin&#39;, &#39;password&#39;: &#39;admin&#39;, &#39;db&#39;: &#39;demo<em>db&#39; } base</em>url = &#39;http://theninnercicle.com.ng&#39;</p> <p>req = requests.post(&#39;{}/api/auth/token&#39;.format(base_url), data=data, headers=headers) content = req.json()</p> <p>headers[&#39;access-token&#39;] = content[&#39;access_token&#39;] # add the access token to the header print(headers) ```</p> <h3>To delete acccess-token</h3> <p><code>python req = requests.delete(&#39;{}/api/auth/token&#39;.format(base_url), params=query, headers=headers) </code></p> <h3>[GET]</h3> <p>```python import requests</p> <p>headers = { &#39;charset&#39;: &#39;utf-8&#39;, &#39;access-token&#39;: &#39;access_token&#39; } model = &#39;res.partner&#39;</p> <h1>You can get object by its id</h1> <p>id = 100 req = requests.get(&#39;{}/api/{}/{}&#39;.format(base_url, model, id), headers=headers) print(req.json())</p> <h1>You can make queries</h1> <p>headers[&#39;content-type&#39;] = &#39;application/x-www-form-urlencoded&#39; query = { &#39;domain&#39;: &quot;[(&#39;supplier&#39;,&#39;=&#39;,True),(&#39;parent<em>id&#39;,&#39;=&#39;, False)]&quot;, &#39;order&#39;: [&#39;name asc&#39;, &#39;id desc&#39;], # or &#39;name asc, id desc&#39; &#39;limit&#39;: 10, &#39;offset&#39;: 0, &#39;fields&#39;: &quot;[&#39;name&#39;, &#39;supplier&#39;, &#39;parent</em>id&#39;]&quot; }</p> <h1>You can ommit unnessesary query options</h1> <p>query = { &#39;domain&#39;: &quot;[(&#39;supplier&#39;,&#39;=&#39;,True),(&#39;parent_id&#39;,&#39;=&#39;, False)]&quot;, &#39;limit&#39;: 10 }</p> <h1>You can also use JSON-like domains</h1> <p>query = { &#39;domain&#39;: &quot;{&#39;id&#39;:100, &#39;parent<em>id!&#39;:true}&quot;, &#39;limit&#39;: 10 } req = requests.get(&#39;{}/api/{}/&#39;.format(base</em>url, model), headers=headers, params=query) print(req.json()) ```</p> <h3>[POST]</h3> <p><code>python model = &#39;res.partner&#39; data = { &#39;name&#39;: &#39;Babatope Ajepe&#39;, &#39;country_id&#39;: 105, &#39;child_ids&#39;: [ { &#39;name&#39;: &#39;Contact&#39;, &#39;type&#39;: &#39;contact&#39; }, { &#39;name&#39;: &#39;Invoice&#39;, &#39;type&#39;: &#39;invoice&#39; } ], &#39;category_id&#39;: [{&#39;id&#39;: 9}, {&#39;id&#39;: 10}] } req = requests.post(&#39;{}/api/{}/&#39;.format(base_url, model), headers=headers, data=data) print(req.json()) </code></p> <h3>[PUT]</h3> <p><code>python model = &#39;res.partner&#39; id = 100 data = { &#39;name&#39;: &#39;Babatope Ajepe&#39;, &#39;country_id&#39;: 103, &#39;category_id&#39;: [{&#39;id&#39;: 9}] } req = requests.put(&#39;{}/api/{}/{}&#39;.format(base_url, model, id), headers=headers, data=data) print(req.json()) </code></p> <h3>[DELETE]</h3> <p><code>python model = &#39;res.partner&#39; id = 100 req = requests.delete(&#39;{}/api/{}/{}&#39;.format(base_url, model, id), headers=headers) print(req.status_code) </code></p>