From 4e2bdc8e0f80b4ee58ad331680cab508aa32ef2a Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Mon, 30 Oct 2023 17:31:10 +0100 Subject: [PATCH 01/12] [ADD] base_external_system_http: new module --- base_external_system_http/README.rst | 9 ++ base_external_system_http/__init__.py | 2 + base_external_system_http/__manifest__.py | 20 ++++ .../demo/external_system_demo.xml | 17 +++ .../demo/external_system_endpoint_demo.xml | 14 +++ base_external_system_http/models/__init__.py | 4 + .../models/external_system.py | 17 +++ .../models/external_system_adapter_http.py | 101 ++++++++++++++++++ .../models/external_system_endpoint.py | 20 ++++ .../security/ir.model.access.csv | 3 + .../static/description/icon.png | Bin 0 -> 1142 bytes base_external_system_http/tests/__init__.py | 3 + .../tests/test_external_system.py | 43 ++++++++ .../views/external_system.xml | 22 ++++ 14 files changed, 275 insertions(+) create mode 100644 base_external_system_http/README.rst create mode 100644 base_external_system_http/__init__.py create mode 100644 base_external_system_http/__manifest__.py create mode 100755 base_external_system_http/demo/external_system_demo.xml create mode 100644 base_external_system_http/demo/external_system_endpoint_demo.xml create mode 100644 base_external_system_http/models/__init__.py create mode 100644 base_external_system_http/models/external_system.py create mode 100644 base_external_system_http/models/external_system_adapter_http.py create mode 100644 base_external_system_http/models/external_system_endpoint.py create mode 100644 base_external_system_http/security/ir.model.access.csv create mode 100644 base_external_system_http/static/description/icon.png create mode 100644 base_external_system_http/tests/__init__.py create mode 100644 base_external_system_http/tests/test_external_system.py create mode 100644 base_external_system_http/views/external_system.xml diff --git a/base_external_system_http/README.rst b/base_external_system_http/README.rst new file mode 100644 index 000000000..ad3191994 --- /dev/null +++ b/base_external_system_http/README.rst @@ -0,0 +1,9 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: https://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +================== +Hc external system +================== + +A connection to external systems for Human Company. diff --git a/base_external_system_http/__init__.py b/base_external_system_http/__init__.py new file mode 100644 index 000000000..c32fd62b7 --- /dev/null +++ b/base_external_system_http/__init__.py @@ -0,0 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +from . import models diff --git a/base_external_system_http/__manifest__.py b/base_external_system_http/__manifest__.py new file mode 100644 index 000000000..09b87a745 --- /dev/null +++ b/base_external_system_http/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2023 Therp BV . +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "External System HTTP", + "version": "10.0.1.0.0", + "author": "Therp BV, Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Base", + "website": "https://github.com/OCA/server-tools", + "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, +} diff --git a/base_external_system_http/demo/external_system_demo.xml b/base_external_system_http/demo/external_system_demo.xml new file mode 100755 index 000000000..e851931a3 --- /dev/null +++ b/base_external_system_http/demo/external_system_demo.xml @@ -0,0 +1,17 @@ + + + + + + Example Connection to GitHub for Testing + external.system.adapter.http + https + github.com + /OCA + + + + diff --git a/base_external_system_http/demo/external_system_endpoint_demo.xml b/base_external_system_http/demo/external_system_endpoint_demo.xml new file mode 100644 index 000000000..e6e5a664c --- /dev/null +++ b/base_external_system_http/demo/external_system_endpoint_demo.xml @@ -0,0 +1,14 @@ + + + + + + + tools + /server-tools + + + diff --git a/base_external_system_http/models/__init__.py b/base_external_system_http/models/__init__.py new file mode 100644 index 000000000..f2f6d84ba --- /dev/null +++ b/base_external_system_http/models/__init__.py @@ -0,0 +1,4 @@ +# 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_adapter_http diff --git a/base_external_system_http/models/external_system.py b/base_external_system_http/models/external_system.py new file mode 100644 index 000000000..07fbed4b7 --- /dev/null +++ b/base_external_system_http/models/external_system.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- +# Copyright 2023 Therp BV . +# 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", + ) diff --git a/base_external_system_http/models/external_system_adapter_http.py b/base_external_system_http/models/external_system_adapter_http.py new file mode 100644 index 000000000..2da76419a --- /dev/null +++ b/base_external_system_http/models/external_system_adapter_http.py @@ -0,0 +1,101 @@ +# -*- coding: utf-8 -*- +# Copyright 2023 Therp BV . +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +"""Extend base adapter model for connections through http(s).""" +import json +import logging + +import requests + +from odoo import _, api, fields, models +from odoo.exceptions import UserError + +# You must initialize logging, otherwise you'll not see debug output. +logging.basicConfig() +logging.getLogger().setLevel(logging.DEBUG) +requests_log = logging.getLogger("requests.packages.urllib3") +requests_log.setLevel(logging.DEBUG) +requests_log.propagate = True + + +_logger = logging.getLogger(__name__) # pylint: disable=invalid-name + + +class ExternalSystemAdapterHTTP(models.AbstractModel): + """HTTP external system Adapter""" + + _inherit = "external.system.adapter" + _name = "external.system.adapter.http" + _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.""" + pass + + def get(self, endpoint=None, params=None, **kwargs): + """Pass transparantly to request.get, but check response.""" + url = self._get_url(endpoint=endpoint) + _logger.debug("Will get data from %s", url) + response = requests.get(url, params=params, **kwargs) + if response.status_code != 200: + message = _("Data could not be retrieved from endpoint %s: %s") % ( + endpoint, + str(response.text), + ) + _logger.error(message) + raise UserError(message) + _logger.info( + _("Data succesfully retrieved from endpoint %s"), + endpoint, + ) + return response + + def post(self, endpoint=None, data=None, json=None, **kwargs): + """Post data to http server.""" + url = self._get_url(endpoint=endpoint) + _logger.debug("Will post data to %s", url) + response = requests.post(url, data=data, json=json, **kwargs) + if response.status_code not in (200, 201): + message = _("Data could not be pushed to endpoint %s: %s") % ( + endpoint, + str(response.text), + ) + _logger.error(message) + raise UserError(message) + + 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 %s not found on system %s") + % (endpoint, system.name) + ) + url = "%(scheme)s://%(host)s%(port)s%(remote_path)s%(endpoint)s%(url_suffix)s" % { + "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 diff --git a/base_external_system_http/models/external_system_endpoint.py b/base_external_system_http/models/external_system_endpoint.py new file mode 100644 index 000000000..6f44c6915 --- /dev/null +++ b/base_external_system_http/models/external_system_endpoint.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Copyright 2023 Therp BV . +# 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", + ) diff --git a/base_external_system_http/security/ir.model.access.csv b/base_external_system_http/security/ir.model.access.csv new file mode 100644 index 000000000..067c0f961 --- /dev/null +++ b/base_external_system_http/security/ir.model.access.csv @@ -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 diff --git a/base_external_system_http/static/description/icon.png b/base_external_system_http/static/description/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..4c7ab302908e114888446d84d3493fa726033c1f GIT binary patch literal 1142 zcmeAS@N?(olHy`uVBq!ia0vp^0U*r53?z4+XPOVBSkfJR9T^xl_H+M9WCijWi-X*q z7}lMWc?skwBzpw;GB8xBF)%c=FfjZA3N^f7U???UV0e|lz+g3lfkC`r&aOZkpuXS$ zpAc7|0+Vy!%`Sd3J@*~Rz=H@Xz@vBMNCCrhU++~OAXQKjgnPb5^?zLm6yRy4l)f7mx|d; zx?*(k%?4)UEmyi0Ecrc2=k&YFn|8nX@qd4)(saLN%zo##oL4V9SpH%8W(I{5_Kby- zneS~VhopAyb6lBS&$U5E`gKppbdV7NQIC+SE zKe2ts8LoR*Hw$jqcIEDHSU_mi4;HoUDLTgOJMIHx zO|`@|q9i4;B-JXpC>2OC7#SEE>l#?<8d`)H8e5qfSQ#5>8yHy`7 + + + + external.system + + + + + + + + + + + + + + + + + From 61f2eb730b519890af547111055e1a2ae786f649 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Wed, 29 Nov 2023 11:57:17 +0100 Subject: [PATCH 02/12] [IMP] *_system_http: streamlined logging --- .../models/external_system_adapter_http.py | 72 ++++++++++--------- 1 file changed, 39 insertions(+), 33 deletions(-) diff --git a/base_external_system_http/models/external_system_adapter_http.py b/base_external_system_http/models/external_system_adapter_http.py index 2da76419a..03592b82f 100644 --- a/base_external_system_http/models/external_system_adapter_http.py +++ b/base_external_system_http/models/external_system_adapter_http.py @@ -2,12 +2,11 @@ # Copyright 2023 Therp BV . # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). """Extend base adapter model for connections through http(s).""" -import json import logging import requests -from odoo import _, api, fields, models +from odoo import _, api, models from odoo.exceptions import UserError # You must initialize logging, otherwise you'll not see debug output. @@ -36,38 +35,20 @@ def external_get_client(self): @api.model def external_destroy_client(self, client): """If needed logout of server.""" - pass def get(self, endpoint=None, params=None, **kwargs): """Pass transparantly to request.get, but check response.""" url = self._get_url(endpoint=endpoint) _logger.debug("Will get data from %s", url) response = requests.get(url, params=params, **kwargs) - if response.status_code != 200: - message = _("Data could not be retrieved from endpoint %s: %s") % ( - endpoint, - str(response.text), - ) - _logger.error(message) - raise UserError(message) - _logger.info( - _("Data succesfully retrieved from endpoint %s"), - endpoint, - ) - return response + return self._return_checked_response(endpoint, response) def post(self, endpoint=None, data=None, json=None, **kwargs): """Post data to http server.""" url = self._get_url(endpoint=endpoint) _logger.debug("Will post data to %s", url) response = requests.post(url, data=data, json=json, **kwargs) - if response.status_code not in (200, 201): - message = _("Data could not be pushed to endpoint %s: %s") % ( - endpoint, - str(response.text), - ) - _logger.error(message) - raise UserError(message) + return self._return_checked_response(endpoint, response) def _get_url(self, endpoint=None, url_suffix=None): """Make full url for endpoint. @@ -83,19 +64,44 @@ def _get_url(self, endpoint=None, url_suffix=None): ("system_id", "=", system.id), ("name", "=", endpoint), ], - limit=1 + limit=1, ) if not endpoint_record: raise UserError( - _("Endpoint %s not found on system %s") - % (endpoint, system.name) + _("Endpoint %(endpoint)s not found on system %(system_name)s") + % { + "endpoint": endpoint, + "system_name": system.name, + } ) - url = "%(scheme)s://%(host)s%(port)s%(remote_path)s%(endpoint)s%(url_suffix)s" % { - "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 "", - } + url = ( + "%(scheme)s://%(host)s%(port)s%(remote_path)s%(endpoint)s%(url_suffix)s" + % { + "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 %s, check log") % endpoint) + _logger.info( + "Succesfull communication with endpoint %(endpoint)s", + {"endpoint": endpoint}, + ) + return response From 68b6e6d5ce58b4ff3a86ea11e5a5ee72b746bf9c Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Wed, 3 Apr 2024 11:20:46 +0200 Subject: [PATCH 03/12] [IMP] base_external_system_http: pre-commit stuff --- base_external_system_http/demo/external_system_demo.xml | 2 +- .../demo/external_system_endpoint_demo.xml | 2 +- base_external_system_http/models/external_system.py | 1 - .../models/external_system_adapter_http.py | 1 - base_external_system_http/models/external_system_endpoint.py | 1 - base_external_system_http/tests/__init__.py | 2 -- base_external_system_http/tests/test_external_system.py | 2 -- 7 files changed, 2 insertions(+), 9 deletions(-) diff --git a/base_external_system_http/demo/external_system_demo.xml b/base_external_system_http/demo/external_system_demo.xml index e851931a3..861b805ff 100755 --- a/base_external_system_http/demo/external_system_demo.xml +++ b/base_external_system_http/demo/external_system_demo.xml @@ -1,4 +1,4 @@ - + +

