-
Notifications
You must be signed in to change notification settings - Fork 527
/
webargstest.py
65 lines (46 loc) · 1.67 KB
/
webargstest.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
import re
from flask import Flask, jsonify, request
from webargs import fields
from webargs.flaskparser import use_args
app = Flask("hello")
@app.route("/api/login", methods=["POST"])
@use_args({"username": fields.Str(required=True)},
location="json")
def login(args):
name = args['username']
password = args['password']
return jsonify({"code": 200, "msg": "ok"})
@app.route("/api/login", methods=["POST"])
def login3():
data = request.get_json()
email = data.get("email")
if not email or re.match(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", email):
return jsonify({"code": 400, "msg": "参数有误"}), 400
password = data.get("password")
if not password or len(password) < 6:
return jsonify({"code": 400, "msg": "参数有误"}), 400
# 数据库查询
return jsonify({"code": 200, "msg": "ok"})
from webargs import fields, ValidationError
def must_exist_in_db(val):
if val != 1:
raise ValidationError("id not exist")
hello_args = {"name": fields.Str(missing="Friend"),
"id": fields.Integer(required=True, validate=must_exist_in_db)}
@app.route("/", methods=["GET"])
@use_args(hello_args, location="query")
def hello(args):
"""A welcome page."""
return jsonify({"message": "Welcome, {}!".format(args["name"])})
@app.errorhandler(422)
@app.errorhandler(400)
def handle_error(err):
headers = err.data.get("headers", None)
messages = err.data.get("messages", ["Invalid request."])
if headers:
return jsonify({"errors": messages}), err.code, headers
else:
return jsonify({"errors": messages}), 400
#
if __name__ == '__main__':
app.run(port=5000)