APIBlueprint
¶
Bases: APIScaffold
, Blueprint
Flask's Blueprint
object with some web API support.
Examples:
from apiflask import APIBlueprint
bp = APIBlueprint('foo', __name__)
Version changed: 0.5.0
- Add
enable_openapi
parameter.
Version added: 0.2.0
Source code in apiflask/blueprint.py
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 |
|
__init__(name, import_name, tag=None, enable_openapi=True, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_group=_sentinel)
¶
Make a blueprint instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the blueprint. Will be prepended to each endpoint name. |
required |
import_name |
str
|
The name of the blueprint package, usually
|
required |
tag |
str | dict | None
|
The tag of this blueprint. If not set, the
|
None
|
enable_openapi |
bool
|
If |
True
|
Other keyword arguments are directly passed to flask.Blueprint
.
Source code in apiflask/blueprint.py
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 |
|
APIScaffold
¶
A base class for APIFlask
and
APIBlueprint
.
This class contains the route shortcut decorators (i.e. get
, post
, etc.) and
API-related decorators (i.e. auth_required
, input
, output
, doc
).
Version added: 1.0
Source code in apiflask/scaffold.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
|
auth_required(auth, roles=None, optional=None)
¶
Protect a view with provided authentication settings.
Be sure to put it under the routes decorators (i.e.,
app.route
,app.get
,app.post
, etc.).
Examples:
from apiflask import APIFlask, HTTPTokenAuth
app = APIFlask(__name__)
auth = HTTPTokenAuth()
@app.get('/')
@app.auth_required(auth)
def hello():
return 'Hello'!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auth |
HTTPAuthType
|
The |
required |
roles |
list | None
|
The selected roles to allow to visit this view, accepts a list of role names. See Flask-HTTPAuth's documentation for more details. |
None
|
optional |
str | None
|
Set to |
None
|
Version changed: 2.0.0
- Remove the deprecated
role
parameter.
Version changed: 1.0.0
- The
role
parameter is deprecated.
Version changed: 0.12.0
- Move to
APIFlask
andAPIBlueprint
classes.
Version changed: 0.4.0
- Add parameter
roles
.
Source code in apiflask/scaffold.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
delete(rule, **options)
¶
Shortcut for app.route(methods=['DELETE'])
.
Source code in apiflask/scaffold.py
184 185 186 |
|
doc(summary=None, description=None, tags=None, responses=None, deprecated=None, hide=None, operation_id=None, security=None, extensions=None)
¶
Set up the OpenAPI Spec for view functions.
Be sure to put it under the routes decorators (i.e.,
app.route
,app.get
,app.post
, etc.).
Examples:
from apiflask import APIFlask
app = APIFlask(__name__)
@app.get('/')
@app.doc(summary='Say hello', tags=['Foo'])
def hello():
return 'Hello'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
summary |
str | None
|
The summary of this endpoint. If not set, the name of the view function
will be used. If your view function is named with
|
None
|
description |
str | None
|
The description of this endpoint. If not set, the lines after the empty line of the docstring will be used. |
None
|
tags |
list[str] | None
|
A list of tag names of this endpoint, map the tags you passed in the |
None
|
responses |
ResponsesType | None
|
The other responses for this view function, accepts a list of status codes
( |
None
|
deprecated |
bool | None
|
Flag this endpoint as deprecated in API docs. |
None
|
hide |
bool | None
|
Hide this endpoint in API docs. |
None
|
operation_id |
str | None
|
The |
None
|
security |
str | list[str | dict[str, list]] | None
|
The |
None
|
extensions |
dict[str, Any] | None
|
The spec extensions of this endpoint (OpenAPI operation object). The fields in this extensions dict should start with "x-" prefix. See more details in the Specification Extensions chapter of OpenAPI docs. |
None
|
Version changed: 2.2.0
- Add
extensions
parameter to support setting spec extensions.
Version changed: 2.0.0
- Remove the deprecated
tag
parameter. - Expand
responses
to support additional structure and parameters.
Version changed: 1.0
- Add
security
parameter to support customizing security info. - The
role
parameter is deprecated.
Version changed: 0.12.0
- Move to
APIFlask
andAPIBlueprint
classes.
Version changed: 0.10.0
- Add parameter
operation_id
.
Version changed: 0.5.0
- Change the default value of parameters
hide
anddeprecated
fromFalse
toNone
.
Version changed: 0.4.0
- Add parameter
tag
.
Version changed: 0.3.0
- Change the default value of
deprecated
fromNone
toFalse
. - Rename parameter
tags
totag
.
Version added: 0.2.0
Source code in apiflask/scaffold.py
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 |
|
get(rule, **options)
¶
Shortcut for app.route()
or app.route(methods=['GET'])
.
Source code in apiflask/scaffold.py
168 169 170 |
|
input(schema, location='json', arg_name=None, schema_name=None, example=None, examples=None, validation=True, **kwargs)
¶
Add input settings for view functions.
If the validation passed, the data will be injected into the view
function as a keyword argument in the form of dict
and named {location}_data
.
Otherwise, an error response with the detail of the validation result will be
returned.
Be sure to put it under the routes decorators (i.e.,
app.route
,app.get
,app.post
, etc.).
Examples:
from apiflask import APIFlask
app = APIFlask(__name__)
@app.get('/')
@app.input(PetIn, location='json')
def hello(json_data):
print(json_data)
return 'Hello'!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
SchemaType
|
The marshmallow schema of the input data. |
required |
location |
str
|
The location of the input data, one of |
'json'
|
arg_name |
str | None
|
The name of the argument passed to the view function,
defaults to |
None
|
schema_name |
str | None
|
The schema name for dict schema, only needed when you pass
a schema dict (e.g., |
None
|
example |
Any | None
|
The example data in dict for request body, you should use either
|
None
|
examples |
dict[str, Any] | None
|
Multiple examples for request body, you should pass a dict that contains multiple examples. Example:
|
None
|
validation |
bool
|
Flag to allow disabling of validation on input. Default to |
True
|
*Version changed: 2.2.2
- Add parameter
validation
to allow disabling of validation on input.
Version changed: 2.0.0
- Always pass parsed data to view function as a keyword argument.
The argument name will be in the form of
{location}_data
.
Version changed: 1.0
- Ensure only one input body location was used.
- Add
form_and_files
andjson_or_form
(from webargs) location. - Rewrite
files
to act asform_and_files
. - Use correct request content type for
form
andfiles
.
Version changed: 0.12.0
- Move to APIFlask and APIBlueprint classes.
Version changed: 0.4.0
- Add parameter
examples
.
Source code in apiflask/scaffold.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
output(schema, status_code=200, description=None, schema_name=None, example=None, examples=None, links=None, content_type='application/json', headers=None)
¶
Add output settings for view functions.
Be sure to put it under the routes decorators (i.e.,
app.route
,app.get
,app.post
, etc.).
The decorator will format the return value of your view function with provided marshmallow schema. You can return a dict or an object (such as a model class instance of ORMs). APIFlask will handle the formatting and turn your return value into a JSON response.
P.S. The output data will not be validated; it's a design choice of marshmallow. marshmallow 4.0 may be support the output validation.
Examples:
from apiflask import APIFlask
app = APIFlask(__name__)
@app.get('/')
@app.output(PetOut)
def hello():
return the_dict_or_object_match_petout_schema
Parameters:
Name | Type | Description | Default |
---|---|---|---|
schema |
SchemaType
|
The schemas of the output data. |
required |
status_code |
int
|
The status code of the response, defaults to |
200
|
description |
str | None
|
The description of the response. |
None
|
schema_name |
str | None
|
The schema name for dict schema, only needed when you pass
a schema dict (e.g., |
None
|
example |
Any | None
|
The example data in dict for response body, you should use either
|
None
|
examples |
dict[str, Any] | None
|
Multiple examples for response body, you should pass a dict that contains multiple examples. Example:
|
None
|
links |
dict[str, Any] | None
|
The
See the docs for more details about setting response links. |
None
|
content_type |
str | None
|
The content/media type of the response. It defaults to |
'application/json'
|
headers |
SchemaType | None
|
The schemas of the headers. |
None
|
Version changed: 2.1.0
- Add parameter
headers
.
Version changed: 2.0.0
- Don't change the status code to 204 for EmptySchema.
Version changed: 1.3.0
- Add parameter
content_type
.
Version changed: 0.12.0
- Move to APIFlask and APIBlueprint classes.
Version changed: 0.10.0
- Add
links
parameter.
Version changed: 0.9.0
- Add base response customization support.
Version changed: 0.6.0
- Support decorating async views.
Version changed: 0.5.2
- Return the
Response
object directly.
Version changed: 0.4.0
- Add parameter
examples
.
Source code in apiflask/scaffold.py
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
|
patch(rule, **options)
¶
Shortcut for app.route(methods=['PATCH'])
.
Source code in apiflask/scaffold.py
180 181 182 |
|
post(rule, **options)
¶
Shortcut for app.route(methods=['POST'])
.
Source code in apiflask/scaffold.py
172 173 174 |
|
put(rule, **options)
¶
Shortcut for app.route(methods=['PUT'])
.
Source code in apiflask/scaffold.py
176 177 178 |
|