Helpers¶
get_filestorage_size(file)
¶
A helper function to get the size of the FileStorage object in bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
FileStorage
|
A FileStorage object. |
required |
Version added: 2.1.0
Source code in apiflask/helpers.py
114 115 116 117 118 119 120 121 122 123 124 |
|
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
default
parameter.
Source code in apiflask/helpers.py
15 16 17 18 19 20 21 22 23 24 25 26 |
|
pagination_builder(pagination, **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:
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):
pagination = PetModel.query.paginate(
page=query['page'],
per_page=query['per_page']
)
pets = pagination.items
return {
'pets': pets,
'pagination': pagination_builder(pagination)
}
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 |
**kwargs |
Any
|
Additional keyword arguments that passed to the
|
{}
|
Version Added: 0.6.0
Source code in apiflask/helpers.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 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 |
|
parse_size(size)
¶
A helper function to parse a str representing the size into a number in bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
str
|
A str representing the size. |
required |
Version added: 2.1.0
Source code in apiflask/helpers.py
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 |
|