-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflask-swagger.py
162 lines (139 loc) · 3.85 KB
/
flask-swagger.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from flask import Flask, jsonify
from flask.views import MethodView
from flask_swagger import swagger
app = Flask(__name__)
class UserAPI(MethodView):
def get(self, team_id):
"""
Get a list of users
First line is the summary
All following lines until the hyphens is added to description
---
tags:
- users
responses:
200:
description: Returns a list of users
"""
return []
def post(self, team_id):
"""
Create a new user
---
tags:
- users
parameters:
- in: body
name: body
schema:
id: User
required:
- email
- name
properties:
email:
type: string
description: email for user
name:
type: string
description: name for user
responses:
201:
description: User created
"""
return {}
def put(self, user_id):
"""
Update a user
swagger_from_file: user_put.yml
"""
return {}
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin','*')
response.headers.add('Access-Control-Allow-Headers', "Authorization, Content-Type")
response.headers.add('Access-Control-Expose-Headers', "Authorization")
response.headers.add('Access-Control-Allow-Methods', "GET, POST, PUT, DELETE, OPTIONS")
response.headers.add('Access-Control-Allow-Credentials', "true")
response.headers.add('Access-Control-Max-Age', 60 * 60 * 24 * 20)
return response
view = UserAPI.as_view('users')
app.add_url_rule('/users/<int:team_id>', view_func=view, methods=["GET"])
app.add_url_rule('/testing/<int:team_id>', view_func=view)
@app.route("/hacky")
def bla():
"""
An endpoint that isn't using method view
---
tags:
- hacks
responses:
200:
description: Hacked some hacks
schema:
id: Hack
properties:
hack:
type: string
description: it's a hack
subitems:
type: array
items:
schema:
id: SubItem
properties:
bla:
type: string
description: Bla
blu:
type: integer
description: Blu
"""
return jsonify(['hacky'])
class PetAPI(MethodView):
def get(self, pet_id):
"""
Get a pet.
This is an example of how to use references and factored out definitions
---
tags:
- pets
parameters:
- in: path
name: pet_id
definitions:
- schema:
id: Pet
required:
- name
- owner
properties:
name:
type: string
description: the pet's name
owner:
$ref: '#/definitions/Owner'
- schema:
id: Owner
required:
- name
properties:
name:
type: string
description: the owner's name
responses:
200:
description: Returns the specified pet
$ref: '#/definitions/Pet'
"""
return {}
pet_view = PetAPI.as_view('pets')
app.add_url_rule('/pets/<int:pet_id>', view_func=pet_view, methods=["GET"])
@app.route("/")
def hello():
return "Hello World!"
@app.route("/spec")
def spec():
return jsonify(swagger(app, from_file_keyword='swagger_from_file'))
if __name__ == "__main__":
app.run(debug=True)