def handle_resource(dbname, resource_name, primary_key): resp = Response("Internal server error", status=500, mimetype="text/plain") try: # The design pattern is that the primary key comes in in the form value1_value2_value3 key_columns = primary_key.split(key_delimiter) # Merge dbname and resource names to form the dbschema.tablename element for the resource. # This should probably occur in the data service and not here. resource = dbname + "." + resource_name if request.method == 'GET': # Look for the fields=f1,f2, ... argument in the query parameters. field_list = request.args.get('fields', None) if field_list is not None: field_list = field_list.split(",") # Call the data service layer. result = ds.get_by_primary_key(resource, key_columns, field_list=field_list) if result: result = process_result(result, dbname, resource_name, primary_key) result = {'data': result} result = compute_links(result) result_data = json.dumps(result, default=str) resp = Response(result_data, status=200, mimetype='application/json') else: resp = Response("Not found", status=404, mimetype="text/plain") if result: # We managed to find a row. Return JSON data and 200 result_data = json.dumps(result, default=str) resp = Response(result_data, status=200, mimetype='application/json') else: resp = Response("NOT FOUND", status=404, mimetype='text/plain') elif request.method == 'DELETE': result = ds.delete(resource, key_columns) if result and result > 0: resp = Response("OK", status=200, mimetype='text/plain') else: resp = Response("NOT FOUND", status=404, mimetype='text/plain') elif request.method == 'PUT': new_r = request.get_json() result = ds.update(resource, key_columns, new_r) if result and result == 1: resp = Response("OK UPDATED", status=200, mimetype="text/plain") else: resp = Response("I don't recognize this method", status = 422, mimetype="text/plain") except Exception as e: # We need a better overall approach to generating correct errors. utils.debug_message("Something awful happened, e = ", e) return resp
def handle_resource(dbname, resource_name, primary_key): resp = Response("Internal server error", status=500, mimetype="text/plain") try: # The design pattern is that the primary key comes in in the form value1_value2_value3 key_columns = primary_key.split(key_delimiter) # Merge dbname and resource names to form the dbschema.tablename element for the resource. # This should probably occur in the data service and not here. resource = dbname + "." + resource_name if request.method == "GET": # Look for the fields=f1,f2, ... argument in the query parameters. field_list = request.args.get('fields', None) if field_list is not None: field_list = field_list.split(",") # Call the data service layer. result = ds.get_by_primary_key(resource, key_columns, field_list=field_list) if result: # We managed to find a row. Return JSON data and 200 result_data = json.dumps(result, default=str) resp = Response(result_data, status=200, mimetype='application/json') else: resp = Response("NOT FOUND", status=404, mimetype='text/plain') elif request.method == "DELETE": result = ds.delete(resource, key_columns) if result and result >= 1: resp = Response("OK", status=200, mimetype='text/plain') else: resp = Response("NOT FOUND", status=404, mimetype='text/plain') else: resp = Response("I am a teapot thath will not PUT", status=422, mimetype='text/plain') # if result: # # We managed to find a row. Return JSON data and 200 # result_data = json.dumps(result, default=str) # resp = Response(result_data, status=200, mimetype='application/json') # else: # # We did not get an exception and we did not get data, therefore this is 404 not found. # resp = Response("Not found", status=404, mimetype="text/plain") except Exception as e: # We need a better overall approach to generating correct errors. utils.debug_message("Something awlful happened, e = ", e) return resp
def handle_tables(dbname, resource_name, resource_name2, primary_key): resp = Response("Internal server error", status=500, mimetype="text/plain") try: key_columns = primary_key.split(key_delimiter) # Form the compound resource names dbschema.table_name # resource = dbname + "." + resource_name # resource2 = dbname + "." + resource_name2 if request.method == "GET": # Get the field list if it exists. field_list = request.args.get('fields', None) if field_list is not None: field_list = field_list.split(",") limit = request.args.get("limit", "10") offset = request.args.get("offset", "0") order_by = request.args.get("order_by", None) # The query string is of the form ?f1=v1&f2=v2& ... # This maps to a query template of the form { "f1" : "v1", ... } # We need to ignore the fields parameters. tmp = None for k, v in request.args.items(): if (not k == "fields") and (not k == "limit") and (not k == "offset") and \ (not k == "order_by"): if tmp is None: tmp = {} tmp[k] = v # Find by template. join_paths = ds.get_join_column_mapping(dbname, resource_name, dbname, resource_name2) if join_paths: columns = join_paths[list(join_paths.keys())[0]]["MAP"][0] referenced_table_name = columns["REFERENCED_TABLE_NAME"] referenced_column_name = columns["REFERENCED_COLUMN_NAME"] table_name = columns["TABLE_NAME"] column_name = columns["COLUMN_NAME"] referenced_row = ds.get_by_primary_key(dbname +"."+ referenced_table_name, key_columns, \ field_list=None, commit=True) tmp[column_name] = referenced_row[column_name] result = ds.get_by_template(dbname +"."+ table_name, tmp, \ field_list=field_list, limit=limit, offset=offset, order_by=order_by, commit=True) result = {"data": result} result = compute_links(result, limit, offset) result_data = json.dumps(result, default=str) resp = Response(result_data, status=200, mimetype='application/json') else: resp = Response("Not found", status=404, mimetype="text/plain") elif request.method == "POST": join_paths = ds.get_join_column_mapping(dbname, resource_name, dbname, resource_name2) if join_paths: columns = join_paths[list(join_paths.keys())[0]]["MAP"][0] referenced_table_name = columns["REFERENCED_TABLE_NAME"] referenced_column_name = columns["REFERENCED_COLUMN_NAME"] table_name = columns["TABLE_NAME"] column_name = columns["COLUMN_NAME"] new_r = request.get_json() referenced_row = ds.get_by_primary_key(dbname + "." + referenced_table_name, key_columns, field_list=None, commit=True) new_r[column_name] = referenced_row[column_name] result = ds.create(dbname + "." + table_name, new_r) if result and result == 1: resp = Response("CREATED", status=201, mimetype="text/plain") else: resp = Response("Not found", status=404, mimetype="text/plain") except Exception as e: utils.debug_message("Something awlful happened, e = ", e) return resp
def handle_path_resource(dbname, resource_name, primary_key, subresource_name): resp = Response("Internal server error", status=500, mimetype="text/plain") try: resource = dbname + "." + resource_name subresource = dbname + "." + subresource_name ##make args mutable request.args = request.args.copy() actual_key = ds.get_key(resource) keys_input = primary_key.split('_') if len(keys_input) != len(actual_key): raise ValueError("Wrong key length please try " + actual_key) benchmark_row = ds.get_by_primary_key(resource, keys_input) if request.method == 'GET': # Form the compound resource names dbschema.table_name foreign_key = ds.get_foreign_key(resource, subresource) for k, v in foreign_key.items(): map = v['MAP'][0] request.args[map['REFERENCED_COLUMN_NAME']] = benchmark_row[0][ map['COLUMN_NAME']] # Get the field list if it exists. field_list = request.args.get('fields', None) if field_list is not None: field_list = field_list.split(",") # for queries of type 127.0.0.1:5000/api/lahman2017/batting?teamID=BOS&limit=10 limit = min(int(request.args.get('limit', 10)), 10) offset = request.args.get('offset', 0) order_by = request.args.get('order_by', None) children = request.args.get('children', None) # The query string is of the form ?f1=v1&f2=v2& ... # This maps to a query template of the form { "f1" : "v1", ... } # We need to ignore the fields parameters. tmp = None for k, v in request.args.items(): if (not k == 'fields') and (not k == 'limit') and (not k=='offset') and \ (not k == 'order_by') and (not k == 'children'): if tmp is None: tmp = {} tmp[k] = v # Find by template result = ds.get_by_template(subresource, tmp, field_list=field_list, limit=limit, offset=offset, order_by=order_by, children=children) if result: result = {"data": result} result = compute_links(result, limit, offset) result_data = json.dumps(result, default=str) resp = Response(result_data, status=200, mimetype='application/json') else: resp = Response("Not found", status=404, mimetype="text/plain") elif request.method == 'POST': actual_key = ds.get_key(resource) keys_input = primary_key.split('_') if len(keys_input) != len(actual_key): raise ValueError("Wrong key length please try " + actual_key) new_r = request.get_json() foreign_key = ds.get_foreign_key(resource, subresource) for k, v in foreign_key.items(): map = v['MAP'][0] new_r[map['COLUMN_NAME']] = benchmark_row[0][ map['REFERENCED_COLUMN_NAME']] ####Might need to handle multiple keys here k = result = ds.create(subresource, new_r) if k: location = get_location(dbname, subresource_name, k) resp = Response("CREATED", status=201, mimetype="text/plain") resp.headers['Location'] = location except Exception as e: resp = Response("STATUS 422 " + str(e), status=422, mimetype="text/plain") utils.debug_message("Something awlful happened, e = ", e) return resp
def handle_resource(dbname, resource_name, primary_key): resp = Response("Internal server error", status=500, mimetype="text/plain") try: # The design pattern is that the primary key comes in in the form value1_value2_value3 key_columns = primary_key.split(key_delimiter) # Merge dbname and resource names to form the dbschema.tablename element for the resource. # This should probably occur in the data service and not here. resource = dbname + "." + resource_name if request.method == 'GET': # Look for the fields=f1,f2, ... argument in the query parameters. field_list = request.args.get('fields', None) if field_list is not None: field_list = field_list.split(",") # Call the data service layer. result = ds.get_by_primary_key(resource, key_columns, field_list=field_list) if result: # We managed to find a row. Return JSON data and 200 result = {"data": result} result_data = json.dumps(result, default=str) resp = Response(result_data, status=200, mimetype='application/json') else: resp = Response("NOT FOUND", status=404, mimetype='text/plain') elif request.method == 'DELETE': result = ds.delete_by_key(resource, key_columns) if result and result >= 1: resp = Response("OK", status=200, mimetype='text/plain') else: resp = Response("NOT FOUND", status=404, mimetype='text/plain') #@TODO DO THIS UPDATE ROW BASED ON KEY elif request.method == 'PUT': check = ds.get_by_primary_key(resource, key_columns) ###Create if result is None if check is None: resp = Response("NOT FOUND", status=404, mimetype="text/plain") ###Update if result is not None else: new_r = request.get_json() result = ds.update_by_key(resource, key_columns, new_r) if (not result is None) and (result == 1): resp = Response("UPDATED", status=201, mimetype="text/plain") if (not result is None) and (result == 0): resp = Response( "UPDATED BUT YOU ENTERED THE SAME VALUES. NONE CHANGED ", status=201, mimetype="text/plain") else: result = Response( "Unimplmented method type please try GET, PUT, or DELETE", status=500, mimetype="text/plain") return result except Exception as e: resp = Response("STATUS 422 " + str(e), status=422, mimetype="text/plain") utils.debug_message("Something awlful happened, e = ", e) return resp