yezyilomo / odoo-rest-api

Module which expose Odoo as a REST API
MIT License
202 stars 162 forks source link
api erp odoo odoo-addons odoo-api

Odoo REST API

This is a module which expose Odoo as a REST API

Installing

Getting Started

Authenticating users

Before making any request make sure you are authenticated. The route which is used to authenticate users is /auth/. Below is an example showing how to authenticate users.

import json
import requests
import sys

AUTH_URL = 'http://localhost:8069/auth/'

headers = {'Content-type': 'application/json'}

# Remember to configure default db on odoo configuration file(dbfilter = ^db_name$)
# Authentication credentials
data = {
    'params': {
         'login': 'your@email.com',
         'password': 'yor_password',
         'db': 'your_db_name'
    }
}

# Authenticate user
res = requests.post(
    AUTH_URL, 
    data=json.dumps(data), 
    headers=headers
)

# Get response cookies
# This hold information for authenticated user
cookies = res.cookies

# Example 1
# Get users
USERS_URL = 'http://localhost:8069/api/res.users/'

# This will take time since it retrives all res.users fields
# You can use query param to fetch specific fields

res = requests.get(
    USERS_URL, 
    cookies=cookies  # Here we are sending cookies which holds info for authenticated user
)

# This will be a very long response since it has many data
print(res.text)

# Example 2
# Get products(assuming you have products in you db)
# Here am using query param to fetch only product id and name(This will be faster)
USERS_URL = 'http://localhost:8069/api/product.product/'

# Use query param to fetch only id and name
params = {'query': '{id, name}'}

res = requests.get(
    USERS_URL, 
    params=params,
    cookies=cookies  # Here we are sending cookies which holds info for authenticated user
)

# This will be small since we've retrieved only id and name
print(res.text)

Allowed HTTP methods

1. GET

Model records:

GET /api/{model}/

Parameters

Model record:

GET /api/{model}/{id}

Parameters

2. POST

POST /api/{model}/

Headers

3. PUT

Model records:

PUT /api/{model}/

Headers

Model record:

PUT /api/{model}/{id}

Headers

All parameters works the same as explained on previous section, what changes is that here they apply to a single record being updated and we don't have filter parameter because id of record to be updated is passed on URL as {id}. Example to give us an idea of how this works.

PUT /api/product.template/95/

Request Body

{
    "params": {
        "data": {
            "related_product_ids": {
                "push": [102, 30],
                "pop": [45],
                "delete": [55]
            }
        }
    }
}

4. DELETE

Model records:

DELETE /api/{model}/

Parameters

Model records:

DELETE /api/{model}/{id}

Parameters

This takes no parameter and we don't have filter parameter because id of record to be deleted is passed on URL as {id}. Example to give us an idea of how this works.

DELETE /api/product.template/95/

Response

{
    "result": true
}

Calling Model's Function

Sometimes you might need to call model's function or a function bound to a record, inorder to do so, send a POST request with a body containing arguments(args) and keyword arguments(kwargs) required by the function you want to call.

Below is how you can call model's function

POST /object/{model}/{function name}

Request Body

{
    "params": {
    "args": [arg1, arg2, ..],
    "kwargs ": {
        "key1": "value1",
        "key2": "value2",
        ...
    }
    }
}

And below is how you can call a function bound to a record

POST /object/{model}/{record_id}/{function name}

Request Body

{
    "params": {
    "args": [arg1, arg2, ..],
    "kwargs ": {
        "key1": "value1",
        "key2": "value2",
        ...
    }
    }
}

In both cases the response will be the result returned by the function called