A multipurpose python flask API server and administration SPA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
847 B

  1. """Decorators to be used in the api module."""
  2. from functools import wraps
  3. from typing import Callable, Any
  4. from flask import jsonify, Response
  5. from corvus.api.model import APIResponse
  6. def return_json(func: Callable) -> Callable:
  7. """
  8. If an Response object is not returned, jsonify the result and return it.
  9. :param func:
  10. :return:
  11. """
  12. @wraps(func)
  13. def decorate(*args: list, **kwargs: dict) -> Any:
  14. """
  15. Make sure that the return of the function is jsonified into a Response.
  16. :param args:
  17. :param kwargs:
  18. :return:
  19. """
  20. result = func(*args, **kwargs)
  21. if isinstance(result, Response):
  22. return result
  23. if isinstance(result, APIResponse):
  24. return jsonify(result), result.status
  25. return jsonify(result)
  26. return decorate