Beta License: AGPL-3 OCA/server-backend Translate me on Weblate Try me on Runboat

+

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

+

Table of contents

+ +
+

Bug Tracker

+

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.

+
+
+

Credits

+
+

Authors

+
    +
  • Therp BV
  • +
+
+
+

Contributors

+
    +
  • Ronald Portier (Therp BV)
  • +
+
+
+

Maintainers

+

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:

+

NL66278

+

This module is part of the OCA/server-backend project on GitHub.

+

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

+
+
+ + + diff --git a/base_external_system_http/tests/__init__.py b/base_external_system_http/tests/__init__.py index 66519dcc2..e87b34525 100644 --- a/base_external_system_http/tests/__init__.py +++ b/base_external_system_http/tests/__init__.py @@ -1 +1,2 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from . import test_external_system diff --git a/base_external_system_http/tests/test_external_system.py b/base_external_system_http/tests/test_external_system.py index 7f814509a..a66eb06ec 100644 --- a/base_external_system_http/tests/test_external_system.py +++ b/base_external_system_http/tests/test_external_system.py @@ -1,12 +1,13 @@ -# Copyright 2023 Therp BV. +# Copyright 2023-2024 Therp BV. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). from odoo.tests.common import TransactionCase class TestExternalSystem(TransactionCase): - def setUp(self): - super(TestExternalSystem, self).setUp() - self.record = self.env.ref("base_external_system_http.external_system_github") + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.record = cls.env.ref("base_external_system_http.external_system_github") def test_get_system_types(self): """It should return at least the test record's interface.""" From 1a669eb757bbbeac8452ecf12f57db9254365180 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Tue, 14 May 2024 12:40:26 +0200 Subject: [PATCH 05/12] [ADD] base_external_system_ms_client_assertion: new module --- base_external_system_http/__init__.py | 1 + base_external_system_http/models/__init__.py | 3 +- .../models/external_system_adapter_http.py | 89 +----------------- .../external_system_interaction_mixin.py | 94 +++++++++++++++++++ base_external_system_http/tools/__init__.py | 6 ++ base_external_system_http/tools/common.py | 35 +++++++ 6 files changed, 142 insertions(+), 86 deletions(-) create mode 100644 base_external_system_http/models/external_system_interaction_mixin.py create mode 100644 base_external_system_http/tools/__init__.py create mode 100644 base_external_system_http/tools/common.py diff --git a/base_external_system_http/__init__.py b/base_external_system_http/__init__.py index c32fd62b7..0e4efdeee 100644 --- a/base_external_system_http/__init__.py +++ b/base_external_system_http/__init__.py @@ -1,2 +1,3 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from . import models +from . import tools diff --git a/base_external_system_http/models/__init__.py b/base_external_system_http/models/__init__.py index f2f6d84ba..176e1de84 100644 --- a/base_external_system_http/models/__init__.py +++ b/base_external_system_http/models/__init__.py @@ -1,4 +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_adapter_http +from . import external_system_interaction_mixin +from . import external_system_adapter_http # will use mixin diff --git a/base_external_system_http/models/external_system_adapter_http.py b/base_external_system_http/models/external_system_adapter_http.py index 767fa43cb..cd122d719 100644 --- a/base_external_system_http/models/external_system_adapter_http.py +++ b/base_external_system_http/models/external_system_adapter_http.py @@ -1,29 +1,18 @@ # Copyright 2023 Therp BV . # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). """Extend base adapter model for connections through http(s).""" -import logging +from odoo import api, models -import requests +from ..tools import initialize_request_logging -from odoo import _, api, models -from odoo.exceptions import UserError - -# You must initialize logging, otherwise you'll not see debug output. -logging.basicConfig() -logging.getLogger().setLevel(logging.DEBUG) -requests_log = logging.getLogger("requests.packages.urllib3") -requests_log.setLevel(logging.DEBUG) -requests_log.propagate = True - - -_logger = logging.getLogger(__name__) # pylint: disable=invalid-name +initialize_request_logging() class ExternalSystemAdapterHTTP(models.AbstractModel): """HTTP external system Adapter""" - _inherit = "external.system.adapter" _name = "external.system.adapter.http" + _inherit = ["external.system.adapter", "external.system.interaction.mixin"] _description = __doc__ @api.model @@ -34,73 +23,3 @@ def external_get_client(self): @api.model def external_destroy_client(self, client): """If needed logout of server.""" - - def get(self, endpoint=None, params=None, **kwargs): - """Pass transparantly to request.get, but check response.""" - url = self._get_url(endpoint=endpoint) - _logger.debug("Will get data from %s", url) - response = requests.get(url, params=params, timeout=16, **kwargs) - return self._return_checked_response(endpoint, response) - - def post(self, endpoint=None, data=None, json=None, **kwargs): - """Post data to http server.""" - url = self._get_url(endpoint=endpoint) - _logger.debug("Will post data to %s", url) - response = requests.post(url, data=data, json=json, timeout=16, **kwargs) - 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)s://%(host)s%(port)s%(remote_path)s%(endpoint)s%(url_suffix)s" - % { - "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 %s, check log") % endpoint) - _logger.info( - "Succesfull communication with endpoint %(endpoint)s", - {"endpoint": endpoint}, - ) - return response diff --git a/base_external_system_http/models/external_system_interaction_mixin.py b/base_external_system_http/models/external_system_interaction_mixin.py new file mode 100644 index 000000000..5c96d8c1c --- /dev/null +++ b/base_external_system_http/models/external_system_interaction_mixin.py @@ -0,0 +1,94 @@ +# Copyright 2023 Therp BV . +# 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, **kwargs): + """Pass transparantly to request.get, but check response.""" + url = self._get_url(endpoint=endpoint) + _logger.debug("Will get data from %s", url) + response = requests.get(url, params=params, timeout=16, **kwargs) + return self._return_checked_response(endpoint, response) + + def post(self, endpoint=None, data=None, json=None, **kwargs): + """Post data to http server.""" + url = self._get_url(endpoint=endpoint) + _logger.debug("Will post data to %s", url) + response = requests.post(url, data=data, json=json, timeout=16, **kwargs) + 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)s://%(host)s%(port)s%(remote_path)s%(endpoint)s%(url_suffix)s" + % { + "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 %s, check log") % endpoint) + _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) diff --git a/base_external_system_http/tools/__init__.py b/base_external_system_http/tools/__init__.py new file mode 100644 index 000000000..2267b89dd --- /dev/null +++ b/base_external_system_http/tools/__init__.py @@ -0,0 +1,6 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from .common import ( + initialize_request_logging, + strip_empty, +) diff --git a/base_external_system_http/tools/common.py b/base_external_system_http/tools/common.py new file mode 100644 index 000000000..bb0a4716d --- /dev/null +++ b/base_external_system_http/tools/common.py @@ -0,0 +1,35 @@ +# Copyright 2023-2024 Therp BV . +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +"""Common methods to use in communication with external systems.""" + +import logging + + +def initialize_request_logging(): + """Make sure calls to request lib are logged.""" + # You must initialize logging, otherwise you'll not see debug output. + logging.basicConfig() + logging.getLogger().setLevel(logging.DEBUG) + requests_log = logging.getLogger("requests.packages.urllib3") + requests_log.setLevel(logging.DEBUG) + requests_log.propagate = True + + +def strip_empty(data): + """Strip all empty data from nested datastructure.""" + if isinstance(data, dict): + return {k: strip_empty(v) for k, v in data.items() if k and v} + if isinstance(data, list): + return [strip_empty(item) for item in data if item] + if isinstance(data, tuple): + return tuple(strip_empty(item) for item in data if item) + if isinstance(data, set): + return {strip_empty(item) for item in data if item} + return strip_empty_spaces(data) + + +def strip_empty_spaces(string): + """Remove all whitespace from end of string like item.""" + if isinstance(string, str): + return string.strip() + return string From 013546196611f29a476d27e1eb9b96432d3326c1 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Tue, 22 Oct 2024 17:01:25 +0200 Subject: [PATCH 06/12] [FIX] base_external..: should be possible to override timeout --- .../models/external_system_interaction_mixin.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base_external_system_http/models/external_system_interaction_mixin.py b/base_external_system_http/models/external_system_interaction_mixin.py index 5c96d8c1c..faf14db93 100644 --- a/base_external_system_http/models/external_system_interaction_mixin.py +++ b/base_external_system_http/models/external_system_interaction_mixin.py @@ -23,14 +23,16 @@ def get(self, endpoint=None, params=None, **kwargs): """Pass transparantly to request.get, but check response.""" url = self._get_url(endpoint=endpoint) _logger.debug("Will get data from %s", url) - response = requests.get(url, params=params, timeout=16, **kwargs) + timeout = kwargs.pop("timeout", 60) # default timeout a full minute. + response = requests.get(url, params=params, timeout=timeout, **kwargs) return self._return_checked_response(endpoint, response) def post(self, endpoint=None, data=None, json=None, **kwargs): """Post data to http server.""" url = self._get_url(endpoint=endpoint) _logger.debug("Will post data to %s", url) - response = requests.post(url, data=data, json=json, timeout=16, **kwargs) + timeout = kwargs.pop("timeout", 60) # default timeout a full minute. + response = requests.post(url, data=data, json=json, timeout=timeout, **kwargs) return self._return_checked_response(endpoint, response) def _get_url(self, endpoint=None, url_suffix=None): From b2c7034bdd8c7ec1982728f29cc43351feef670c Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Tue, 22 Oct 2024 17:03:30 +0200 Subject: [PATCH 07/12] [FIX] base_external..: prevent excessive logging --- base_external_system_http/tools/common.py | 1 - 1 file changed, 1 deletion(-) diff --git a/base_external_system_http/tools/common.py b/base_external_system_http/tools/common.py index bb0a4716d..5b975f7c4 100644 --- a/base_external_system_http/tools/common.py +++ b/base_external_system_http/tools/common.py @@ -9,7 +9,6 @@ def initialize_request_logging(): """Make sure calls to request lib are logged.""" # You must initialize logging, otherwise you'll not see debug output. logging.basicConfig() - logging.getLogger().setLevel(logging.DEBUG) requests_log = logging.getLogger("requests.packages.urllib3") requests_log.setLevel(logging.DEBUG) requests_log.propagate = True From 6522ee5a398b6199ce7b44b03e6dc3d2e0c9084c Mon Sep 17 00:00:00 2001 From: lfreeke Date: Tue, 20 Oct 2026 10:06:23 +0200 Subject: [PATCH 08/12] [IMP] base_external_system_http: pre-commit execution --- .../demo/external_system_demo.xml | 2 -- .../demo/external_system_endpoint_demo.xml | 2 -- .../models/external_system_adapter_http.py | 1 + .../external_system_interaction_mixin.py | 18 ++++++++---------- base_external_system_http/pyproject.toml | 3 +++ .../views/external_system.xml | 4 +--- 6 files changed, 13 insertions(+), 17 deletions(-) create mode 100644 base_external_system_http/pyproject.toml diff --git a/base_external_system_http/demo/external_system_demo.xml b/base_external_system_http/demo/external_system_demo.xml index 861b805ff..5a0cb6aeb 100644 --- a/base_external_system_http/demo/external_system_demo.xml +++ b/base_external_system_http/demo/external_system_demo.xml @@ -4,7 +4,6 @@ License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). --> - Example Connection to GitHub for Testing external.system.adapter.http @@ -13,5 +12,4 @@ /OCA - diff --git a/base_external_system_http/demo/external_system_endpoint_demo.xml b/base_external_system_http/demo/external_system_endpoint_demo.xml index 5f8030a8f..0f1c60580 100644 --- a/base_external_system_http/demo/external_system_endpoint_demo.xml +++ b/base_external_system_http/demo/external_system_endpoint_demo.xml @@ -4,11 +4,9 @@ License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). --> - tools /server-tools - diff --git a/base_external_system_http/models/external_system_adapter_http.py b/base_external_system_http/models/external_system_adapter_http.py index cd122d719..cbab39ec4 100644 --- a/base_external_system_http/models/external_system_adapter_http.py +++ b/base_external_system_http/models/external_system_adapter_http.py @@ -1,6 +1,7 @@ # Copyright 2023 Therp BV . # 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 diff --git a/base_external_system_http/models/external_system_interaction_mixin.py b/base_external_system_http/models/external_system_interaction_mixin.py index faf14db93..9ceeeaf59 100644 --- a/base_external_system_http/models/external_system_interaction_mixin.py +++ b/base_external_system_http/models/external_system_interaction_mixin.py @@ -1,6 +1,7 @@ # Copyright 2023 Therp BV . # 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 @@ -59,16 +60,13 @@ def _get_url(self, endpoint=None, url_suffix=None): "system_name": system.name, } ) - url = ( - "%(scheme)s://%(host)s%(port)s%(remote_path)s%(endpoint)s%(url_suffix)s" - % { - "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 "", - } + 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 diff --git a/base_external_system_http/pyproject.toml b/base_external_system_http/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/base_external_system_http/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/base_external_system_http/views/external_system.xml b/base_external_system_http/views/external_system.xml index 0961ef94b..cf62c2717 100644 --- a/base_external_system_http/views/external_system.xml +++ b/base_external_system_http/views/external_system.xml @@ -1,11 +1,10 @@ - external.system - + @@ -18,5 +17,4 @@ - From f4333076a5bb1bddf705f51573b49a8aba583686 Mon Sep 17 00:00:00 2001 From: lfreeke Date: Tue, 20 Oct 2026 10:06:23 +0200 Subject: [PATCH 09/12] [MIG] base_external_system_http: Migration to 18.0 --- base_external_system_http/__manifest__.py | 2 +- base_external_system_http/views/external_system.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/base_external_system_http/__manifest__.py b/base_external_system_http/__manifest__.py index 4ac036809..16dfb5e1d 100644 --- a/base_external_system_http/__manifest__.py +++ b/base_external_system_http/__manifest__.py @@ -2,7 +2,7 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). { "name": "External System HTTP", - "version": "16.0.1.0.0", + "version": "18.0.1.0.0", "author": "Therp BV, Odoo Community Association (OCA)", "maintainers": ["NL66278"], "license": "AGPL-3", diff --git a/base_external_system_http/views/external_system.xml b/base_external_system_http/views/external_system.xml index cf62c2717..1de45bfc2 100644 --- a/base_external_system_http/views/external_system.xml +++ b/base_external_system_http/views/external_system.xml @@ -8,10 +8,10 @@ - + - + From d0d00592bc72f0775f0c2e84672f5e7859966799 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Wed, 3 Sep 2025 13:04:43 +0200 Subject: [PATCH 10/12] [ADD] *_system_http: add put interface --- .../models/external_system_interaction_mixin.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/base_external_system_http/models/external_system_interaction_mixin.py b/base_external_system_http/models/external_system_interaction_mixin.py index 9ceeeaf59..0f9d33039 100644 --- a/base_external_system_http/models/external_system_interaction_mixin.py +++ b/base_external_system_http/models/external_system_interaction_mixin.py @@ -36,6 +36,14 @@ def post(self, endpoint=None, data=None, json=None, **kwargs): response = requests.post(url, data=data, json=json, timeout=timeout, **kwargs) return self._return_checked_response(endpoint, response) + def put(self, endpoint=None, data=None, json=None, **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) + return self._return_checked_response(endpoint, response) + def _get_url(self, endpoint=None, url_suffix=None): """Make full url for endpoint. From dc99657a974382fe3be7bbcd2d193bbc353254b4 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Wed, 11 Feb 2026 20:35:25 +0100 Subject: [PATCH 11/12] [IMP] base..http: new parameter raise_exception On get, post and put operations by default a UserError is raised for status code > 201. With this commit the caller of those methods can opt to receive the original response. --- .../models/external_system_interaction_mixin.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/base_external_system_http/models/external_system_interaction_mixin.py b/base_external_system_http/models/external_system_interaction_mixin.py index 0f9d33039..0011cd6d9 100644 --- a/base_external_system_http/models/external_system_interaction_mixin.py +++ b/base_external_system_http/models/external_system_interaction_mixin.py @@ -20,28 +20,34 @@ class ExternalSystemInteractionMixin(models.AbstractModel): _name = "external.system.interaction.mixin" _description = __doc__ - def get(self, endpoint=None, params=None, **kwargs): + 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, **kwargs): + 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, **kwargs): + 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): From 845d39d06f2be9e363034e846d11f54d1475640e Mon Sep 17 00:00:00 2001 From: lfreeke Date: Thu, 5 Nov 2026 12:45:59 +0100 Subject: [PATCH 12/12] [UPD] better error logging --- .../models/external_system_interaction_mixin.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/base_external_system_http/models/external_system_interaction_mixin.py b/base_external_system_http/models/external_system_interaction_mixin.py index 0011cd6d9..bce0a7f35 100644 --- a/base_external_system_http/models/external_system_interaction_mixin.py +++ b/base_external_system_http/models/external_system_interaction_mixin.py @@ -96,7 +96,16 @@ def _return_checked_response(self, endpoint, response): "text": str(response.text), }, ) - raise UserError(_("Communication failure with %s, check log") % endpoint) + 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},