An ebook/comic library service and web client
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.

35 lines
856 B

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