Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 230 additions & 0 deletions webservice_base/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
===============
WebService Base
===============

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:ad7f19cee6c43ff3b4e9901cd1fd7be8b4ec5b0c5a73c7fad30497bdb0f27037
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
:alt: Production/Stable
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb--api-lightgray.png?logo=github
:target: https://github.com/OCA/web-api/tree/18.0/webservice_base
:alt: OCA/web-api
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/web-api-18-0/web-api-18-0-webservice_base
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/web-api&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

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**

.. contents::
:local:

Configuration
=============

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``).

Usage
=====

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

Basic usage
===========

1. Create a ``webservice.backend`` record (see *Configuration*).
2. From Python code, fetch the backend and call HTTP methods.

Example: simple GET
-------------------

.. code:: python

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.

Example: POST JSON payload
--------------------------

.. code:: python

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

Getting the full ``requests.Response``
--------------------------------------

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

.. code:: python

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

Timeouts
--------

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.

.. code:: python

backend = self.env["webservice.backend"].get_by_tech_name("demo_ws")
result = backend.call("get", url="api/v1/ping", timeout=(2, 10))

URL formatting with parameters
------------------------------

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

.. code:: python

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})

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/web-api/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 <https://github.com/OCA/web-api/issues/new?body=module:%20webservice_base%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
-------

* Creu Blanca
* Camptocamp

Contributors
------------

- Enric Tobella <etobella@creublanca.es>
- Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
- Daniel Reis <dreis@opensourceintegrators.com>

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

.. |maintainer-etobella| image:: https://github.com/etobella.png?size=40px
:target: https://github.com/etobella
:alt: etobella

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-etobella|

This module is part of the `OCA/web-api <https://github.com/OCA/web-api/tree/18.0/webservice_base>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions webservice_base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions webservice_base/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2020 Creu Blanca
# Copyright 2022 Camptocamp SA
# @author Simone Orsi <simahawk@gmail.com>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

{
"name": "WebService Base",
"summary": """Defines webservice abstract definition to be used generally""",
"version": "18.0.1.1.0",
"license": "LGPL-3",
"development_status": "Production/Stable",
"maintainers": ["etobella"],
"author": "Creu Blanca, Camptocamp, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/web-api",
"depends": ["base"],
"external_dependencies": {"python": ["requests-oauthlib", "oauthlib", "responses"]},
"data": [
"security/ir.model.access.csv",
"security/ir_rule.xml",
"views/webservice_backend.xml",
],
"demo": [],
}
Loading