From ff55f866ffb5d0200bae6fc7358197608369feec Mon Sep 17 00:00:00 2001 From: "robin.keunen" Date: Fri, 6 Mar 2020 18:44:24 +0100 Subject: [PATCH] [ADD] easy_my_coop_api --- easy_my_coop_api/README.rst | 69 +++ easy_my_coop_api/__init__.py | 2 + easy_my_coop_api/__manifest__.py | 20 + easy_my_coop_api/controllers/__init__.py | 1 + .../controllers/ping_controller.py | 23 + easy_my_coop_api/readme/CONTRIBUTORS.rst | 2 + easy_my_coop_api/readme/DESCRIPTION.rst | 1 + easy_my_coop_api/services/__init__.py | 1 + easy_my_coop_api/services/ping_service.py | 36 ++ .../static/description/index.html | 425 ++++++++++++++++++ easy_my_coop_api/tests/__init__.py | 2 + easy_my_coop_api/tests/common.py | 28 ++ easy_my_coop_api/tests/test_ping.py | 38 ++ easy_my_coop_api/tests/test_registry.py | 28 ++ 14 files changed, 676 insertions(+) create mode 100644 easy_my_coop_api/README.rst create mode 100644 easy_my_coop_api/__init__.py create mode 100644 easy_my_coop_api/__manifest__.py create mode 100644 easy_my_coop_api/controllers/__init__.py create mode 100644 easy_my_coop_api/controllers/ping_controller.py create mode 100644 easy_my_coop_api/readme/CONTRIBUTORS.rst create mode 100644 easy_my_coop_api/readme/DESCRIPTION.rst create mode 100644 easy_my_coop_api/services/__init__.py create mode 100644 easy_my_coop_api/services/ping_service.py create mode 100644 easy_my_coop_api/static/description/index.html create mode 100644 easy_my_coop_api/tests/__init__.py create mode 100644 easy_my_coop_api/tests/common.py create mode 100644 easy_my_coop_api/tests/test_ping.py create mode 100644 easy_my_coop_api/tests/test_registry.py diff --git a/easy_my_coop_api/README.rst b/easy_my_coop_api/README.rst new file mode 100644 index 0000000..4eb4cb6 --- /dev/null +++ b/easy_my_coop_api/README.rst @@ -0,0 +1,69 @@ +================ +Easy My Coop API +================ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-coopiteasy%2Fvertical--cooperative-lightgray.png?logo=github + :target: https://github.com/coopiteasy/vertical-cooperative/tree/12.0/easy_my_coop_api + :alt: coopiteasy/vertical-cooperative + +|badge1| |badge2| |badge3| + +Open Easy My Coop to the world: RESTful API. + +**Table of contents** + +.. contents:: + :local: + +Known issues / Roadmap +====================== + +The API should generate and use an external id for records instead +of odoo's generated id. It would make importing and export data as +well as migrating across versions easier. + +One way would be to use uuid but the default BaseRESTController +routes would need to be rewritten: they only take integer as ids. +Another way is to define a sequence per model. + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Coop IT Easy SCRLfs + +Contributors +~~~~~~~~~~~~ + +* Coop IT Easy SCRLfs +* Robin Keunen + +Maintainers +~~~~~~~~~~~ + +This module is part of the `coopiteasy/vertical-cooperative `_ project on GitHub. + +You are welcome to contribute. diff --git a/easy_my_coop_api/__init__.py b/easy_my_coop_api/__init__.py new file mode 100644 index 0000000..d6d6244 --- /dev/null +++ b/easy_my_coop_api/__init__.py @@ -0,0 +1,2 @@ +from . import controllers +from . import services diff --git a/easy_my_coop_api/__manifest__.py b/easy_my_coop_api/__manifest__.py new file mode 100644 index 0000000..54b4f4f --- /dev/null +++ b/easy_my_coop_api/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +{ + "name": "Easy My Coop API", + "version": "12.0.0.0.1", + "depends": ["base_rest", "easy_my_coop"], + "author": "Coop IT Easy SCRLfs", + "category": "Cooperative management", + "website": "www.coopiteasy.be", + "license": "AGPL-3", + "summary": """ + Open Easy My Coop to the world: RESTful API. + """, + "data": [], + "demo": [], + "installable": True, + "application": False, +} diff --git a/easy_my_coop_api/controllers/__init__.py b/easy_my_coop_api/controllers/__init__.py new file mode 100644 index 0000000..e505936 --- /dev/null +++ b/easy_my_coop_api/controllers/__init__.py @@ -0,0 +1 @@ +from . import ping_controller diff --git a/easy_my_coop_api/controllers/ping_controller.py b/easy_my_coop_api/controllers/ping_controller.py new file mode 100644 index 0000000..90e8d06 --- /dev/null +++ b/easy_my_coop_api/controllers/ping_controller.py @@ -0,0 +1,23 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + + +from odoo.addons.base_rest.controllers import main +from odoo.http import route + + +class PingController(main.RestController): + _root_path = "/api/" + _collection_name = "emc.services" + _default_auth = "public" + + @route( + _root_path + "/ping", + methods=["GET"], + csrf=False, + ) + def test(self, _service_name, _id=None, **params): + return self._process_method( + _service_name, "ping", _id=_id, params=params + ) diff --git a/easy_my_coop_api/readme/CONTRIBUTORS.rst b/easy_my_coop_api/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000..f8672e6 --- /dev/null +++ b/easy_my_coop_api/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Coop IT Easy SCRLfs +* Robin Keunen diff --git a/easy_my_coop_api/readme/DESCRIPTION.rst b/easy_my_coop_api/readme/DESCRIPTION.rst new file mode 100644 index 0000000..8a4c837 --- /dev/null +++ b/easy_my_coop_api/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Open Easy My Coop to the world: RESTful API. diff --git a/easy_my_coop_api/services/__init__.py b/easy_my_coop_api/services/__init__.py new file mode 100644 index 0000000..8d8dcb6 --- /dev/null +++ b/easy_my_coop_api/services/__init__.py @@ -0,0 +1 @@ +from . import ping_service diff --git a/easy_my_coop_api/services/ping_service.py b/easy_my_coop_api/services/ping_service.py new file mode 100644 index 0000000..823033e --- /dev/null +++ b/easy_my_coop_api/services/ping_service.py @@ -0,0 +1,36 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + + +from odoo.addons.component.core import Component +from odoo import _ + + +class PingService(Component): + _inherit = "base.rest.service" + _name = "emc.services" + # _name = "ping.services" + _usage = "ping" # service_name + _collection = "emc.services" + _description = """ + Ping services (test the api) + """ + + def ping(self): + return {"message": _("Called ping on ping API")} + + def search(self): + return {"message": _("Called search on ping API")} + + def _validator_ping(self): + return {} + + def _validator_return_ping(self): + return {"message": {"type": "string"}} + + def _validator_search(self): + return {} + + def _validator_return_search(self): + return {"message": {"type": "string"}} diff --git a/easy_my_coop_api/static/description/index.html b/easy_my_coop_api/static/description/index.html new file mode 100644 index 0000000..bc922dd --- /dev/null +++ b/easy_my_coop_api/static/description/index.html @@ -0,0 +1,425 @@ + + + + + + +Easy My Coop API + + + +
+

