Helpers¶
get_reason_phrase(status_code, default='Unknown')
¶
A helper function to get the reason phrase of the given status code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status_code
|
int
|
A standard HTTP status code. |
required |
default
|
str
|
The default phrase to use if not found, defaults to "Unknown". |
'Unknown'
|
Version Changed: 0.6.0
- Add
defaultparameter.
Source code in apiflask/helpers.py
17 18 19 20 21 22 23 24 25 26 27 28 | |
pagination_builder(pagination, schema_type='marshmallow', **kwargs)
¶
A helper function to make pagination data.
This function is designed based on Flask-SQLAlchemy's Pagination class.
If you are using a different or custom pagination class, make sure the
passed pagination object has the following attributes:
- page
- per_page
- pages
- total
- next_num
- has_next
- prev_num
- has_prev
Or you can write your own builder function to build the pagination data.
Examples:
With marshmallow
from apiflask import PaginationSchema, pagination_builder
...
class PetQuery(Schema):
page = Integer(load_default=1)
per_page = Integer(load_default=20, validate=Range(max=30))
class PetsOut(Schema):
pets = List(Nested(PetOut))
pagination = Nested(PaginationSchema)
@app.get('/pets')
@app.input(PetQuery, location='query')
@app.output(PetsOut)
def get_pets(query_data):
pagination = PetModel.query.paginate(
page=query_data['page'],
per_page=query_data['per_page']
)
pets = pagination.items
return {
'pets': pets,
'pagination': pagination_builder(pagination)
}
With Pydantic:
from apiflask import pagination_builder, PaginationModel
from pydantic import BaseModel, Field, ConfigDict
...
class PetQuery(BaseModel):
page: int = Field(default=1)
per_page: int = Field(default=20, le=30)
class PetOut(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
name: str
category: str
class PetsOut(BaseModel):
pets: List[PetOut] = []
pagination: PaginationModel
@app.get('/pets')
@app.input(PetQuery, location='query')
@app.output(PetsOut)
def get_pets(query_data: PetQuery):
pagination = PetModel.query.paginate(
page=query_data.page,
per_page=query_data.per_page
)
pets = pagination.items
return PetsOut(
pets=pets,
pagination=pagination_builder(pagination, schema_type='pydantic')
)
See https://github.com/apiflask/apiflask/blob/main/examples/pagination/app.py for the complete example.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pagination
|
PaginationType
|
The pagination object. |
required |
schema_type
|
Literal['marshmallow', 'pydantic']
|
The pagination data type. One of |
'marshmallow'
|
**kwargs
|
Any
|
Additional keyword arguments that passed to the
|
{}
|
Version Added: 0.6.0
Version Changed: 3.1.0
- Add
schema_typeparameter.
Source code in apiflask/helpers.py
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 163 164 165 166 167 | |