From d9def32f9342a1b265bb9d4c7e417fc14dceaff7 Mon Sep 17 00:00:00 2001 From: Daniel Reis Date: Mon, 23 Dec 2024 11:47:18 +0000 Subject: [PATCH 1/2] [IMP] endpoint: support usage of querystring parameters --- endpoint/controllers/main.py | 2 +- endpoint/models/endpoint_mixin.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) 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 1e10609a..c15c465a 100644 --- a/endpoint/models/endpoint_mixin.py +++ b/endpoint/models/endpoint_mixin.py @@ -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. + * 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 @@ -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, params=None): if not self._code_snippet_valued(): return {} eval_ctx = self._get_code_snippet_eval_context(request) + eval_ctx["params"] = params or {} snippet = self.code_snippet safe_eval.safe_eval(snippet, eval_ctx, mode="exec", nocopy=True) result = eval_ctx.get("result") @@ -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, 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 From bd1c0b21331fe0306bea505df761cb642bb1141e Mon Sep 17 00:00:00 2001 From: SilvioC2C Date: Mon, 22 Sep 2025 11:17:23 +0200 Subject: [PATCH 2/2] [IMP] endpoint: rename ``params`` to ``querystring_params`` --- endpoint/controllers/main.py | 2 +- endpoint/models/endpoint_mixin.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/endpoint/controllers/main.py b/endpoint/controllers/main.py index d3a557fd..ee31a3a3 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, params=params) + result = endpoint._handle_request(request, querystring_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 c15c465a..2cb52d9d 100644 --- a/endpoint/models/endpoint_mixin.py +++ b/endpoint/models/endpoint_mixin.py @@ -105,7 +105,7 @@ 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. - * params + * querystring_params Must assign a ``result`` variable, with either an instance of ``Response``, or a dict containiny any of the keys: @@ -189,11 +189,11 @@ def _code_snippet_log_func(self, message, level="info"): ), ) - def _handle_exec__code(self, request, params=None): + 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["params"] = params or {} + eval_ctx["querystring_params"] = querystring_params or {} snippet = self.code_snippet safe_eval.safe_eval(snippet, eval_ctx, mode="exec", nocopy=True) result = eval_ctx.get("result") @@ -241,7 +241,7 @@ def _get_handler(self): _("Missing handler for exec mode %s") % self.exec_mode ) from e - def _handle_request(self, request, params=None): + def _handle_request(self, request, querystring_params=None): # Switch user for the whole process self_with_user = self if self.exec_as_user_id: @@ -249,8 +249,8 @@ def _handle_request(self, request, params=None): handler = self_with_user._get_handler() try: # In case the handler does not support params - if params: - res = handler(request, params=params) + if querystring_params: + res = handler(request, querystring_params=querystring_params) else: res = handler(request) except self._bad_request_exceptions() as orig_exec: