-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdict_blueprint.py
96 lines (81 loc) · 2.83 KB
/
dict_blueprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import datetime
from typing import Optional, List, Union
from flask import Blueprint, jsonify
from flask_parameter_validation import ValidateParameters, Route
from flask_parameter_validation.parameter_types.parameter import Parameter
from flask_parameter_validation.test.testing_blueprints.dummy_decorators import dummy_decorator, dummy_async_decorator
def get_dict_blueprint(ParamType: type[Parameter], bp_name: str, http_verb: str) -> Blueprint:
dict_bp = Blueprint(bp_name, __name__, url_prefix="/dict")
decorator = getattr(dict_bp, http_verb)
# dict not currently supported by Route
# def path(base: str, route_additions: str) -> str:
# return base + (route_additions if ParamType is Route else "")
@decorator("/required")
@ValidateParameters()
def req_str(v: dict = ParamType()):
assert type(v) is dict
return jsonify({"v": v})
@decorator("/optional")
@ValidateParameters()
def optional(v: Optional[dict] = ParamType()):
return jsonify({"v": v})
@decorator("/default")
@ValidateParameters()
def default(
n_opt: dict = ParamType(default={"a": "b"}),
opt: Optional[dict] = ParamType(default={"c": "d"})
):
return jsonify({
"n_opt": n_opt,
"opt": opt
})
@decorator("/decorator/default")
@dummy_decorator
@ValidateParameters()
def decorator_default(
n_opt: dict = ParamType(default={"a": "b"}),
opt: Optional[dict] = ParamType(default={"c": "d"})
):
return jsonify({
"n_opt": n_opt,
"opt": opt
})
@decorator("/async_decorator/default")
@dummy_async_decorator
@ValidateParameters()
async def async_decorator_default(
n_opt: dict = ParamType(default={"a": "b"}),
opt: Optional[dict] = ParamType(default={"c": "d"})
):
return jsonify({
"n_opt": n_opt,
"opt": opt
})
def are_keys_lowercase(v):
assert type(v) is dict
for key in v.keys():
if not key.islower():
return False
return True
@decorator("/func")
@ValidateParameters()
def func(v: dict = ParamType(func=are_keys_lowercase)):
return jsonify({"v": v})
json_schema = {
"type": "object",
"required": ["user_id", "first_name", "last_name", "tags"],
"properties": {
"user_id": {"type": "integer"},
"first_name": {"type": "string"},
"last_name": {"type": "string"},
"tags": {
"type": "array",
"items": {"type": "string"}
}
}
}
@decorator("/json_schema")
@ValidateParameters()
def json_schema(v: dict = ParamType(json_schema=json_schema)):
return jsonify({"v": v})
return dict_bp