diff --git a/dj_gui_api_server/DJConnector.py b/dj_gui_api_server/DJConnector.py index 329c3d9..c179681 100644 --- a/dj_gui_api_server/DJConnector.py +++ b/dj_gui_api_server/DJConnector.py @@ -30,7 +30,7 @@ def attempt_login(database_address, username, password): List all schemas under the database Parameters: - jwt_payload (dict): dictionary containing databaseAddress, username and password strings + jwt_payload (dict): Dictionary containing databaseAddress, username and password strings Returns: list: List of schemas names @@ -47,7 +47,8 @@ def list_schemas(jwt_payload): List all tables and their type give a schema Parameters: - jwt_payload (dict): dictionary containing databaseAddress, username and password strings + jwt_payload (dict): Dictionary containing databaseAddress, username and password strings + schema_name (str): Name of schema to list all tables from Returns: dict(manual_tables=[], @@ -83,7 +84,43 @@ def list_tables(jwt_payload, schema_name): print(table_name + ' is of unknown table type') return tables_dict_list + + """ + Get all tuples from table + + Parameters: + jwt_payload (dict): Dictionary containing databaseAddress, username and password strings + schema_name (string): Schema name where to find the table under + table_name (string): Table name under the given schema, must be in camel case + + Returns: + list: a list of tuples in dict form + """ + @staticmethod + def fetch_tuples(jwt_payload, schema_name, table_name): + DJConnector.set_datajoint_config(jwt_payload) + + schema_virtual_module = dj.create_virtual_module(schema_name, schema_name) + return getattr(schema_virtual_module, table_name).fetch(as_dict=True) + """ + Get the table definition + + Parameters: + jwt_payload (dict): Dictionary containing databaseAddress, username and password strings + schema_name (string): Schema name where to find the table under + table_name (string): Table name under the given schema, must be in camel case + + Returns: + string: definition of the table + """ + @staticmethod + def get_table_definition(jwt_payload, schema_name, table_name): + DJConnector.set_datajoint_config(jwt_payload) + + schema_virtual_module = dj.create_virtual_module(schema_name, schema_name) + return getattr(schema_virtual_module, table_name).describe() + """ Method to set credentials for database @@ -100,14 +137,15 @@ def set_datajoint_config(jwt_payload): dj.config['database.password'] = jwt_payload['password'] dj.conn(reset=True) + """ Helper method for converting snake to camel case Parameters: - string (string): string in snake format to convert to camel case + string (string): String in snake format to convert to camel case Returns: - string: string formated in CamelCase notation + string: String formated in CamelCase notation """ @staticmethod def snake_to_camel_case(string): diff --git a/dj_gui_api_server/DJGUIAPIServer.py b/dj_gui_api_server/DJGUIAPIServer.py index 60b8cce..7360552 100644 --- a/dj_gui_api_server/DJGUIAPIServer.py +++ b/dj_gui_api_server/DJGUIAPIServer.py @@ -106,5 +106,48 @@ def list_tables(jwt_payload): except Exception as e: return str(e), 500 +""" +Route to fetch all tuples for a given table + +Parameters: + header: (html:GET:Authorization): Must include in format of: bearer + body: (html:POST:JSON): {"schemaName": , "tableName": } (NOTE: Table name must be in CamalCase) + +Returns: + dict(table_definition=table_definition): table_definition will be in string format + or + dict(error=): With error message of why it failed +""" +@app.route('/api/fetch_tuples', methods=['POST']) +@protected_route +def fetch_tuples(jwt_payload): + try: + table_tuples = DJConnector.fetch_tuples(jwt_payload, request.json["schemaName"], request.json["tableName"]) + return dict(tuples = table_tuples) + except Exception as e: + return str(e), 500 + +""" +Route to get table definition + +Parameters: + header: (html:GET:Authorization): Must include in format of: bearer + body: (html:POST:JSON): {"schemaName": , "tableName": } (NOTE: Table name must be in CamalCase) + +Returns: + string: The table definition + or + dict(error=): With error message of why it failed + +""" +@app.route('/api/get_table_definition', methods=['POST']) +@protected_route +def get_table_definition(jwt_payload): + try: + table_definition = DJConnector.get_table_definition(jwt_payload, request.json["schemaName"], request.json["tableName"]) + return table_definition + except Exception as e: + return str(e), 500 + if __name__ == '__main__': app.run(host='0.0.0.0', port=5000) \ No newline at end of file