api_response.py 2.1 KB
# -*- coding: utf-8 -*-


from server.log import log


# Customize Formatted Response
# with status code 200 and the real status code and message in body


def ok(data=None, message=""):
    return {
        "status": "success",
        "code": 200,
        "data": data,
        "message": message
    }


def __response_with_code(code, message, friendly_message=""):
    log.debug("response with code: %d and message: %s" % (code, message))
    return {
        "status": "failed",
        "code": code,
        "message": message,
        "friendly_message": friendly_message
    }


def bad_request(message="",
                friendly_message=(
                        'Your request does not make sense'
                )):
    return __response_with_code(400, message, friendly_message)


def unauthorized(message="",
                 friendly_message=(
                         'Please login to get a valid token')):
    return __response_with_code(401, message, friendly_message)


def forbidden(message="",
              friendly_message=(
                      'You don\'t have the permission to access the request')):
    return __response_with_code(403, message, friendly_message)


def not_found(message="",
              friendly_message=(
                      'The requested URL was not found on the server.')):
    return __response_with_code(404, message, friendly_message)


def internal_server_error(message="",
                          friendly_message=(
                                  'Encountered an internal error ,'
                                  'unable to complete your request.'
                                  'Either the server is overloaded or there '
                                  'is an error in the application.'
                          )):
    return __response_with_code(500, message, friendly_message)


def insecurity_request(message="",
                       friendly_message=(
                           'Invalid request ipaddress which not in'
                           'platform\'s white list.'
                       )):
    return __response_with_code(412, message, friendly_message)