1
1
# Flask-First
2
2
3
- Flask extension for using "specification first" principle .
3
+ Flask extension for using "specification first" and "API-first" principles .
4
4
5
- Features:
5
+ <!-- TOC-->
6
+
7
+ - [ Flask-First] ( #flask-first )
8
+ - [ Features] ( #features )
9
+ - [ Limitations] ( #limitations )
10
+ - [ Installation] ( #installation )
11
+ - [ Settings] ( #settings )
12
+ - [ Data types] ( #data-types )
13
+ - [ Examples] ( #examples )
14
+ - [ Simple example] ( #simple-example )
15
+ - [ Specification from multiple file] ( #specification-from-multiple-file )
16
+ - [ CORS support] ( #cors-support )
17
+ - [ Additional documentation] ( #additional-documentation )
18
+
19
+ <!-- TOC-->
20
+
21
+ ## Features
6
22
7
23
* ` Application Factory ` supported.
8
- * Validating and serializing arguments from ` request.headers ` to ` request.first_headers ` .
9
- * Validating and serializing arguments from ` request.view_args ` to ` request.first_view_args ` .
10
- * Validating and serializing arguments from ` request.args ` to ` request.first_args ` .
11
- * Validating and serializing arguments from ` request.cookies ` to ` request.first_cookies ` .
12
- * Validating and serializing arguments from ` request.json ` to ` request.first_json ` .
13
- * Validating headers from request.
14
- * Validating cookies from request.
15
- * Validating path parameters from request.
16
- * Validating parameters from request.
17
- * Validating JSON from request.
18
- * Validating JSON from response.
24
+ * Validating and serializing headers of request from ` request.headers ` to ` request.first_headers ` .
25
+ * Validating and serializing path parameters of request from ` request.view_args `
26
+ to ` request.first_view_args ` .
27
+ * Validating and serializing arguments of request from ` request.args ` to ` request.first_args ` .
28
+ * Validating and serializing cookies of request from ` request.cookies ` to ` request.first_cookies ` .
29
+ * Validating and serializing JSON of request from ` request.json ` to ` request.first_json ` .
30
+ * Validating JSON from response for debugging.
19
31
* Provides a Swagger UI.
32
+ * Support OpenAPI version 3.1.0.
33
+ * Support specification from multiple file.
20
34
21
- Limitations
22
- ----
35
+ ### Limitations
23
36
24
37
Will be added in future releases.
25
38
26
- * Full specification in one file.
27
39
* Authorization not supported.
28
40
29
- ## Installing
41
+ ## Installation
30
42
31
43
Recommended using the latest version of Python. Flask-First supports Python 3.9 and newer.
32
44
33
45
Install and update using ` pip ` :
34
46
35
47
``` shell
36
- $ pip install flask_first
48
+ $ pip install -U flask_first
37
49
```
38
50
39
- Simple example
40
- --------------
51
+ ## Settings
52
+
53
+ ` FIRST_RESPONSE_VALIDATION ` - Default: ` False ` . Enabling response body validation. Useful when
54
+ developing. Must be disabled in a production environment.
55
+
56
+ ## Data types
57
+
58
+ Supported formats for string type field:
59
+
60
+ * uuid
61
+ * date-time
62
+ * date
63
+ * time
64
+ * email
65
+ * ipv4
66
+ * ipv6
67
+ * uri
68
+ * binary
69
+
70
+ ## Examples
71
+
72
+ ### Simple example
73
+
41
74
OpenAPI 3 specification file ` openapi.yaml ` :
42
75
43
76
``` yaml
@@ -48,16 +81,16 @@ info:
48
81
paths :
49
82
/{name} :
50
83
parameters :
51
- - name : name
52
- in : path
53
- required : true
54
- schema :
55
- type : string
84
+ - name : name
85
+ in : path
86
+ required : true
87
+ schema :
88
+ type : string
56
89
get :
57
90
operationId : index
58
91
summary : Returns a list of items
59
92
responses :
60
- ' 200 ' :
93
+ 200 :
61
94
description : OK
62
95
content :
63
96
application/json :
@@ -85,14 +118,13 @@ first = First(path_to_spec, app=app, swagger_ui_path='/docs')
85
118
86
119
87
120
def index(name):
88
- return {'message': name}
121
+ return {'message': name}
89
122
90
123
91
124
first.add_view_func(index)
92
125
93
126
if __name__ == '__main__':
94
- app.run()
95
-
127
+ app.run()
96
128
` ` `
97
129
98
130
Run application :
@@ -104,12 +136,58 @@ $ python main.py
104
136
Check url in browser `http://127.0.0.1:5000/username`. Check SwaggerUI url in
105
137
browser `http://127.0.0.1:5000/docs`.
106
138
107
- # # Settings
139
+ # ## Specification from multiple file
108
140
109
- `FIRST_RESPONSE_VALIDATION` - Default : ` False ` . Enabling response body validation. Useful when
110
- developing. May be disabled in a production environment .
141
+ Flask-First supported specification OpenAPI from multiple files. You need create root file for
142
+ specification with name `openapi.yaml` .
111
143
112
- # # CORS support
144
+ Root file `openapi.yaml` :
145
+
146
+ ` ` ` yaml
147
+ openapi: 3.1.0
148
+ info:
149
+ title: Simple API for Flask-First
150
+ version: 1.0.0
151
+ paths:
152
+ /{name}:
153
+ $ref: 'name.openapi.yaml#/name'
154
+ components:
155
+ schemas:
156
+ MessageField:
157
+ type: string
158
+ description: Field for message.
159
+ ` ` `
160
+
161
+ Child file `name.openapi.yaml` :
162
+
163
+ ` ` ` yaml
164
+ name:
165
+ parameters:
166
+ - name: name
167
+ in: path
168
+ required: true
169
+ schema:
170
+ type: string
171
+ get:
172
+ operationId: index
173
+ summary: Returns a list of items
174
+ responses:
175
+ '200':
176
+ $ref: '#/components/responses/ResponseOK'
177
+ components:
178
+ responses:
179
+ ResponseOK:
180
+ description: OK
181
+ content:
182
+ application/json:
183
+ schema:
184
+ type: object
185
+ properties:
186
+ message:
187
+ $ref: 'openapi.yaml#/components/schemas/MessageField'
188
+ ` ` `
189
+
190
+ # ## CORS support
113
191
114
192
Your need enable CORS in Flask and adding `OPTIONS` method in your specification. Example :
115
193
@@ -125,34 +203,20 @@ paths:
125
203
options:
126
204
summary: CORS support
127
205
responses:
128
- 200:
129
- headers:
130
- Access-Control-Allow-Origin:
131
- schema:
132
- type: string
133
- Access-Control-Allow-Methods:
134
- schema:
135
- type: string
136
- Access-Control-Allow-Headers:
137
- schema:
138
- type: string
139
- content: { }
206
+ 200:
207
+ headers:
208
+ Access-Control-Allow-Origin:
209
+ schema:
210
+ type: string
211
+ Access-Control-Allow-Methods:
212
+ schema:
213
+ type: string
214
+ Access-Control-Allow-Headers:
215
+ schema:
216
+ type: string
217
+ content: { }
140
218
` ` `
141
219
142
- # # Data types
143
-
144
- Supported formats for string type field :
145
-
146
- * uuid
147
- * date-time
148
- * date
149
- * time
150
- * email
151
- * ipv4
152
- * ipv6
153
- * uri
154
- * binary
155
-
156
220
# # Additional documentation
157
221
158
222
* [OpenAPI Documentation](https://swagger.io/specification/).
0 commit comments