Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

API Integration example

By
Yves Goldberg
on 7/29/15, 10:47 AM 1,004 views

I am looking for example on how to integrate an external application that provides an API.

i.e. I get a json object from that external application after a request in the form of:

GET ONE: curl -u username:password http://server.com:2222/api/user/1234

Is there a module that show how I would use that collected data and use it in Odoo?


TIA

Hi, whether the application has REST api? Anyway, If you are getting a json object, you can use "loads()" of json library to convert it as a dictionary. Then you can extract the required data from that dictionary and do whatever you need using a python function.

Akhil P Sivan
on 8/18/15, 3:25 AM
1

Axel Mendoza

--Axel Mendoza--
10203
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 7/29/15, 12:02 PM

You could use something like this(this is an api test model that I create):

from openerp.osv import fields, osv
import requests

class solt_http_test(osv.osv): _name = 'solt.http.test' _columns = { 'name': fields.char('URL', size=1024), 'method': fields.selection([('post', 'POST'), ('get', 'GET'), ('put', 'PUT'), ('patch', 'PATCH'), ('delete', 'DELETE')], string='HTTP Method'), 'user': fields.char('User', size=64), 'password': fields.char('Password', size=64), 'content': fields.text('Content'), 'response': fields.text('Response'), } def action_request(self, cr, uid, ids, context=None): for test in self.browse(cr, uid, ids,context): auth = None if test.user and test.password: auth = (test.user,test.password) headers = {'Content-Type': 'application/json', 'Accept': 'application/json'} result = getattr(requests, test.method)(test.name, test.content, auth=auth, headers=headers) test.write({'response':result.text}) return True solt_http_test()

Modify this for your convenience. Here is the views, action and menu i use:

        <record id="solt_http_test_form_view" model="ir.ui.view">
<field name="name">solt.http.test.form</field>
<field name="model">solt.http.test</field>
<field name="arch" type="xml">
<form string="HTTP Test" version="7.0">
<header>
<button name="action_request" string="Test" type="object" icon="gtk-go-forward"/>
</header>
<sheet layout="auto">
<group colspan="6">
<field name="name"/>
<field name="method"/>
<field name="user"/>
<field name="password"/>
</group>
<group>
<field name="content"/>
</group>
<group>
<field name="response"/>
</group>
</sheet>
</form>
</field>
</record>
<record id="solt_http_test_tree_view" model="ir.ui.view">
<field name="name">solt.http.test.model.tree</field>
<field name="model">solt.http.test</field>
<field name="arch" type="xml">
<tree string="HTTP Test" version="7.0">
<field name="name"/>
<field name="method"/>
</tree>
</field>
</record>
<record id="solt_http_test_action" model="ir.actions.act_window">
<field name="name">HTTP Tests</field>
<field name="res_model">solt.http.test</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
<menuitem name="API Tests" id="solt_rest_weaver_menu" parent="base.menu_administration" sequence="5"/> 
<menuitem name="HTTP Tests" id="solt_http_test_submenu" parent="solt_rest_weaver_menu" action="solt_http_test_action" sequence="2"/>



Hi All, What information I can pass in this fields ,I mean URL HTTP Method User Password Content Response 'name': fields.char('URL', size=1024), 'method': fields.selection([('post', 'POST'), ('get', 'GET'), ('put', 'PUT'), ('patch', 'PATCH'), ('delete', 'DELETE')], string='HTTP Method'), 'user': fields.char('User', size=64), 'password': fields.char('Password', size=64), 'content': fields.text('Content'), 'response': fields.text('Response'),

Odoo
on 8/18/15, 7:58 AM

Can you explain with an example?

Odoo
on 8/18/15, 7:59 AM

Just install it packaged in a module and use it throw the view, for example with url like http://google.com, and method get

Axel Mendoza
on 8/18/15, 12:10 PM

I got this Error result = getattr(requests, test.method)(test.name, test.content, auth=auth, headers=headers) TypeError: get() takes exactly 1 argument (4 given)

Odoo
on 8/20/15, 1:55 AM

When I try using GET Method ...

Odoo
on 8/20/15, 1:56 AM

I try with google.com, and method is GET ,but i got above error also here what is the username=? and password=? Need a help...........Axel

Odoo
on 8/20/15, 1:58 AM

The error of get that takes 1arg and received 4 is related to the version that you are using of requests. You need to adjust to the http methods signature of your requests version. The username and password refers to http basic auth

Axel Mendoza
on 8/20/15, 2:16 AM
0
Yves Goldberg
On 8/15/15, 3:40 AM

Thank you Axel

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 7/29/15, 10:47 AM
Seen: 1004 times
Last updated: 8/18/15, 3:25 AM