diff --git a/base_external_system_http/README.rst b/base_external_system_http/README.rst new file mode 100644 index 000000000..8d0294ac6 --- /dev/null +++ b/base_external_system_http/README.rst @@ -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 `_. +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. + +.. 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 `__: + +|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/__init__.py b/base_external_system_http/__init__.py new file mode 100644 index 000000000..0e4efdeee --- /dev/null +++ b/base_external_system_http/__init__.py @@ -0,0 +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/__manifest__.py b/base_external_system_http/__manifest__.py new file mode 100644 index 000000000..16dfb5e1d --- /dev/null +++ b/base_external_system_http/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2023-2024 Therp BV . +# 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, +} 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 100644 index 000000000..5a0cb6aeb --- /dev/null +++ b/base_external_system_http/demo/external_system_demo.xml @@ -0,0 +1,15 @@ + + + + + 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..0f1c60580 --- /dev/null +++ b/base_external_system_http/demo/external_system_endpoint_demo.xml @@ -0,0 +1,12 @@ + + + + + + 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..176e1de84 --- /dev/null +++ b/base_external_system_http/models/__init__.py @@ -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 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..79b95887e --- /dev/null +++ b/base_external_system_http/models/external_system.py @@ -0,0 +1,16 @@ +# 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..cbab39ec4 --- /dev/null +++ b/base_external_system_http/models/external_system_adapter_http.py @@ -0,0 +1,26 @@ +# 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 + +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.""" 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..37a61c033 --- /dev/null +++ b/base_external_system_http/models/external_system_endpoint.py @@ -0,0 +1,19 @@ +# 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/models/external_system_interaction_mixin.py b/base_external_system_http/models/external_system_interaction_mixin.py new file mode 100644 index 000000000..bce0a7f35 --- /dev/null +++ b/base_external_system_http/models/external_system_interaction_mixin.py @@ -0,0 +1,117 @@ +# 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, 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) 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/readme/CONTRIBUTORS.md b/base_external_system_http/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..2a0ccaa96 --- /dev/null +++ b/base_external_system_http/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ + +* Ronald Portier (Therp BV) + diff --git a/base_external_system_http/readme/DESCRIPTION.md b/base_external_system_http/readme/DESCRIPTION.md new file mode 100644 index 000000000..39d536bbb --- /dev/null +++ b/base_external_system_http/readme/DESCRIPTION.md @@ -0,0 +1,4 @@ + +Connect to systems using http basic authentication and +enable post and get requests. + 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 000000000..4c7ab3029 Binary files /dev/null and b/base_external_system_http/static/description/icon.png differ diff --git a/base_external_system_http/static/description/index.html b/base_external_system_http/static/description/index.html new file mode 100644 index 000000000..555d15709 --- /dev/null +++ b/base_external_system_http/static/description/index.html @@ -0,0 +1,424 @@ + + + + + + +External System HTTP + + + +
+

External System HTTP

+ + +

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 new file mode 100644 index 000000000..e87b34525 --- /dev/null +++ b/base_external_system_http/tests/__init__.py @@ -0,0 +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 new file mode 100644 index 000000000..a66eb06ec --- /dev/null +++ b/base_external_system_http/tests/test_external_system.py @@ -0,0 +1,42 @@ +# 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): + @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.""" + system_type_http = self.env["external.system.adapter.http"] + self.assertIn( + (system_type_http._name, system_type_http._description), + self.env["external.system"]._get_system_types(), + ) + + def test_client(self): + """The client should be the adapter class.""" + system_type_http = self.env["external.system.adapter.http"] + with self.record.client() as client: + self.assertEqual(client, system_type_http) + # Client should have system_id property. + self.assertEqual(client.system_id, self.record) + + def test_get_http(self): + """We should be able to get response from system.""" + with self.record.client() as client: + response = client.get() + self.assertIn(". +# 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() + 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 diff --git a/base_external_system_http/views/external_system.xml b/base_external_system_http/views/external_system.xml new file mode 100644 index 000000000..1de45bfc2 --- /dev/null +++ b/base_external_system_http/views/external_system.xml @@ -0,0 +1,20 @@ + + + + external.system + + + + + + + + + + + + + + + +