This question has been flagged
20 Replies
44428 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

Avatar
Discard

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.

Best Answer

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"/>



Avatar
Discard

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'),

Can you explain with an example?

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

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)

When I try using GET Method ...

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

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

Best Answer

If you need any support in Odoo Integration, please refer https://www.confianzit.com/odoo-integration

Avatar
Discard
Best Answer

Alex,  thank you for posting this. I'm a newbie in Odoo and need to integrate auto action to our system (asap as usually:) 
I'm stucked currently with initial step:
    import requests
It gives me following error:
   Odoo Server Error - Validation Error: forbidden opcode(s) in 'import Requests': IMPORT_NAME.

Could someone give me a hint how to enable using "requests"?

Avatar
Discard

requests is this library at: http://docs.python-requests.org

you need to install it first,

pip install requests

should works

Thank you for answer! But ... is that possible to do for free edition of Odoo? Is there some kind of sandbox to install external modules?

Yes, of course, Odoo is Open Core so as long as you know what you do you could do anything

that's cool! But i'm using Odoo like https://my.odoo.com, so where is console to install module? Or should I use Install Model from UI menu (which expect ZIP file) and prepare it first from docs.python-requests.org?

No way then to do it, since you don't have the permissions to do the required installs and customizations

arg... bad news. Could you recommend some else way to have automated actions for integration purpose? I need to setup some trigger on changes in Odoo and hit another system then. Or only the way is have own installation of Odoo?

Your issue doesn't seems to be with requests, since that library is included as a python dependency in requirements.txt

The issue seems to be in the place where your code is located or the way it's loaded. Maybe it's because you are importing the module or something. Maybe you need to install on promise or on own your cloud server vps

OK, thank you once again! I'll try find a way to continue.

Author Best Answer

Thank you Axel

Avatar
Discard