diff --git a/endpoint/controllers/main.py b/endpoint/controllers/main.py index afae8a10..d3a557fd 100644 --- a/endpoint/controllers/main.py +++ b/endpoint/controllers/main.py @@ -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, params=params) return self._handle_result(result) def _handle_result(self, result): diff --git a/endpoint/models/endpoint_mixin.py b/endpoint/models/endpoint_mixin.py index ebbd321d..e76f0050 100644 --- a/endpoint/models/endpoint_mixin.py +++ b/endpoint/models/endpoint_mixin.py @@ -74,8 +74,10 @@ def _default_code_snippet_docs(self): * Response * werkzeug * exceptions + * 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 @@ -154,10 +156,11 @@ def _code_snippet_log_func(self, message, level="info"): ), ) - def _handle_exec__code(self, request): + def _handle_exec__code(self, request, params=None): if not self._code_snippet_valued(): return {} eval_ctx = self._get_code_snippet_eval_context(request) + eval_ctx["params"] = params snippet = self.code_snippet safe_eval.safe_eval(snippet, eval_ctx, mode="exec", nocopy=True) result = eval_ctx.get("result") @@ -205,14 +208,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, 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 + if params: + res = handler(request, params=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