Skip to content

Latest commit

 

History

History
230 lines (162 loc) · 7.41 KB

File metadata and controls

230 lines (162 loc) · 7.41 KB

WebService Base

Production/Stable License: LGPL-3 OCA/web-api Translate me on Weblate Try me on Runboat

This module creates WebService frameworks to be used globally.

It introduces support for HTTP Request protocol. The webservice HTTP call returns by default the content of the response. A content_only parameter can be passed to get the full response object.

This addon is a simplified version of the webservice module. In most cases, you can install webservice_base instead of webservice as a drop-in replacement when you need to define a backend and perform HTTP requests from server-side code.

It comes with no additional dependencies. For the original implementation relying on component objects, see the webservice addon module.

This work is derived from that module, removing the component and server_env dependencies to make it lighter. As a design decision, using component seems to an an additional abstraction layer (and dependency) that brings little benefit, and removing it brings the implementation closer to Odoo's framework design patterns.

Main differences with webservice:

  • webservice_base focuses on direct usage from Odoo models (no component registry layer).
  • It provides a lightweight configuration model (webservice.backend) and a request adapter (webservice.request.adapter).
  • Features that depend on server_environment and component-based extensibility are not part of webservice_base.

Table of contents

To configure webservice_base:

  1. Enable developer mode.
  2. Go to Settings > Technical > WebService Backend.
  3. Create a backend record:
    • Protocol: select HTTP Request
    • URL: base URL of the remote service, for example https://api.example.com/
    • Content Type: set a default Content-Type header (optional)
    • Authentication:
      • Public: no authentication
      • Username & password: set Username and Password
      • API Key: set API Key header (for example Authorization or X-Api-Key) and API Key
  4. (Optional) Tune timeouts:
    • Connect timeout: seconds to establish the TCP connection
    • Read timeout: seconds to wait for the response

The backend record can be used directly from server-side code using the call() helper or the inherited adapter methods (get, post, put).

This module is meant to be used from server-side code.

  1. Create a webservice.backend record (see Configuration).
  2. From Python code, fetch the backend and call HTTP methods.
backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
result = backend.call("get", url="api/v1/ping")
  • If url is relative, it is joined with the backend url.
  • If url is an absolute URL, it is used as-is.
import json

backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
payload = {"name": "John"}

result = backend.call(
    "post",
    url="api/v1/contacts",
    data=json.dumps(payload),
    headers={"Accept": "application/json"},
    content_type="application/json",
)

By default, calls return response.content. To get the full requests.Response, set content_only to False:

backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
response = backend.call("get", url="api/v1/ping", content_only=False)
status_code = response.status_code
body = response.content

You can override timeouts per call using timeout (as accepted by requests):

  • A single number applies to both the connect and read timeouts.
  • A tuple (connect_timeout, read_timeout) allows setting them separately.
backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
result = backend.call("get", url="api/v1/ping", timeout=(2, 10))

If your backend URL or endpoint contains format placeholders, pass url_params:

backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
backend.url = "https://api.example.com/{version}/"
result = backend.call("get", url="resources/{resource_id}", url_params={"version": "v1", "resource_id": 42})

Bugs are tracked on GitHub Issues. In case of trouble, please check there if your issue has already been reported. If you spotted it first, help us to smash it by providing a detailed and welcomed feedback.

Do not contact contributors directly about support or help with technical issues.

  • Creu Blanca
  • Camptocamp

This module is maintained by the OCA.

Odoo Community Association

OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

Current maintainer:

etobella

This module is part of the OCA/web-api project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.