ShakeM
10/23/2018 - 2:59 PM

Restful Flask

from flask import jsonify


class HttpCode:
    success = 200
    unauth_error = 401
    params_error = 400
    server_error = 500


def restful_result(code, message, data):
    return jsonify(dict(
        code=code,
        message=message,
        data=data or {}
    ))


def success(message='', data=None):
    return restful_result(code=HttpCode.success, message=message, data=data)


def unauth_error(message='', data=None):
    return restful_result(code=HttpCode.unauth_error, message=message, data=data)


def params_error(message='', data=None):
    return restful_result(code=HttpCode.params_error, message=message, data=data)


def server_error(message='', data=None):
    return restful_result(code=HttpCode.server_error, message=message, data=data)