v 14.0 Third Party 11 4770
Download for v 14.0 Deploy on Odoo.sh
Availability
Odoo Online
Odoo.sh
On Premise
Odoo Apps Dependencies Discuss (mail)
Community Apps Dependencies
Lines of code 1980
Technical Name openapi
LicenseLGPL-3
Websitehttps://t.me/sync_studio
Versions 11.0 8.0 14.0 13.0 10.0 16.0 15.0 12.0
You bought this module and need support? Click here!
Availability
Odoo Online
Odoo.sh
On Premise
Odoo Apps Dependencies Discuss (mail)
Community Apps Dependencies
Lines of code 1980
Technical Name openapi
LicenseLGPL-3
Websitehttps://t.me/sync_studio
Versions 11.0 8.0 14.0 13.0 10.0 16.0 15.0 12.0

REST API/OpenAPI/Swagger

Secure tool for convenient integrations

Version: v14.0.1.2.4


Tested and maintained by
IT Projects Labs
Assitance: help@itpp.dev

Best API accessories for every integration step:
  • Grant exact access rights needed for an integration
  • Load generated OpenAPI Specification into Postman, Swagger Editor or any other tools that supports it
  • Use new class methods to execute typical integration task in a single request
  • Monitor API requests/responses at desired level of details
To package custom integration into a module, use Sync Studio. It allows exporting scripts and event handlers as xml data file for Odoo module.

Easy to use in any programming languages

# https://github.com/Yelp/bravado
from bravado.requests_client import RequestsClient
from bravado.client import SwaggerClient
http_client = RequestsClient()
http_client.set_basic_auth('yourdomain.example.com', 'DATABASE-NAME', 'USER-TOKEN')
odoo = SwaggerClient.from_url(
    'https://yourdomain.example.com/api/v1/demo/swagger.json?token=demo_token&db=source',
    http_client=http_client
)
result = odoo.res_partner.callMethodForResPartnerModel(
    method_name="search",
    body={
        'args': [[('email', '=', 'sync@it-projects.info')]]
    }
).response().incoming_response.json()
partner_id = result and result[0]
if not partner_id:
    result = odoo.res_partner.addResPartner(body={
        "name": "OpenAPI Support",
        "email": "sync@it-projects.info"
    }).response().result
    partner_id = result.id
odoo.res_partner.callMethodForResPartnerSingleRecord(
  id=partner_id,
  method_name="message_post",
  body={
    "kwargs": {
      "body": "The Openapi module works in Python! Thank you!",
      "partner_ids": [partner_id]
    }
  }
).response()
Comming soon...
Comming soon...
Comming soon...
Comming soon...
Comming soon...
"API" sounds too technical?
You may send this page to your IT guys to evaluate the possibilities or just tell us your concerns: help@itpp.dev

REST API/Openapi/Swagger

Installation

  • Install python packages:

    python3 -m pip install bravado_core swagger_spec_validator

  • Install this module in a usual way

  • Add openapi to --load parameter, e.g.:

    ./odoo-bin --workers=2 --load openapi,base,web --config=/path/to/odoo.conf
    
    • Note: Skipping this step may lead to error response:

      werkzeug.exception.BadRequest: 400 Bad Request <..> :
      Function declared as capable of handling request of type 'apijson' but called with a request of type 'json'
      

Configuration

Activating and customization

  • Open menu [[ OpenAPI ]] >> OpenAPI >> Integrations

  • Click [Create]

  • Specify Name for integration, e.g. test

  • Set Log requests to Full

  • Set Log responses to Full

  • In Accessable models tab click Add an item

    • Set Model, for example res.users

    • Configure allowed operations

      • [x] Create via API

        • Set Creation Context Presets, for example
          • Name: brussels
          • Context: {"default_tz":"Europe/Brussels", "default_lang":"fr_BE"}
      • [x] Read via API

        • Set Read One Fields -- fields to return on reading one record

        • Set Read Many Fields -- fields to return on reading multiple records

          Note: you can use Export widget in corresponding Model to create Fields list. To do that:

          • Open menu for the Model
          • Switch to list view
          • Select any record
          • click [Action] -> Export
          • Set Export Type to Export all Data
          • Add the fields you need to right column
          • Click Save fields list, choose name and save the list
          • Now the list is availab to set Read One Fields, Read Many Fields settings
      • [x] Update via API

      • [x] Delete via API

  • Click [Save]

  • Copy Specification Link to use it in any system that support OpenAPI

Authentication

  • Activate Developer Mode
  • Open menu [[ Settings ]] >> Users & Companies >> Users
  • Select a user that will be used for iteracting over API
  • In Allowed Integration select some integrations
  • Copy OpenAPI Token to use it in any system that support OpenAPI

If necessary, you can reset the token by pressing [Reset OpenAPI Token] button

Usage

Swagger Editor

