11import logging
22from functools import update_wrapper , wraps
3- from typing import Callable , Dict , Union
3+ from typing import Callable , Union , Mapping
44
55from flask import abort , request
66from marshmallow .exceptions import ValidationError
1010from ..schema import FieldSchema , Schema
1111
1212
13- class use_body :
14- """Gets the request body as a single value and adds it as a positional argument"""
15-
16- def __init__ (
17- self , schema : Union [Schema , Field , Dict [str , Union [Field , type ]]], ** _
18- ):
19- self .schema = schema
20-
21- def __call__ (self , f : Callable ):
13+ def use_body (schema : Field , ** _ ) -> Callable :
14+ def inner (f : Callable ):
2215 # Wrapper function
2316 @wraps (f )
2417 def wrapper (* args , ** kwargs ):
@@ -34,17 +27,17 @@ def wrapper(*args, **kwargs):
3427 # If no data is there
3528 if not data :
3629 # If data is required
37- if self . schema .required :
30+ if schema .required :
3831 # Abort
3932 return abort (400 )
4033 # Otherwise, look for the schema fields 'missing' property
41- if self . schema .missing :
42- data = self . schema .missing
34+ if schema .missing :
35+ data = schema .missing
4336
4437 # Serialize data if it exists
4538 if data :
4639 try :
47- data = FieldSchema (self . schema ).deserialize (data )
40+ data = FieldSchema (schema ).deserialize (data )
4841 except ValidationError as e :
4942 logging .error (e )
5043 return abort (400 )
@@ -54,13 +47,13 @@ def wrapper(*args, **kwargs):
5447
5548 return wrapper
5649
50+ return inner
51+
5752
5853class use_args :
5954 """Equivalent to webargs.flask_parser.use_args"""
6055
61- def __init__ (
62- self , schema : Union [Schema , Field , Dict [str , Union [Field , type ]]], ** kwargs
63- ):
56+ def __init__ (self , schema : Union [Schema , Field , Mapping [str , Field ]], ** kwargs ):
6457 self .schema = schema
6558
6659 if isinstance (schema , Field ):
0 commit comments