Skip to content

Utils

get_reason_phrase(status_code)

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
Source code in apiflask/utils.py
def get_reason_phrase(status_code: int) -> str:
    """A helper function to get the reason phrase of the given status code.

    Arguments:
        status_code: A standard HTTP status code.
    """
    return HTTP_STATUS_CODES.get(status_code, 'Unknown error')

route_shortcuts(cls)

A decorator to add route shortcuts for Flask and Blueprint objects.

Includes get(), post(), put(), patch(), delete().

Examples:

from flask import Flask

@route_shortcuts
class APIFlask(Flask):
    pass

app = APIFlask(__name__)

@app.get('/')
def hello():
    return 'Hello!'

Version added: 0.2.0

Version changed: 0.3.0 - Turn base class into class decorator.

Source code in apiflask/utils.py
def route_shortcuts(cls):
    """A decorator to add route shortcuts for `Flask` and `Blueprint` objects.

    Includes `get()`, `post()`, `put()`, `patch()`, `delete()`.

    Examples:

    ```python
    from flask import Flask

    @route_shortcuts
    class APIFlask(Flask):
        pass

    app = APIFlask(__name__)

    @app.get('/')
    def hello():
        return 'Hello!'
    ```

    *Version added: 0.2.0*

    *Version changed: 0.3.0*
    - Turn base class into class decorator.
    """
    cls_route = cls.route

    def get(self, rule: str, **options: Any):
        """Shortcut for `app.route()`."""
        return cls_route(self, rule, methods=['GET'], **options)

    def post(self, rule: str, **options: Any):
        """Shortcut for `app.route(methods=['POST'])`."""
        return cls_route(self, rule, methods=['POST'], **options)

    def put(self, rule: str, **options: Any):
        """Shortcut for `app.route(methods=['PUT'])`."""
        return cls_route(self, rule, methods=['PUT'], **options)

    def patch(self, rule: str, **options: Any):
        """Shortcut for `app.route(methods=['PATCH'])`."""
        return cls_route(self, rule, methods=['PATCH'], **options)

    def delete(self, rule: str, **options: Any):
        """Shortcut for `app.route(methods=['DELETE'])`."""
        return cls_route(self, rule, methods=['DELETE'], **options)

    cls.get = get
    cls.post = post
    cls.put = put
    cls.patch = patch
    cls.delete = delete
    return cls