1
1
import logging
2
2
from functools import update_wrapper , wraps
3
- from typing import Callable , Dict , Union
3
+ from typing import Callable , Union , Mapping
4
4
5
5
from flask import abort , request
6
6
from marshmallow .exceptions import ValidationError
10
10
from ..schema import FieldSchema , Schema
11
11
12
12
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 ):
22
15
# Wrapper function
23
16
@wraps (f )
24
17
def wrapper (* args , ** kwargs ):
@@ -34,17 +27,17 @@ def wrapper(*args, **kwargs):
34
27
# If no data is there
35
28
if not data :
36
29
# If data is required
37
- if self . schema .required :
30
+ if schema .required :
38
31
# Abort
39
32
return abort (400 )
40
33
# 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
43
36
44
37
# Serialize data if it exists
45
38
if data :
46
39
try :
47
- data = FieldSchema (self . schema ).deserialize (data )
40
+ data = FieldSchema (schema ).deserialize (data )
48
41
except ValidationError as e :
49
42
logging .error (e )
50
43
return abort (400 )
@@ -54,13 +47,13 @@ def wrapper(*args, **kwargs):
54
47
55
48
return wrapper
56
49
50
+ return inner
51
+
57
52
58
53
class use_args :
59
54
"""Equivalent to webargs.flask_parser.use_args"""
60
55
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 ):
64
57
self .schema = schema
65
58
66
59
if isinstance (schema , Field ):
0 commit comments