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)
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)
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)
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)
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)
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)
from pyTools.MySQL_Class.MySQL_Class import MSQL from pyTools.RabbitMQ_Class.RabbitClass import Rabbit from pyTools.extra_tools import get_conf, fix_json_quotings # Upon execution, first wait until it can find the config, # and can connect to the rabbit server and mysql server. # When first connecting to mysql, it will try to initialize the databases and # tables in the config. is_rabbit_up, is_mysql_up, is_conf_up = False, False, False print("PUT consumer awaiting config") while is_conf_up is False: try: confer = get_conf(['DBs']) is_conf_up = True except: pass print("PUT consumer awaiting connection to RabbitMQ") while is_rabbit_up is False: try: rab = Rabbit(host=get_conf(['RabbitMQ', 'host'])) is_rabbit_up = True rab.close_connection() except: pass print("PUT consumer awaiting connection to MySQL") while is_mysql_up is False: try: mas = conn = MSQL(get_conf(['mysql_cred','host']), get_conf(['mysql_cred','user']), str(get_conf(['mysql_cred','password']))) is_mysql_up = True
from pyTools.RabbitMQ_Class.RabbitClass import Rabbit from pyTools.extra_tools import get_conf, fix_json_quotings from flask import Flask, request, jsonify import json # Upon execution, first wait until it can find the config, # and can connect to the rabbit server. is_rabbit_up, is_conf_up = False, False print("Web API awaiting config") while is_conf_up is False: try: confer = get_conf(['DBs']) is_conf_up = True except: pass print("Web API awaiting connection to RabbitMQ") while is_rabbit_up is False: try: rab = Rabbit(host=get_conf(['RabbitMQ', 'host'])) is_rabbit_up = True rab.close_connection() except: pass # Initialize the flask server and rabbit object app = Flask(__name__) rabbit = Rabbit(get_conf(['RabbitMQ', 'host'])) # Declare the queues the the server is going to send messages to. rabbit.declare_queue(get_conf(['RabbitMQ', 'queues', 'post_queue']), durable=True) rabbit.declare_queue(get_conf(['RabbitMQ', 'queues', 'get_queue']),
from pyTools.extra_tools import get_conf from pyTools.MySQL_Class.MySQL_Class import MSQL # a simple script that iterates through the config an initialized the databases and tables. conn = MSQL(get_conf(['mysql_cred', 'host']), get_conf(['mysql_cred', 'user']), str(get_conf(['mysql_cred', 'password']))) dbs = get_conf(['DBs']) for db in dbs: conn.create_db(db) print(db + " db initialized") tables = get_conf(['DBs', db, 'tables']) for table in tables: conn.create_table(table, get_conf(['DBs', db, 'tables', table])) print(table + " table initialized")