Esempio n. 1
0
def getter(msg):
    msg_as_str = msg.decode('utf-8')    
    msg_as_dict = fix_json_quotings(msg_as_str)
    
    try:
        conn = MSQL(get_conf(['DBs',msg_as_dict['db'],'db_cred','host']),
                get_conf(['DBs',msg_as_dict['db'],'db_cred','user']),
                str(get_conf(['DBs',msg_as_dict['db'],'db_cred','password'])),
                msg_as_dict['db'])
    except:
        return("ERROR: couldn't connect with given credentials")
    
    msg_as_dict.pop('db')
    table = msg_as_dict.pop('table')

    if 'record_id' in msg_as_dict:
        result = conn.find_record(table,msg_as_dict['record_id'])
    elif len(msg_as_dict)==0:
        result = conn.find_all_records(table)
        result = list_of_tuples_to_list_of_lists(result)
    else:
        column =  str(list(msg_as_dict.keys())[0])
        value = str(list(msg_as_dict.values())[0])
        result = conn.find_records(table, column, value)
        print(result)
        if type(result) == type(['list','list']):
            result = list_of_tuples_to_list_of_lists(result)
        else:
            pass
    return(result)
Esempio n. 2
0
def put(db, table, record_id):
    if db not in get_conf(['DBs']):
        return ("ERROR: db given not specified in the configuration"), 400
    elif table not in get_conf(['DBs', db, 'tables']):
        return ("ERROR: table given not specified in the configuration"), 400
    else:
        if record_id == None:
            return ("ERROR: no record id specified in the url"), 400
        else:
            try:
                body = request.get_json()
                if body == None:
                    return (
                        "ERROR: To put a record, you must send it in a JSON"
                    ), 400
            except:
                return (
                    "ERROR: To put a record, you must send it in a JSON"), 400

            body['db'] = db
            body['table'] = table
            body["record_id"] = record_id
            result = rabbit.send_n_receive(
                get_conf(['RabbitMQ', 'queues', 'put_queue']), body)
            if result.startswith('b"ERROR') == True:
                return result, 400
            else:
                result = result.replace('b"', '')
                result = result.replace('"', '')
                result = fix_json_quotings(result)
                return jsonify(result)
Esempio n. 3
0
def get(db, table, record_id=None):
    if db not in get_conf(['DBs']):
        return ("ERROR: db given not specified in the configuration"), 400
    elif table not in get_conf(['DBs', db, 'tables']):
        return ("ERROR: table given not specified in the configuration"), 400
    else:
        if record_id == None:
            try:
                body = request.get_json()
                if body == None:
                    return (
                        "ERROR: To get a record not by id, you must send it in a JSON"
                    ), 400
            except:
                return (
                    "ERROR: To get a record not by id, you must send it in a JSON"
                ), 400

            body['db'] = db
            body['table'] = table
            result = rabbit.send_n_receive(
                get_conf(['RabbitMQ', 'queues', 'get_queue']), body)

            if result.startswith('b"ERROR') == True:
                return result, 400
            # ie if the query found zero results
            elif result == "b'[]'":
                return jsonify([])
            else:
                result = result.replace('b"', '').replace('"', '').replace(
                    'None', '"None"')
                result = fix_json_quotings(result)
                return jsonify(result)

        else:
            body = {"record_id": record_id, "db": db, "table": table}
            result = rabbit.send_n_receive(
                get_conf(['RabbitMQ', 'queues', 'get_queue']), body)

            if result.startswith('b"ERROR') == True:
                return result, 400
            else:
                result = result.replace('b"', '').replace('"', '').replace(
                    'None', '"None"')
                result = fix_json_quotings(result)
                return jsonify(result)
Esempio n. 4
0
def deleter(msg):
    msg_as_str = msg.decode('utf-8')
    msg_as_dict = fix_json_quotings(msg_as_str)

    try:
        conn = MSQL(
            get_conf(['DBs', msg_as_dict['db'], 'db_cred', 'host']),
            get_conf(['DBs', msg_as_dict['db'], 'db_cred', 'user']),
            str(get_conf(['DBs', msg_as_dict['db'], 'db_cred', 'password'])),
            msg_as_dict['db'])
    except:
        return ("ERROR: couldn't connect with given credentials")

    result = conn.delete_record(msg_as_dict['table'], msg_as_dict['record_id'])
    return (result)
Esempio n. 5
0
def poster(msg):
    msg_as_str = msg.decode('utf-8')    
    msg_as_dict = fix_json_quotings(msg_as_str)
    
    try:
        conn = MSQL(get_conf(['DBs',msg_as_dict['db'],'db_cred','host']),
                get_conf(['DBs',msg_as_dict['db'],'db_cred','user']),
                str(get_conf(['DBs',msg_as_dict['db'],'db_cred','password'])),
                msg_as_dict['db'])
    except:
        return("ERROR: couldn't connect with given credentials")
    msg_as_dict.pop('db')
    table = msg_as_dict.pop('table')
    
    record = conn.insert_record(table, tuple(msg_as_dict.keys()), tuple(msg_as_dict.values()))
    return(record)
Esempio n. 6
0
def delete(db, table, record_id):
    if db not in get_conf(['DBs']):
        return ("ERROR: db given not specified in the configuration"), 400
    elif table not in get_conf(['DBs', db, 'tables']):
        return ("ERROR: table given not specified in the configuration"), 400
    else:
        if record_id == None:
            return ("ERROR: no record id specified in the url"), 400
        else:
            body = {"record_id": record_id, "db": db, "table": table}
            result = rabbit.send_n_receive(
                get_conf(['RabbitMQ', 'queues', 'delete_queue']), body)

            if result.startswith('b"ERROR') == True:
                return result, 400
            else:
                result = result.replace('b"', '')
                result = result.replace('"', '')
                result = fix_json_quotings(result)
                return jsonify(result)