Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion endpoint/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def _handle_endpoint(self, env, model, endpoint_route, **params):
if not endpoint:
raise NotFound()
endpoint._validate_request(request)
result = endpoint._handle_request(request)
result = endpoint._handle_request(request, querystring_params=params)
return self._handle_result(result)

def _handle_result(self, result):
Expand Down
12 changes: 12 additions & 0 deletions endpoint/demo/endpoint_demo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@ result = {"payload": "Method used:" + request.httprequest.method}
</field>
</record>

<record id="endpoint_demo_8" model="endpoint.endpoint">
<field name="name">Demo Endpoint 8</field>
<field name="route">/demo/&lt;string:param1&gt;/&lt;int:param2&gt;</field>
<field name="request_method">GET</field>
<field name="exec_mode">code</field>
<field name="auth_type">public</field>
<field name="exec_as_user_id" ref="base.user_demo" />
<field name="code_snippet">
result = {"payload": querystring_params}
</field>
</record>

</odoo>
15 changes: 11 additions & 4 deletions endpoint/models/endpoint_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ def _default_code_snippet_docs(self):
'sha3_512', 'shake_128', 'shake_256', 'blake2b',
'blake2s', 'md5', 'new'
* hmac: Python 'hmac' library. Use 'new' to create HMAC objects.
* querystring_params

Must generate either an instance of ``Response`` into ``response`` var or:
Must assign a ``result`` variable, with either an instance of ``Response``,
or a dict containiny any of the keys:

* payload
* headers
Expand Down Expand Up @@ -187,10 +189,11 @@ def _code_snippet_log_func(self, message, level="info"):
),
)

def _handle_exec__code(self, request):
def _handle_exec__code(self, request, querystring_params=None):
if not self._code_snippet_valued():
return {}
eval_ctx = self._get_code_snippet_eval_context(request)
eval_ctx["querystring_params"] = querystring_params or {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need this? We have the request and as such we have request.params that contains the qs params.

snippet = self.code_snippet
safe_eval.safe_eval(snippet, eval_ctx, mode="exec", nocopy=True)
result = eval_ctx.get("result")
Expand Down Expand Up @@ -238,14 +241,18 @@ def _get_handler(self):
_("Missing handler for exec mode %s") % self.exec_mode
) from e

def _handle_request(self, request):
def _handle_request(self, request, querystring_params=None):
# Switch user for the whole process
self_with_user = self
if self.exec_as_user_id:
self_with_user = self.with_user(user=self.exec_as_user_id)
handler = self_with_user._get_handler()
try:
res = handler(request)
# In case the handler does not support params

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would simply let it fail. Or if you really want you can catch a TypeError and log it. But I would just let it break. Beside, I doubt anyone did ever implement a different handler :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be answered

if querystring_params:
res = handler(request, querystring_params=querystring_params)
else:
res = handler(request)
except self._bad_request_exceptions() as orig_exec:
self._logger.error("_validate_request: BadRequest")
raise werkzeug.exceptions.BadRequest() from orig_exec
Expand Down
4 changes: 4 additions & 0 deletions endpoint/tests/test_endpoint_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ def test_call6(self):
def test_call7(self):
response = self.url_open("/demo/bad_method", data="ok")
self.assertEqual(response.status_code, 405)

def test_call8(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand how this test is supposed to work. You should pass params in the query string no?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only used the URL path converters (string:param1/int:param2), so it never actually checked a real query string.
I will update it to test /demo/test/8?extra=1 and assert extra shows up in the merged querystring_params alongside the path params.

response = self.url_open("/demo/test/8?extra=1")
self.assertEqual(response.json(), {"param1": "test", "param2": 8, "extra": "1"})
Loading