Skip to content

Commit 9a0881a

Browse files
committed
Added fetch_tuple route
1 parent 5d9c751 commit 9a0881a

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

dj_gui_api_server/DJConnector.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def attempt_login(database_address, username, password):
3030
List all schemas under the database
3131
3232
Parameters:
33-
jwt_payload (dict): dictionary containing databaseAddress, username and password strings
33+
jwt_payload (dict): Dictionary containing databaseAddress, username and password strings
3434
3535
Returns:
3636
list<strings>: List of schemas names
@@ -47,7 +47,8 @@ def list_schemas(jwt_payload):
4747
List all tables and their type give a schema
4848
4949
Parameters:
50-
jwt_payload (dict): dictionary containing databaseAddress, username and password strings
50+
jwt_payload (dict): Dictionary containing databaseAddress, username and password strings
51+
schema_name (str): Name of schema to list all tables from
5152
5253
Returns:
5354
dict(manual_tables=[<table_names>],
@@ -83,6 +84,24 @@ def list_tables(jwt_payload, schema_name):
8384
print(table_name + ' is of unknown table type')
8485

8586
return tables_dict_list
87+
88+
"""
89+
Get all tuples from table
90+
91+
Parameters:
92+
jwt_payload (dict): Dictionary containing databaseAddress, username and password strings
93+
schema_name (string): Schema name where to find the table under
94+
table_name (string): Table name under the given schema, must be in camel case
95+
96+
Returns:
97+
(tuples)
98+
"""
99+
@staticmethod
100+
def fetch_tuples(jwt_payload, schema_name, table_name):
101+
DJConnector.set_datajoint_config(jwt_payload)
102+
103+
schema_virtual_module = dj.create_virtual_module(schema_name, schema_name)
104+
return getattr(schema_virtual_module, table_name).fetch(as_dict=True)
86105

87106
"""
88107
Method to set credentials for database
@@ -104,10 +123,10 @@ def set_datajoint_config(jwt_payload):
104123
Helper method for converting snake to camel case
105124
106125
Parameters:
107-
string (string): string in snake format to convert to camel case
126+
string (string): String in snake format to convert to camel case
108127
109128
Returns:
110-
string: string formated in CamelCase notation
129+
string: String formated in CamelCase notation
111130
"""
112131
@staticmethod
113132
def snake_to_camel_case(string):

dj_gui_api_server/DJGUIAPIServer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,26 @@ def list_tables(jwt_payload):
106106
except Exception as e:
107107
return str(e), 500
108108

109+
"""
110+
Route to fetch all tuples for a given table
111+
112+
Parameters:
113+
header: (html:GET:Authorization): Must include in format of: bearer <JWT-Token>
114+
body: (html:POST:JSON): {"schemaName": <schema_name>, "tableName": <table_name>} (NOTE: Table name must be in CamalCase)
115+
116+
Returns:
117+
dict(tuples=[tuples_as_dicts])
118+
or
119+
dict(error=<error_message>): With error message of why it failed
120+
"""
121+
@app.route('/api/fetch_tuples', methods=['POST'])
122+
@protected_route
123+
def fetch_tuples(jwt_payload):
124+
try:
125+
table_tuples = DJConnector.fetch_tuples(jwt_payload, request.json["schemaName"], request.json["tableName"])
126+
return dict(tuples = table_tuples)
127+
except Exception as e:
128+
return str(e), 500
129+
109130
if __name__ == '__main__':
110131
app.run(host='0.0.0.0', port=5000)

0 commit comments

Comments
 (0)