diff --git a/endpoint/controllers/main.py b/endpoint/controllers/main.py index afae8a10..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) + 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 1e10609a..2cb52d9d 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. + * 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 @@ -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 {} 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, 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 + 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