From 1b36f3c254d2b50e3a5ad62e375c9fc3b3bf749c Mon Sep 17 00:00:00 2001 From: summersz Date: Wed, 1 Nov 2023 15:38:10 +0000 Subject: [PATCH 1/2] feat: support async view functions closes #22 --- flask_parameter_validation/parameter_validation.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/flask_parameter_validation/parameter_validation.py b/flask_parameter_validation/parameter_validation.py index fae588d..fe6b846 100644 --- a/flask_parameter_validation/parameter_validation.py +++ b/flask_parameter_validation/parameter_validation.py @@ -1,3 +1,5 @@ +import asyncio +import functools import inspect import re from inspect import signature @@ -39,7 +41,8 @@ def __call__(self, f): } fn_list[fsig] = fdocs - def nested_func(**kwargs): + @functools.wraps(f) + async def nested_func(**kwargs): # Step 1 - Combine all flask input types to one dict json_input = None if request.headers.get("Content-Type") is not None: @@ -74,7 +77,11 @@ def nested_func(**kwargs): except Exception as e: return self.custom_error_handler(e) validated_inputs[expected.name] = new_input - return f(**validated_inputs) + + if asyncio.iscoroutinefunctions(f): + return await f(**validated_inputs) + else: + return f(**validated_inputs) nested_func.__name__ = f.__name__ return nested_func From 3e02b86d8320b8d8c865febbeaf08ff9ce32beb2 Mon Sep 17 00:00:00 2001 From: George O <16269580+Ge0rg3@users.noreply.github.com> Date: Thu, 16 Nov 2023 10:59:05 +0000 Subject: [PATCH 2/2] Fix typo and add dep --- flask_parameter_validation/parameter_validation.py | 4 ++-- setup.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/flask_parameter_validation/parameter_validation.py b/flask_parameter_validation/parameter_validation.py index fe6b846..005a31f 100644 --- a/flask_parameter_validation/parameter_validation.py +++ b/flask_parameter_validation/parameter_validation.py @@ -77,8 +77,8 @@ async def nested_func(**kwargs): except Exception as e: return self.custom_error_handler(e) validated_inputs[expected.name] = new_input - - if asyncio.iscoroutinefunctions(f): + + if asyncio.iscoroutinefunction(f): return await f(**validated_inputs) else: return f(**validated_inputs) diff --git a/setup.py b/setup.py index 8078343..95958b1 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ platforms='any', install_requires=[ 'Flask', + 'flask[async]', 'python-dateutil', ], classifiers=[