Skip to content

Commit f4f762d

Browse files
committed
Merge PR #117 into 19.0
Signed-off-by simahawk
2 parents 059b7d7 + 509964e commit f4f762d

31 files changed

Lines changed: 2947 additions & 0 deletions

endpoint_route_handler/README.rst

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
.. image:: https://odoo-community.org/readme-banner-image
2+
:target: https://odoo-community.org/get-involved?utm_source=readme
3+
:alt: Odoo Community Association
4+
5+
======================
6+
Endpoint route handler
7+
======================
8+
9+
..
10+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
11+
!! This file is generated by oca-gen-addon-readme !!
12+
!! changes will be overwritten. !!
13+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14+
!! source digest: sha256:b9dd15b039dbf78d7a01947b6f273b1487a97ce0911b41b0b5652b2dbbb38456
15+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
16+
17+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
18+
:target: https://odoo-community.org/page/development-status
19+
:alt: Beta
20+
.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png
21+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
22+
:alt: License: LGPL-3
23+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb--api-lightgray.png?logo=github
24+
:target: https://github.com/OCA/web-api/tree/19.0/endpoint_route_handler
25+
:alt: OCA/web-api
26+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
27+
:target: https://translation.odoo-community.org/projects/web-api-19-0/web-api-19-0-endpoint_route_handler
28+
:alt: Translate me on Weblate
29+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
30+
:target: https://runboat.odoo-community.org/builds?repo=OCA/web-api&target_branch=19.0
31+
:alt: Try me on Runboat
32+
33+
|badge1| |badge2| |badge3| |badge4| |badge5|
34+
35+
Technical module that provides a base handler for adding and removing
36+
controller routes on the fly.
37+
38+
Can be used as a mixin or as a tool.
39+
40+
**Table of contents**
41+
42+
.. contents::
43+
:local:
44+
45+
Usage
46+
=====
47+
48+
As a mixin
49+
----------
50+
51+
Use standard Odoo inheritance:
52+
53+
::
54+
55+
class MyModel(models.Model):
56+
_name = "my.model"
57+
_inherit = "endpoint.route.handler"
58+
59+
Once you have this, each my.model record will generate a route. You can
60+
have a look at the endpoint module to see a real life example.
61+
62+
The options of the routing rules are defined by the method
63+
\_default_endpoint_options. Here's an example from the endpoint module:
64+
65+
::
66+
67+
def _default_endpoint_options_handler(self):
68+
return {
69+
"klass_dotted_path": "odoo.addons.endpoint.controllers.main.EndpointController",
70+
"method_name": "auto_endpoint",
71+
"default_pargs": (self.route,),
72+
}
73+
74+
As you can see, you have to pass the references to the controller class
75+
and the method to use when the endpoint is called. And you can prepare
76+
some default arguments to pass. In this case, the route of the current
77+
record.
78+
79+
As a tool
80+
---------
81+
82+
Initialize non stored route handlers and generate routes from them. For
83+
instance:
84+
85+
::
86+
87+
route_handler = self.env["endpoint.route.handler.tool"]
88+
endpoint_handler = MyController()._my_handler
89+
vals = {
90+
"name": "My custom route",
91+
"route": "/my/custom/route",
92+
"request_method": "GET",
93+
"auth_type": "public",
94+
}
95+
new_route = route_handler.new(vals)
96+
new_route._register_controller()
97+
98+
You can override options and define - for instance - a different
99+
controller method:
100+
101+
::
102+
103+
options = {
104+
"handler": {
105+
"klass_dotted_path": "odoo.addons.my_module.controllers.SpecialController",
106+
"method_name": "my_special_handler",
107+
}
108+
}
109+
new_route._register_controller(options=options)
110+
111+
Of course, what happens when the endpoint gets called depends on the
112+
logic defined on the controller method.
113+
114+
In both cases (mixin and tool) when a new route is generated or an
115+
existing one is updated, the ir.http.routing_map (which holds all Odoo
116+
controllers) will be updated.
117+
118+
You can see a real life example on shopfloor.app model.
119+
120+
Known issues / Roadmap
121+
======================
122+
123+
- add api docs helpers
124+
125+
- allow multiple HTTP methods on the same endpoint
126+
127+
- multiple values for route and methods
128+
129+
keep the same in the ui for now, later own we can imagine a
130+
multi-value selection or just add text field w/ proper validation
131+
and cleanup
132+
133+
remove the route field in the table of endpoint_route
134+
135+
support a comma separated list of routes maybe support comma
136+
separated list of methods use only routing.routes for generating
137+
the rule sort and freeze its values to update the endpoint hash
138+
139+
catch dup route exception on the sync to detect duplicated routes
140+
and use the endpoint_hash to retrieve the real record (note: we
141+
could store more info in the routing information which will stay
142+
in the map)
143+
144+
for customizing the rule behavior the endpoint the hook is to
145+
override the registry lookup
146+
147+
make EndpointRule class overridable on the registry
148+
149+
NOTE in v16 we won't care anymore about odoo controller so the lookup of
150+
the controller can be simplified to a basic py obj that holds the
151+
routing info.
152+
153+
Bug Tracker
154+
===========
155+
156+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/web-api/issues>`_.
157+
In case of trouble, please check there if your issue has already been reported.
158+
If you spotted it first, help us to smash it by providing a detailed and welcomed
159+
`feedback <https://github.com/OCA/web-api/issues/new?body=module:%20endpoint_route_handler%0Aversion:%2019.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
160+
161+
Do not contact contributors directly about support or help with technical issues.
162+
163+
Credits
164+
=======
165+
166+
Authors
167+
-------
168+
169+
* Camptocamp
170+
171+
Contributors
172+
------------
173+
174+
- Simone Orsi <simone.orsi@camptocamp.com>
175+
- Nguyen Minh Chien <chien@trobz.com>
176+
177+
Maintainers
178+
-----------
179+
180+
This module is maintained by the OCA.
181+
182+
.. image:: https://odoo-community.org/logo.png
183+
:alt: Odoo Community Association
184+
:target: https://odoo-community.org
185+
186+
OCA, or the Odoo Community Association, is a nonprofit organization whose
187+
mission is to support the collaborative development of Odoo features and
188+
promote its widespread use.
189+
190+
.. |maintainer-simahawk| image:: https://github.com/simahawk.png?size=40px
191+
:target: https://github.com/simahawk
192+
:alt: simahawk
193+
194+
Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
195+
196+
|maintainer-simahawk|
197+
198+
This module is part of the `OCA/web-api <https://github.com/OCA/web-api/tree/19.0/endpoint_route_handler>`_ project on GitHub.
199+
200+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

