Skip to content

Commit e310be4

Browse files
committed
Add an HTTP endpoint and an always true token
1 parent 162e618 commit e310be4

5 files changed

Lines changed: 75 additions & 12 deletions

File tree

base_web_hook/controllers/main.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,22 @@
88
class WebHookController(http.Controller):
99

1010
@http.route(
11-
['/base_web_hook/<string:slug>'],
11+
['/base_web_hook/json/<string:slug>.json'],
1212
type='json',
1313
auth='none',
1414
)
15-
def receive(self, slug, **kwargs):
15+
def json_receive(self, *args, **kwargs):
16+
return self._receive(*args, **kwargs)
17+
18+
@http.route(
19+
['/base_web_hook/<string:slug>'],
20+
type='http',
21+
auth='none',
22+
)
23+
def http_receive(self, *args, **kwargs):
24+
return self._receive(*args, **kwargs)
25+
26+
def _receive(self, slug, **kwargs):
1627
hook = self.env['web.hook'].search_by_slug(slug)
1728
return hook.receive(
1829
data=kwargs,

base_web_hook/models/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
# -*- coding: utf-8 -*-
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
33

4+
# Concrete models
45
from . import web_hook
5-
from . import web_hook_adapter
66
from . import web_hook_token
7+
8+
# Adapters
9+
from . import web_hook_adapter
710
from . import web_hook_token_adapter
11+
12+
# Token Interfaces
13+
from . import web_hook_token_none
814
from . import web_hook_token_plain
915
from . import web_hook_token_user
16+
17+
# Request Bin Hook
18+
from . import web_hook_request_bin
19+
from . import web_hook_request_bin_request

base_web_hook/models/web_hook.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,31 @@ class WebHook(models.Model):
3737
selection='_get_interface_types',
3838
required=True,
3939
)
40-
uri_path = fields.Char(
41-
help='This is the URI path that is used to call the web hook.',
40+
uri_path_json = fields.Char(
41+
help='This is the URI path that is used to call the web hook with '
42+
'a JSON request.',
4243
compute='_compute_uri_path',
4344
store=True,
4445
readonly=True,
4546
)
46-
uri = fields.Char(
47-
help='This is the URI that is used to call the web hook externally.',
47+
uri_path_http = fields.Char(
48+
help='This is the URI path that is used to call the web hook with '
49+
'a form encoded request.',
50+
compute='_compute_uri_path',
51+
store=True,
52+
readonly=True,
53+
)
54+
uri_json = fields.Char(
55+
string='JSON Endpoint',
56+
help='This is the URI that is used to call the web hook externally. '
57+
'This endpoint only accepts requests with a JSON mime-type.',
58+
compute='_compute_uri',
59+
)
60+
uri_http = fields.Char(
61+
string='Form-Encoded Endpoint',
62+
help='This is the URI that is used to call the web hook externally. '
63+
'This endpoint should be used with requests that are form '
64+
'encoded, not JSON.',
4865
compute='_compute_uri',
4966
)
5067
token_id = fields.Many2one(
@@ -85,14 +102,18 @@ def _compute_uri_path(self):
85102
# Do not compute slug until saved
86103
continue
87104
name = slugify(record.name or '').strip().strip('-')
88-
record.uri_path = '/base_web_hook/%s-%d' % (name, record.id)
105+
record.uri_path_json = '/base_web_hook/%s-%d.json' % (
106+
name, record.id,
107+
)
108+
record.uri_path_http = '/base_web_hook/%s-%d' % (name, record.id)
89109

90110
@api.multi
91-
@api.depends('uri_path')
111+
@api.depends('uri_path_http', 'uri_path_json')
92112
def _compute_uri(self):
93113
base_uri = self.env['ir.config_parameter'].get_param('web.base.url')
94114
for record in self.filtered(lambda r: r.uri_path):
95-
record.uri = '%s%s' % (base_uri, record.uri_path)
115+
record.uri_json = '%s%s' % (base_uri, record.uri_path_json)
116+
record.uri_http = '%s%s' % (base_uri, record.uri_path_http)
96117

97118
@api.model
98119
def _get_token_types(self):
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2017 LasLabs Inc.
3+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
4+
5+
from odoo import api, models
6+
7+
8+
class WebHookTokenNone(models.Model):
9+
"""This is a token that will validate under all circumstances."""
10+
11+
_name = 'web.hook.token.none'
12+
_inherit = 'web.hook.token.adapter'
13+
_description = 'Web Hook Token - None'
14+
15+
@api.multi
16+
def validate(self, token_string, *_, **__):
17+
"""Return ``True`` if the received token is the same as configured.
18+
"""
19+
return True

base_web_hook/views/web_hook_view.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
<field name="token_type" />
2020
<field name="token_secret" />
2121
<field name="company_id" />
22-
<field name="uri" />
22+
<field name="uri_http" />
23+
<field name="uri_json" />
2324
</group>
2425
</sheet>
2526

@@ -34,7 +35,8 @@
3435
<tree string="Web Hooks">
3536
<field name="name" />
3637
<field name="interface_type" />
37-
<field name="uri" />
38+
<field name="uri_http" />
39+
<field name="uri_json" />
3840
</tree>
3941
</field>
4042
</record>

0 commit comments

Comments
 (0)