data_routes.py 1.01 KiB
import functools
import json
from flask import Blueprint, request, Response
# render_template, url_for
routes = Blueprint("python-utils-ui-routes", __name__)
VERSION = {
"api": "0.1",
"module": "0.1"
}
def require_accepts_json(f):
"""
used as a route handler decorator to return an error
unless the request allows responses with type "application/json"
:param f: the function to be decorated
:return: the decorated function
"""
@functools.wraps(f)
def decorated_function(*args, **kwargs):
# TODO: use best_match to disallow */* ...?
if not request.accept_mimetypes.accept_json:
return Response(
response="response will be json",
status=406,
mimetype="text/html")
return f(*args, **kwargs)
return decorated_function
@routes.route("/version", methods=['GET', 'POST'])
@require_accepts_json
def version():
return Response(
json.dumps(VERSION),
mimetype="application/json"
)