This repository was archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
111 lines (82 loc) · 2.02 KB
/
Copy pathexample.py
File metadata and controls
111 lines (82 loc) · 2.02 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
from flask import Flask, jsonify
import flask_docjson
app = Flask(__name__)
@app.errorhandler(flask_docjson.RequestValidationError)
def on_request_validation_error(err):
"""Returns bad request on request validation errors.
"""
print(err)
return jsonify(message='Bad request'), 400
@app.errorhandler(flask_docjson.ResponseValidationError)
def on_response_validation_error(err):
"""Returns bad response on response validation errors.
"""
return jsonify(message='Bad response'), 500
@app.route('/item', methods=['POST', 'PUT'])
def create_item():
"""Create an item.
Schema::
POST/PUT /item
{
"name": string(10),
"number": i8
}
200
{
"id": i32,
"name": string(10),
"number": i8
}
4XX/5XX
{"message": string}
"""
return jsonify(id=1, name='name', number=123)
@app.route('/item/<int:id>', methods=['GET'])
def get_item(id):
"""Get an item by id.
Schema::
GET /item/<i32:id>
200
{
"id": i32,
"name": string(10),
"number": i8
}
4XX/5XX
{"message": string}
"""
return jsonify(id=id, name='name', number=123)
@app.route('/item/<int:id>', methods=['DELETE'])
def delete_item(id):
"""Delete an item by id.
Schema::
DELETE /item/<i32:id>
201
4XX/5XX
{"message": string}
"""
return '', 201
@app.route('/items', methods=['GET'])
def get_items():
"""Get all items.
Schema::
GET /items
200
[
{
"id": i32,
"name": string(10),
"number": i8
},
...
]
4XX/5XX
{"message": string}
"""
items = [dict(id=1, name='name', number=123)]
return jsonify(items)
# Must be called after all route definitions
flask_docjson.register_all(app)
if __name__ == '__main__':
app.run()