As the simplest example, you can try API in Swagger Editor. It allows to review and check API

  • Open http://editor.swagger.io/
  • Click menu File >> Import URL
  • Set Specification link
  • RESULT: Specification is parsed succefully and you can see API presentation
  • Click [Authorize] button
    • Username -- set database name
    • Password -- set OpenAPI Token (how to get one is described in authentication above)
Note:

The Swagger Editor sends requests directly from browser which leads to CORS error and work with it is not available in odoo.sh. The easiest solution is to simply copy-past the curl command from Swagger Editor and run it from the terminal.

Alternatively, you can grant CORS headers in your web server. Below is example for Nginx:

location /api/v1 {
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*' 'always';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH' 'always';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' 'always';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
    }
    if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*' 'always';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH' 'always';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' 'always';
    }
    if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*' 'always';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH' 'always';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' 'always';
    }
    if ($request_method = 'PUT') {
        add_header 'Access-Control-Allow-Origin' '*' 'always';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH' 'always';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' 'always';
    }
    if ($request_method = 'DELETE') {
        add_header 'Access-Control-Allow-Origin' '*' 'always';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH' 'always';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' 'always';
    }
    if ($request_method = 'PATCH') {
        add_header 'Access-Control-Allow-Origin' '*' 'always';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE, PATCH' 'always';
        add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' 'always';
    }
    # ...
}

How to call methods with arguments via API

Here is an example of calling a search method with domain.

This is how it is usually done from python code:

partner_ids = self.env["res.partner"].search([("is_company", "=", "True")])

On using API it would be as following:

curl -X PATCH "http://example.com/api/v1/demo/res.partner/call/search" -H "accept: application/json" \
-H "authorization: Basic BASE64_ENCODED_EXPRESSION" -H "Content-Type: application/json" \
-d '{ "args": [[["is_company", "=", "True" ]]]}'

Updating existing record

For example, to set phone value for a partner, make a PUT request in the following way:

curl -X PUT -H "Authorization: Basic BASE64_ENCODED_EXPRESSION" \
-H "Content-Type: application/json" -H "Accept: */*" \
-d '{ "phone": "+7123456789"}' "http://example.com/api/v1/demo/res.partner/41"

To set many2one field, you need to pass id as a value:

curl -X PUT -H "Authorization: Basic BASE64_ENCODED_EXPRESSION" \
-H "Content-Type: application/json" -H "Accept: */*" \
-d '{ "parent_id": *RECORD_ID*}' "http://example.com/api/v1/demo/res.partner/41"

For more examples visit https://itpp.dev/sync website

Please log in to comment on this module

  • The author can leave a single reply to each comment.
  • This section is meant to ask simple questions or leave a rating. Every report of a problem experienced while using the module should be addressed to the author directly (refer to the following point).
  • If you want to start a discussion with the author, please use the developer contact information. They can usually be found in the description.
Please choose a rating from 1 to 5 for this module.
by
Mariem-Fogits
on 4/24/23, 9:31 AM



Unable to install in the server
by
Rezkya Hairy
on 6/13/21, 12:52 PM

on my local machine works fine. but when i tried to install in the ubuntu server, i got "Unable to install module "openapi" because an external dependency is not met: Python library not installed: bravado_core" tried to install the bravado_core but no luck..

Re: Unable to install in the server
by
Elizaryev Ivan Nikolaevich
on 6/13/21, 5:51 PM Author

You probably have few python environments on the the server.

Try this command:

python3 -m pip install bravado_core swagger_spec_validator


by
Mustafa Sanei
on 5/29/21, 10:20 AM

Time to time unreasonably losts pathes used in integration.
That is first several calls GET: /api/v1/test4/mrp.production works well. Then suddenly same path call returns error 404 NOT FOUND, one-three calls not found error and then works good again.
Meantime Odoo itself fully working like a charm, without any issues or breaks, logs are normal no suspicious lines.
Very strange behaviour, unreliable api.

Re:
by
Elizaryev Ivan Nikolaevich
on 5/30/21, 2:31 PM Author

Hello Mustafa,

Thank you for your feedback! Can you contact us via e-mail help@itpp.dev with more details about errors you get?


Integrated with Job Queue (OCA)
by
Naran
on 12/16/21, 12:17 AM

Can this module be integrated with OCA Job queue for POST & PUT calls..?

Thanks,



by
Wolfgang
on 11/8/21, 9:17 PM

Hello!

Unfortunately even after installing bravado core the way you recommended (python3 -m pip install bravado_core swagger_spec_validator) I get the error 

WARNINGserverDistributionNotFound: The 'bravado_core' distribution was not found and is required by the application
Re:
by
Elizaryev Ivan Nikolaevich
on 11/9/21, 2:12 AM Author

Hello!

Consider to book our support service and help us to maintain this module: https://www.patreon.com/itpp


by
ivan garcia
on 5/10/21, 10:18 AM