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
85 changes: 85 additions & 0 deletions base_external_system_http/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
====================
External System HTTP
====================

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

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--backend-lightgray.png?logo=github
:target: https://github.com/OCA/server-backend/tree/16.0/base_external_system_http
:alt: OCA/server-backend
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-backend-16-0/server-backend-16-0-base_external_system_http
: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/server-backend&target_branch=16.0
:alt: Try me on Runboat

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

Connect to systems using http basic authentication and enable post and
get requests.

**Table of contents**

.. contents::
:local:

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-backend/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/server-backend/issues/new?body=module:%20base_external_system_http%0Aversion:%2016.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
-------

* Therp BV

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

- Ronald Portier (Therp BV)

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-NL66278| image:: https://github.com/NL66278.png?size=40px
:target: https://github.com/NL66278
:alt: NL66278

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

|maintainer-NL66278|

This module is part of the `OCA/server-backend <https://github.com/OCA/server-backend/tree/16.0/base_external_system_http>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions base_external_system_http/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import models
from . import tools
21 changes: 21 additions & 0 deletions base_external_system_http/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2023-2024 Therp BV <https://therp.nl>.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "External System HTTP",
"version": "18.0.1.0.0",
"author": "Therp BV, Odoo Community Association (OCA)",
"maintainers": ["NL66278"],
"license": "AGPL-3",
"category": "Base",
"website": "https://github.com/OCA/server-backend",
"summary": "HTTP Connector for External Systems",
"depends": ["base_external_system"],
"data": [
"demo/external_system_demo.xml",
"demo/external_system_endpoint_demo.xml",
"security/ir.model.access.csv",
"views/external_system.xml",
],
"installable": True,
"application": False,
}
15 changes: 15 additions & 0 deletions base_external_system_http/demo/external_system_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2023 Therp BV.
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
<odoo>
<record id="external_system_github" model="external.system">
<field name="name">Example Connection to GitHub for Testing</field>
<field name="system_type">external.system.adapter.http</field>
<field name="scheme">https</field>
<field name="host">github.com</field>
<field name="remote_path">/OCA</field>
<field name="company_ids" eval="[(5, 0), (4, ref('base.main_company'))]" />
</record>
</odoo>
12 changes: 12 additions & 0 deletions base_external_system_http/demo/external_system_endpoint_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2023 Therp BV.
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
<odoo>
<record id="external_system_github_server_tools" model="external.system.endpoint">
<field name="system_id" ref="external_system_github" />
<field name="name">tools</field>
<field name="endpoint">/server-tools</field>
</record>
</odoo>
5 changes: 5 additions & 0 deletions base_external_system_http/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import external_system_endpoint # Must be before external_system!
from . import external_system
from . import external_system_interaction_mixin
from . import external_system_adapter_http # will use mixin
16 changes: 16 additions & 0 deletions base_external_system_http/models/external_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2023 Therp BV <https://therp.nl>.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ExternalSystem(models.Model):
"""Extend base external system"""

_inherit = "external.system"

endpoint_ids = fields.One2many(
comodel_name="external.system.endpoint",
inverse_name="system_id",
help="Endpoints on remote system",
)
26 changes: 26 additions & 0 deletions base_external_system_http/models/external_system_adapter_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 Therp BV <https://therp.nl>.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
"""Extend base adapter model for connections through http(s)."""

from odoo import api, models

from ..tools import initialize_request_logging

initialize_request_logging()


class ExternalSystemAdapterHTTP(models.AbstractModel):
"""HTTP external system Adapter"""

_name = "external.system.adapter.http"
_inherit = ["external.system.adapter", "external.system.interaction.mixin"]
_description = __doc__

@api.model
def external_get_client(self):
"""Return self as the client."""
return self

@api.model
def external_destroy_client(self, client):
"""If needed logout of server."""
19 changes: 19 additions & 0 deletions base_external_system_http/models/external_system_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 Therp BV <https://therp.nl>.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
"""Endpoint on remote system to get from or post to."""

from odoo import fields, models


class ExternalSystemEndpoint(models.Model):
"""Endpoint on remote system to get from or post to."""

_name = "external.system.endpoint"
_description = __doc__

system_id = fields.Many2one(comodel_name="external.system", required=True)
name = fields.Char(string="Logical name for endpoint", required=True)
endpoint = fields.Char(
required=True,
help="Remote path to append to base url for endpoint",
)
117 changes: 117 additions & 0 deletions base_external_system_http/models/external_system_interaction_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Copyright 2023 Therp BV <https://therp.nl>.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
"""Extend base adapter model for connections through http(s)."""

import logging

import requests

from odoo import _, models
from odoo.exceptions import UserError

from ..tools import strip_empty

_logger = logging.getLogger(__name__) # pylint: disable=invalid-name


class ExternalSystemInteractionMixin(models.AbstractModel):
"""Methods to interact with http based systems"""

_name = "external.system.interaction.mixin"
_description = __doc__

def get(self, endpoint=None, params=None, raise_exception=True, **kwargs):
"""Pass transparantly to request.get, but check response."""
url = self._get_url(endpoint=endpoint)
_logger.debug("Will get data from %s", url)
timeout = kwargs.pop("timeout", 60) # default timeout a full minute.
response = requests.get(url, params=params, timeout=timeout, **kwargs)
if not raise_exception:
return response
return self._return_checked_response(endpoint, response)

def post(self, endpoint=None, data=None, json=None, raise_exception=True, **kwargs):
"""Post data to http server."""
url = self._get_url(endpoint=endpoint)
_logger.debug("Will post data to %s", url)
timeout = kwargs.pop("timeout", 60) # default timeout a full minute.
response = requests.post(url, data=data, json=json, timeout=timeout, **kwargs)
if not raise_exception:
return response
return self._return_checked_response(endpoint, response)

def put(self, endpoint=None, data=None, json=None, raise_exception=True, **kwargs):
"""Post data to http server."""
url = self._get_url(endpoint=endpoint)
_logger.debug("Will post data to %s", url)
timeout = kwargs.pop("timeout", 60) # default timeout a full minute.
response = requests.put(url, data=data, json=json, timeout=timeout, **kwargs)
if not raise_exception:
return response
return self._return_checked_response(endpoint, response)

def _get_url(self, endpoint=None, url_suffix=None):
"""Make full url for endpoint.

The configured remote_path, endpoint and the passed url_suffix
must, if used, always start with "/".
"""
system = self.system_id
if endpoint:
endpoint_model = self.env["external.system.endpoint"]
endpoint_record = endpoint_model.search(
[
("system_id", "=", system.id),
("name", "=", endpoint),
],
limit=1,
)
if not endpoint_record:
raise UserError(
_("Endpoint %(endpoint)s not found on system %(system_name)s")
% {
"endpoint": endpoint,
"system_name": system.name,
}
)
url = "{scheme}://{host}{port}{remote_path}{endpoint}{url_suffix}".format(
scheme=system.scheme or "https",
host=system.host,
port=":" + str(system.port) if system.port else "",
remote_path=system.remote_path if system.remote_path else "",
endpoint=endpoint_record.endpoint if endpoint else "",
url_suffix=url_suffix if url_suffix else "",
)
return url

def _return_checked_response(self, endpoint, response):
"""For response statuscodes > 201, log error and raise exception."""
if response.status_code > 201:
_logger.error(
"Got response with statuscode %(status)s"
" from endpoint %(endpoint)s: %(text)s",
{
"status": str(response.status_code),
"endpoint": endpoint,
"text": str(response.text),
},
)
raise UserError(
_(
"Communication failure with %(endpoint)s.\n"
"Status code: %(status)s\n"
"Response: %(text)s",
endpoint=endpoint,
status=response.status_code,
text=response.text[:1000],
)
)
_logger.info(
"Succesfull communication with endpoint %(endpoint)s",
{"endpoint": endpoint},
)
return response

def strip_empty_data(self, data):
""" "Utility method that can be called on adapter to cleanup data."""
return strip_empty(data)
3 changes: 3 additions & 0 deletions base_external_system_http/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
3 changes: 3 additions & 0 deletions base_external_system_http/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

* Ronald Portier (Therp BV)

4 changes: 4 additions & 0 deletions base_external_system_http/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

Connect to systems using http basic authentication and
enable post and get requests.

3 changes: 3 additions & 0 deletions base_external_system_http/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_external_system_endpoint_admin,access_external_system_endpoint_admin,model_external_system_endpoint,base.group_system,1,1,1,1
access_external_system_endpoint_user,access_external_system_endpoint_user,model_external_system_endpoint,base.group_user,1,0,0,0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading