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
849 B
33 lines
849 B
"""Decorators to be used in the api module."""
|
|
from functools import wraps
|
|
from typing import Callable, Any
|
|
|
|
from flask import jsonify, Response
|
|
|
|
from atheneum.api.model import APIResponse
|
|
|
|
|
|
def return_json(func: Callable) -> Callable:
|
|
"""
|
|
If an Response object is not returned, jsonify the result and return it.
|
|
|
|
:param func:
|
|
:return:
|
|
"""
|
|
@wraps(func)
|
|
def decorate(*args: list, **kwargs: dict) -> Any:
|
|
"""
|
|
Make sure that the return of the function is jsonified into a Response.
|
|
|
|
:param args:
|
|
:param kwargs:
|
|
:return:
|
|
"""
|
|
result = func(*args, **kwargs)
|
|
if isinstance(result, Response):
|
|
return result
|
|
if isinstance(result, APIResponse):
|
|
return jsonify(result), result.status
|
|
return jsonify(result)
|
|
|
|
return decorate
|