Easy My Coop API

+ + +

Beta License: AGPL-3 coopiteasy/vertical-cooperative

+

Open Easy My Coop to the world: RESTful API.

+

Table of contents

+ +
+

Known issues / Roadmap

+

The API should generate and use an external id for records instead +of odoo’s generated id. It would make importing and export data as +well as migrating across versions easier.

+

One way would be to use uuid but the default BaseRESTController +routes would need to be rewritten: they only take integer as ids. +Another way is to define a sequence per model.

+
+
+

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 smashing it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Coop IT Easy SCRLfs
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is part of the coopiteasy/vertical-cooperative project on GitHub.

+

You are welcome to contribute.

+
+
+
+ + diff --git a/easy_my_coop_api/tests/__init__.py b/easy_my_coop_api/tests/__init__.py new file mode 100644 index 0000000..99cc5b3 --- /dev/null +++ b/easy_my_coop_api/tests/__init__.py @@ -0,0 +1,2 @@ +from . import test_ping +from . import test_registry diff --git a/easy_my_coop_api/tests/common.py b/easy_my_coop_api/tests/common.py new file mode 100644 index 0000000..d755ead --- /dev/null +++ b/easy_my_coop_api/tests/common.py @@ -0,0 +1,28 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + + +import requests +import odoo + +from odoo.addons.base_rest.tests.common import BaseRestCase + +HOST = "127.0.0.1" +PORT = odoo.tools.config["http_port"] + + +class BaseEMCRestCase(BaseRestCase): + def setUp(self): + super().setUp() + self.session = requests.Session() + + def http_get(self, url): + if url.startswith("/"): + url = "http://%s:%s%s" % (HOST, PORT, url) + return self.session.get(url) + + def http_post(self, url, data): + if url.startswith("/"): + url = "http://%s:%s%s" % (HOST, PORT, url) + return self.session.post(url, data=data) diff --git a/easy_my_coop_api/tests/test_ping.py b/easy_my_coop_api/tests/test_ping.py new file mode 100644 index 0000000..2389d22 --- /dev/null +++ b/easy_my_coop_api/tests/test_ping.py @@ -0,0 +1,38 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + + +import json + +from odoo.addons.base_rest.controllers.main import _PseudoCollection +from odoo.addons.component.core import WorkContext + +from .common import BaseEMCRestCase + + +class TestPing(BaseEMCRestCase): + def test_ping_service(self): + collection = _PseudoCollection("emc.services", self.env) + emc_services_env = WorkContext( + model_name="rest.service.registration", collection=collection + ) + + service = emc_services_env.component(usage="ping") + result = service.ping() + + self.assertTrue("message" in result) + + def test_ping_route(self): + response = self.http_get("/api/ping/ping") + self.assertEquals(response.status_code, 200) + + content = json.loads(response.content) + self.assertTrue("message" in content) + + def test_search_route(self): + response = self.http_get("/api/ping") + self.assertEquals(response.status_code, 200) + + content = json.loads(response.content) + self.assertTrue("message" in content) diff --git a/easy_my_coop_api/tests/test_registry.py b/easy_my_coop_api/tests/test_registry.py new file mode 100644 index 0000000..c31df1a --- /dev/null +++ b/easy_my_coop_api/tests/test_registry.py @@ -0,0 +1,28 @@ +# Copyright 2020 Coop IT Easy SCRL fs +# Robin Keunen +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + + +import odoo + +from odoo.http import controllers_per_module +from odoo.addons.base_rest.tests.common import BaseRestCase + +from ..controllers.ping_controller import PingController + +HOST = "127.0.0.1" +PORT = odoo.tools.config["http_port"] + + +class TestControllerRegistry(BaseRestCase): + def test_controller_registry(self): + controllers = controllers_per_module["easy_my_coop_api"] + self.assertEqual(len(controllers), 1) + + self.assertIn( + ( + "odoo.addons.easy_my_coop_api.controllers.ping_controller.PingController", + PingController, + ), + controllers, + )