12
12
from .do_wrapper import _do_wrapper
13
13
from .http import HTTPMethod
14
14
from .models import Tag , Components
15
+ from .types import OpenAPIResponsesType
15
16
from .utils import get_openapi_path , get_operation , get_responses , parse_and_store_tags , parse_parameters , \
16
17
validate_responses_type , parse_method , get_operation_id_for_path
17
18
@@ -24,7 +25,7 @@ def __init__(
24
25
* ,
25
26
abp_tags : Optional [List [Tag ]] = None ,
26
27
abp_security : Optional [List [Dict [str , List [str ]]]] = None ,
27
- abp_responses : Optional [ Dict [ str , Type [ BaseModel ]]] = None ,
28
+ abp_responses : OpenAPIResponsesType = None ,
28
29
doc_ui : bool = True ,
29
30
** kwargs : Any
30
31
) -> None :
@@ -53,6 +54,28 @@ def __init__(
53
54
self .abp_responses = abp_responses or {}
54
55
self .doc_ui = doc_ui
55
56
57
+ def register_api (self , api : "APIBlueprint" ) -> None :
58
+ """Register a nested APIBlueprint"""
59
+ if api is self :
60
+ raise ValueError ("Cannot register a api blueprint on itself" )
61
+
62
+ for tag in api .tags :
63
+ if tag .name not in self .tag_names :
64
+ self .tags .append (tag )
65
+
66
+ for path_url , path_item in api .paths .items ():
67
+ trail_slash = path_url .endswith ('/' )
68
+ # merge url_prefix and new api blueprint path url
69
+ uri = self .url_prefix .rstrip ("/" ) + "/" + path_url .lstrip ("/" ) if self .url_prefix else path_url
70
+ # strip the right slash
71
+ if not trail_slash :
72
+ uri = uri .rstrip ('/' )
73
+ self .paths [uri ] = path_item
74
+
75
+ self .components_schemas .update (** api .components_schemas )
76
+
77
+ self .register_blueprint (api )
78
+
56
79
def _do_decorator (
57
80
self ,
58
81
rule : str ,
@@ -145,12 +168,13 @@ def get(
145
168
tags : Optional [List [Tag ]] = None ,
146
169
summary : Optional [str ] = None ,
147
170
description : Optional [str ] = None ,
148
- responses : Optional [ Dict [ str , Type [ BaseModel ]]] = None ,
171
+ responses : OpenAPIResponsesType = None ,
149
172
extra_responses : Optional [Dict [str , dict ]] = None ,
150
173
security : Optional [List [Dict [str , List [Any ]]]] = None ,
151
174
deprecated : Optional [bool ] = None ,
152
175
operation_id : Optional [str ] = None ,
153
- doc_ui : bool = True
176
+ doc_ui : bool = True ,
177
+ ** options : Any
154
178
) -> Callable :
155
179
"""Decorator for rest api, like: app.route(methods=['GET'])"""
156
180
@@ -186,7 +210,7 @@ def wrapper(**kwargs) -> Response:
186
210
)
187
211
return resp
188
212
189
- options = {"methods" : [HTTPMethod .GET ]}
213
+ options . update ( {"methods" : [HTTPMethod .GET ]})
190
214
self .add_url_rule (rule , view_func = wrapper , ** options )
191
215
192
216
return wrapper
@@ -200,12 +224,13 @@ def post(
200
224
tags : Optional [List [Tag ]] = None ,
201
225
summary : Optional [str ] = None ,
202
226
description : Optional [str ] = None ,
203
- responses : Optional [ Dict [ str , Type [ BaseModel ]]] = None ,
227
+ responses : OpenAPIResponsesType = None ,
204
228
extra_responses : Optional [Dict [str , dict ]] = None ,
205
229
security : Optional [List [Dict [str , List [Any ]]]] = None ,
206
230
deprecated : Optional [bool ] = None ,
207
231
operation_id : Optional [str ] = None ,
208
- doc_ui : bool = True
232
+ doc_ui : bool = True ,
233
+ ** options : Any
209
234
) -> Callable :
210
235
"""Decorator for rest api, like: app.route(methods=['POST'])"""
211
236
@@ -241,7 +266,7 @@ def wrapper(**kwargs) -> Response:
241
266
)
242
267
return resp
243
268
244
- options = {"methods" : [HTTPMethod .POST ]}
269
+ options . update ( {"methods" : [HTTPMethod .POST ]})
245
270
self .add_url_rule (rule , view_func = wrapper , ** options )
246
271
247
272
return wrapper
@@ -255,12 +280,13 @@ def put(
255
280
tags : Optional [List [Tag ]] = None ,
256
281
summary : Optional [str ] = None ,
257
282
description : Optional [str ] = None ,
258
- responses : Optional [ Dict [ str , Type [ BaseModel ]]] = None ,
283
+ responses : OpenAPIResponsesType = None ,
259
284
extra_responses : Optional [Dict [str , dict ]] = None ,
260
285
security : Optional [List [Dict [str , List [Any ]]]] = None ,
261
286
deprecated : Optional [bool ] = None ,
262
287
operation_id : Optional [str ] = None ,
263
- doc_ui : bool = True
288
+ doc_ui : bool = True ,
289
+ ** options : Any
264
290
) -> Callable :
265
291
"""Decorator for rest api, like: app.route(methods=['PUT'])"""
266
292
@@ -296,7 +322,7 @@ def wrapper(**kwargs) -> Response:
296
322
)
297
323
return resp
298
324
299
- options = {"methods" : [HTTPMethod .PUT ]}
325
+ options . update ( {"methods" : [HTTPMethod .PUT ]})
300
326
self .add_url_rule (rule , view_func = wrapper , ** options )
301
327
302
328
return wrapper
@@ -310,12 +336,13 @@ def delete(
310
336
tags : Optional [List [Tag ]] = None ,
311
337
summary : Optional [str ] = None ,
312
338
description : Optional [str ] = None ,
313
- responses : Optional [ Dict [ str , Type [ BaseModel ]]] = None ,
339
+ responses : OpenAPIResponsesType = None ,
314
340
extra_responses : Optional [Dict [str , dict ]] = None ,
315
341
security : Optional [List [Dict [str , List [Any ]]]] = None ,
316
342
deprecated : Optional [bool ] = None ,
317
343
operation_id : Optional [str ] = None ,
318
- doc_ui : bool = True
344
+ doc_ui : bool = True ,
345
+ ** options : Any
319
346
) -> Callable :
320
347
"""Decorator for rest api, like: app.route(methods=['DELETE'])"""
321
348
@@ -351,7 +378,7 @@ def wrapper(**kwargs) -> Response:
351
378
)
352
379
return resp
353
380
354
- options = {"methods" : [HTTPMethod .DELETE ]}
381
+ options . update ( {"methods" : [HTTPMethod .DELETE ]})
355
382
self .add_url_rule (rule , view_func = wrapper , ** options )
356
383
357
384
return wrapper
@@ -365,12 +392,13 @@ def patch(
365
392
tags : Optional [List [Tag ]] = None ,
366
393
summary : Optional [str ] = None ,
367
394
description : Optional [str ] = None ,
368
- responses : Optional [ Dict [ str , Type [ BaseModel ]]] = None ,
395
+ responses : OpenAPIResponsesType = None ,
369
396
extra_responses : Optional [Dict [str , dict ]] = None ,
370
397
security : Optional [List [Dict [str , List [Any ]]]] = None ,
371
398
deprecated : Optional [bool ] = None ,
372
399
operation_id : Optional [str ] = None ,
373
- doc_ui : bool = True
400
+ doc_ui : bool = True ,
401
+ ** options : Any
374
402
) -> Callable :
375
403
"""Decorator for rest api, like: app.route(methods=['PATCH'])"""
376
404
@@ -406,7 +434,7 @@ def wrapper(**kwargs) -> Response:
406
434
)
407
435
return resp
408
436
409
- options = {"methods" : [HTTPMethod .PATCH ]}
437
+ options . update ( {"methods" : [HTTPMethod .PATCH ]})
410
438
self .add_url_rule (rule , view_func = wrapper , ** options )
411
439
412
440
return wrapper
0 commit comments