@@ -6,18 +6,18 @@ from pydantic import BaseModel
66from flask_openapi3 import Info, Tag
77from flask_openapi3 import OpenAPI
88
9- info = Info(title = ' book API' , version = ' 1.0.0' )
9+ info = Info(title = " book API" , version = " 1.0.0" )
1010app = OpenAPI(__name__ , info = info)
1111
12- book_tag = Tag(name = ' book' , description = ' Some Book' )
12+ book_tag = Tag(name = " book" , description = " Some Book" )
1313
1414
1515class BookQuery (BaseModel ):
1616 age: int
1717 author: str
1818
1919
20- @app.get (' /book' , tags = [book_tag])
20+ @app.get (" /book" , tags = [book_tag])
2121def get_book (query : BookQuery):
2222 """ get books
2323 to get all books
@@ -32,23 +32,21 @@ def get_book(query: BookQuery):
3232 }
3333
3434
35- if __name__ == ' __main__' :
35+ if __name__ == " __main__" :
3636 app.run(debug = True )
3737```
3838
3939## REST Demo
4040
4141``` python
4242from http import HTTPStatus
43- from typing import Optional, List
44-
4543from pydantic import BaseModel, Field
4644
4745from flask_openapi3 import Info, Tag
4846from flask_openapi3 import OpenAPI
4947
5048
51- info = Info(title = ' book API' , version = ' 1.0.0' )
49+ info = Info(title = " book API" , version = " 1.0.0" )
5250# Basic Authentication Sample
5351basic = {
5452 " type" : " http" ,
@@ -89,44 +87,44 @@ class NotFoundResponse(BaseModel):
8987
9088app = OpenAPI(__name__ , info = info, security_schemes = security_schemes, responses = {404 : NotFoundResponse})
9189
92- book_tag = Tag(name = ' book' , description = ' Some Book' )
90+ book_tag = Tag(name = " book" , description = " Some Book" )
9391security = [
9492 {" jwt" : []},
9593 {" oauth2" : [" write:pets" , " read:pets" ]}
9694]
9795
9896
9997class BookPath (BaseModel ):
100- bid: int = Field(... , description = ' book id' )
98+ bid: int = Field(... , description = " book id" )
10199
102100
103101class BookQuery (BaseModel ):
104- age: Optional[ int ] = Field(None , description = ' Age' )
105- s_list: List [str ] = Field(None , alias = ' s_list[]' , description = ' some array' )
102+ age: int | None = Field(None , description = " Age" )
103+ s_list: list [str ] = Field(None , alias = " s_list[]" , description = " some array" )
106104
107105
108106class BookBody (BaseModel ):
109- age: Optional[ int ] = Field(... , ge = 2 , le = 4 , description = ' Age' )
110- author: str = Field(None , min_length = 2 , max_length = 4 , description = ' Author' )
107+ age: int | None = Field(... , ge = 2 , le = 4 , description = " Age" )
108+ author: str = Field(None , min_length = 2 , max_length = 4 , description = " Author" )
111109
112110
113111class BookBodyWithID (BaseModel ):
114- bid: int = Field(... , description = ' book id' )
115- age: Optional[ int ] = Field(None , ge = 2 , le = 4 , description = ' Age' )
116- author: str = Field(None , min_length = 2 , max_length = 4 , description = ' Author' )
112+ bid: int = Field(... , description = " book id" )
113+ age: int | None = Field(None , ge = 2 , le = 4 , description = " Age" )
114+ author: str = Field(None , min_length = 2 , max_length = 4 , description = " Author" )
117115
118116
119117class BookResponse (BaseModel ):
120118 code: int = Field(0 , description = " Status Code" )
121119 message: str = Field(" ok" , description = " Exception Information" )
122- data: Optional[ BookBodyWithID]
120+ data: BookBodyWithID | None
123121
124122
125123@app.get (
126- ' /book/<int:bid>' ,
124+ " /book/<int:bid>" ,
127125 tags = [book_tag],
128- summary = ' new summary' ,
129- description = ' new description' ,
126+ summary = " new summary" ,
127+ description = " new description" ,
130128 responses = {200 : BookResponse, 201 : {" content" : {" text/csv" : {" schema" : {" type" : " string" }}}}},
131129 security = security
132130)
@@ -137,11 +135,11 @@ def get_book(path: BookPath):
137135 """
138136 if path.bid == 4 :
139137 return NotFoundResponse().dict(), 404
140- return {" code" : 0 , " message" : " ok" , " data" : {" bid" : path.bid, " age" : 3 , " author" : ' no ' }}
138+ return {" code" : 0 , " message" : " ok" , " data" : {" bid" : path.bid, " age" : 3 , " author" : " no " }}
141139
142140
143141# set doc_ui False disable openapi UI
144- @app.get (' /book' , doc_ui = True , deprecated = True )
142+ @app.get (" /book" , doc_ui = True , deprecated = True )
145143def get_books (query : BookQuery):
146144 """ get books
147145 to get all books
@@ -151,46 +149,44 @@ def get_books(query: BookQuery):
151149 " code" : 0 ,
152150 " message" : " ok" ,
153151 " data" : [
154- {" bid" : 1 , " age" : query.age, " author" : ' a1 ' },
155- {" bid" : 2 , " age" : query.age, " author" : ' a2 ' }
152+ {" bid" : 1 , " age" : query.age, " author" : " a1 " },
153+ {" bid" : 2 , " age" : query.age, " author" : " a2 " }
156154 ]
157155 }
158156
159157
160- @app.post (' /book' , tags = [book_tag], responses = {200 : BookResponse})
158+ @app.post (" /book" , tags = [book_tag], responses = {200 : BookResponse})
161159def create_book (body : BookBody):
162160 print (body)
163161 return {" code" : 0 , " message" : " ok" }, HTTPStatus.OK
164162
165163
166- @app.put (' /book/<int:bid>' , tags = [book_tag])
164+ @app.put (" /book/<int:bid>" , tags = [book_tag])
167165def update_book (path : BookPath, body : BookBody):
168166 print (path)
169167 print (body)
170168 return {" code" : 0 , " message" : " ok" }
171169
172170
173- @app.delete (' /book/<int:bid>' , tags = [book_tag], doc_ui = False )
171+ @app.delete (" /book/<int:bid>" , tags = [book_tag], doc_ui = False )
174172def delete_book (path : BookPath):
175173 print (path)
176174 return {" code" : 0 , " message" : " ok" }
177175
178176
179- if __name__ == ' __main__' :
177+ if __name__ == " __main__" :
180178 app.run(debug = True )
181179```
182180
183181## APIBlueprint
184182
185183``` python
186- from typing import Optional
187-
188184from pydantic import BaseModel, Field
189185
190186from flask_openapi3 import APIBlueprint, OpenAPI
191187from flask_openapi3 import Tag, Info
192188
193- info = Info(title = ' book API' , version = ' 1.0.0' )
189+ info = Info(title = " book API" , version = " 1.0.0" )
194190
195191jwt = {
196192 " type" : " http" ,
@@ -201,7 +197,7 @@ security_schemes = {"jwt": jwt}
201197
202198app = OpenAPI(__name__ , info = info, security_schemes = security_schemes)
203199
204- tag = Tag(name = ' book' , description = " Some Book" )
200+ tag = Tag(name = " book" , description = " Some Book" )
205201security = [{" jwt" : []}]
206202
207203
@@ -211,9 +207,9 @@ class Unauthorized(BaseModel):
211207
212208
213209api = APIBlueprint(
214- ' /book' ,
210+ " /book" ,
215211 __name__ ,
216- url_prefix = ' /api' ,
212+ url_prefix = " /api" ,
217213 abp_tags = [tag],
218214 abp_security = security,
219215 abp_responses = {" 401" : Unauthorized},
@@ -223,26 +219,26 @@ api = APIBlueprint(
223219
224220
225221class BookBody (BaseModel ):
226- age: Optional[ int ] = Field(... , ge = 2 , le = 4 , description = ' Age' )
227- author: str = Field(None , min_length = 2 , max_length = 4 , description = ' Author' )
222+ age: int | None = Field(... , ge = 2 , le = 4 , description = " Age" )
223+ author: str = Field(None , min_length = 2 , max_length = 4 , description = " Author" )
228224
229225
230226class Path (BaseModel ):
231- bid: int = Field(... , description = ' book id' )
227+ bid: int = Field(... , description = " book id" )
232228
233229
234- @api.get (' /book' , doc_ui = False )
230+ @api.get (" /book" , doc_ui = False )
235231def get_book ():
236232 return {" code" : 0 , " message" : " ok" }
237233
238234
239- @api.post (' /book' , responses = {201 : {" content" : {" text/csv" : {" schema" : {" type" : " string" }}}}})
235+ @api.post (" /book" , responses = {201 : {" content" : {" text/csv" : {" schema" : {" type" : " string" }}}}})
240236def create_book (body : BookBody):
241237 assert body.age == 3
242238 return {" code" : 0 , " message" : " ok" }
243239
244240
245- @api.put (' /book/<int:bid>' )
241+ @api.put (" /book/<int:bid>" )
246242def update_book (path : Path, body : BookBody):
247243 assert path.bid == 1
248244 assert body.age == 3
@@ -252,7 +248,7 @@ def update_book(path: Path, body: BookBody):
252248# register api
253249app.register_api(api)
254250
255- if __name__ == ' __main__' :
251+ if __name__ == " __main__" :
256252 app.run(debug = True )
257253```
258254
@@ -271,15 +267,15 @@ class UploadFileForm(BaseModel):
271267 file_type: str = Field(None , description = " File Type" )
272268
273269
274- @app.post (' /upload' )
270+ @app.post (" /upload" )
275271def upload_file (form : UploadFileForm):
276272 print (form.file.filename)
277273 print (form.file_type)
278- form.file.save(' test.jpg' )
274+ form.file.save(" test.jpg" )
279275 return {" code" : 0 , " message" : " ok" }
280276
281277
282- if __name__ == ' __main__' :
278+ if __name__ == " __main__" :
283279 app.run(debug = True )
284280```
285281
0 commit comments