-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
flask_example.py
88 lines (64 loc) · 2.25 KB
/
flask_example.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
"""A simple number and datetime addition JSON API.
Run the app:
$ python examples/flask_example.py
Try the following with httpie (a cURL-like utility, http://httpie.org):
$ pip install httpie
$ http GET :5001/
$ http GET :5001/ name==Ada
$ http POST :5001/add x=40 y=2
$ http POST :5001/subtract x=40 y=2
$ http POST :5001/dateadd value=1973-04-10 addend=63
$ http POST :5001/dateadd value=2014-10-23 addend=525600 unit=minutes
"""
import datetime as dt
from flask import Flask, jsonify
from webargs import fields, validate
from webargs.flaskparser import use_args, use_kwargs
app = Flask(__name__)
hello_args = {"name": fields.Str(load_default="Friend")}
@app.route("/", methods=["GET"])
@use_args(hello_args)
def index(args):
"""A welcome page."""
return jsonify({"message": "Welcome, {}!".format(args["name"])})
add_args = {"x": fields.Float(required=True), "y": fields.Float(required=True)}
@app.route("/add", methods=["POST"])
@use_kwargs(add_args)
def add(x, y):
"""An addition endpoint."""
return jsonify({"result": x + y})
@app.route("/subtract", methods=["POST"])
@use_kwargs(add_args)
async def subtract(x, y):
"""An async subtraction endpoint."""
return jsonify({"result": x - y})
dateadd_args = {
"value": fields.Date(required=False),
"addend": fields.Int(required=True, validate=validate.Range(min=1)),
"unit": fields.Str(
load_default="days", validate=validate.OneOf(["minutes", "days"])
),
}
@app.route("/dateadd", methods=["POST"])
@use_kwargs(dateadd_args)
def dateadd(value, addend, unit):
"""A date adder endpoint."""
value = value or dt.datetime.utcnow()
if unit == "minutes":
delta = dt.timedelta(minutes=addend)
else:
delta = dt.timedelta(days=addend)
result = value + delta
return jsonify({"result": result.isoformat()})
# Return validation errors as JSON
@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}), err.code
if __name__ == "__main__":
app.run(port=5001, debug=True)