endpoint_route_handler/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from . import models
2+
from . import wizard
3+
from .post_init_hook import post_init_hook
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2021 Camptocamp SA
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
{
5+
"name": "Endpoint route handler",
6+
"summary": """Provide mixin and tool to generate custom endpoints on the fly.""",
7+
"version": "19.0.1.0.0",
8+
"license": "LGPL-3",
9+
"development_status": "Beta",
10+
"author": "Camptocamp,Odoo Community Association (OCA)",
11+
"maintainers": ["simahawk"],
12+
"website": "https://github.com/OCA/web-api",
13+
"data": [
14+
"security/ir.model.access.csv",
15+
],
16+
"post_init_hook": "post_init_hook",
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import main
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2021 Camptocamp SA
2+
# @author: Simone Orsi <simone.orsi@camptocamp.com>
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
6+
import logging
7+
8+
from werkzeug.exceptions import NotFound
9+
10+
from odoo import http
11+
12+
_logger = logging.getLogger(__file__)
13+
14+
15+
class EndpointNotFoundController(http.Controller):
16+
def auto_not_found(self, endpoint_route, **params):
17+
_logger.error("Non registered endpoint for %s", endpoint_route)
18+
raise NotFound()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2022 Camptocamp SA
2+
# @author: Simone Orsi <simone.orsi@camptocamp.com>
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
6+
class EndpointHandlerNotFound(Exception):
7+
"""Raise when an endpoint handler is not found."""

0 commit comments

Comments
 (0)