Esempio n. 1
0
def validate_db_response(db_response_par):

    logger.debug("validate_db_response(db_response_par)")

    if 'status_code' in db_response_par:
        status = db_response_par['status_code']

        if status not in SUCCESS_STATUS_CODES:
            # Generate error message
            error_message = ERROR_DB_PREFIX
            if 'msg' in db_response_par:
                error_message += db_response_par['msg']

            if 'ErrorText' in db_response_par:
                error_message += ', ErrorText: ' + db_response_par['ErrorText'].replace('\"', '')

            # Abort with statusCode and errorMessage
            logger.debug('Abort with: ' + str(status) + ', ' + error_message)
            abort(int(status), str(error_message))
    else:
        abort(500, ERROR_DB_NO_STATUS_CODE)

    logger.debug("validate_db_response(db_response_par): OK")

    return db_response_par
Esempio n. 2
0
def generate_response(return_content_par, status_par):

    if RESPONSE_DEBUG:
        logger.debug("1")

    logger.info('generate_response(return_content, status)')

    if RESPONSE_DEBUG:
        logger.debug("2")

    return_content = return_content_par

    if RESPONSE_DEBUG:
        logger.debug("3")
        logger.debug(repr(return_content))

    try:
        if RESPONSE_DEBUG:
            logger.debug("4")
            logger.debug(repr(return_content))

        return_content = json.dumps(return_content_par).replace("\"[", "[").replace("]\"", "]").replace('\\', '').replace("\"{", "{").replace("}\"", "}")
    except Exception as argument:
        if RESPONSE_DEBUG:
            logger.debug("5")
            logger.debug(repr(return_content))
        abort(500, ERROR_RESPONSE_JSON_PARSE + ' json.dumps(): ' + repr(argument).replace("u'", "").replace("'", ""))

    try:
        if RESPONSE_DEBUG:
            logger.debug("6")
            logger.debug(repr(return_content))
        return_content = json.loads(return_content)
        if RESPONSE_DEBUG:
            logger.debug("7")
            logger.debug(repr(return_content))
    except Exception as argument:
        if RESPONSE_DEBUG:
            logger.debug("8")
            logger.debug(repr(return_content))
        abort(500, ERROR_RESPONSE_JSON_PARSE + ' json.loads(): ' + repr(argument).replace("u'", "").replace("'", ""))

    #Log response content
    if RESPONSE_DEBUG:
        logger.debug("9")
        logger.debug(repr(return_content))
    logger.debug('Response Status: ' + str(status_par) + ', Content: ' + repr(return_content))

    # Because 204 has no body content
    if status_par == '204':
        response = make_response()
        response.status_code = 204
    else:
        response = make_response(jsonify({'requestURL': request.url, 'requestMethod': request.method, 'requestStatusCode': status_par, 'content': return_content}))
        response.headers['Content-Type'] = 'application/json'
        response.status_code = int(status_par)

    logger.debug('Response: ' + repr(response))
    return response