Skip to content

Commit

Permalink
Merge pull request #28 from summersz/async
Browse files Browse the repository at this point in the history
feat: support async view functions
  • Loading branch information
Ge0rg3 authored Nov 16, 2023
2 parents a03ad91 + 3e02b86 commit f64236d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions flask_parameter_validation/parameter_validation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio
import functools
import inspect
import re
from inspect import signature
Expand Down Expand Up @@ -40,7 +42,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:
Expand Down Expand Up @@ -78,7 +81,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.iscoroutinefunction(f):
return await f(**validated_inputs)
else:
return f(**validated_inputs)

nested_func.__name__ = f.__name__
return nested_func
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
platforms='any',
install_requires=[
'Flask',
'flask[async]',
'python-dateutil',
],
classifiers=[
Expand Down

0 comments on commit f64236d

Please sign in to comment.