11"""Flask blueprint for Multinet REST API."""
2- import json
3-
4- from flask import Blueprint , request , Response
2+ from flask import Blueprint , request
53from webargs import fields
64from webargs .flaskparser import use_kwargs
75
8- from . import db
6+ from . import db , util
97from .errors import ValidationFailed
108
119bp = Blueprint ("multinet" , __name__ )
12-
13-
14- def generate (iterator ):
15- """Return a generator that yields an iterator's contents into a JSON list."""
16- yield "["
17-
18- comma = ""
19- for row in iterator :
20- yield f"{ comma } { json .dumps (row )} "
21- comma = ","
22-
23- yield "]"
24-
25-
26- def stream (iterator ):
27- """Convert an iterator to a Flask response."""
28- return Response (generate (iterator ), mimetype = "application/json" )
29-
30-
31- def require_db ():
32- """Check if the db is live."""
33- if not db .check_db ():
34- return ("" , "500 Database Not Live" )
35-
36-
37- bp .before_request (require_db )
10+ bp .before_request (util .require_db )
3811
3912
4013@bp .route ("/workspaces" , methods = ["GET" ])
4114def get_workspaces ():
4215 """Retrieve list of workspaces."""
43- return stream (db .get_workspaces ())
16+ return util . stream (db .get_workspaces ())
4417
4518
4619@bp .route ("/workspaces/<workspace>" , methods = ["GET" ])
@@ -54,22 +27,22 @@ def get_workspace(workspace):
5427def get_workspace_tables (workspace , type = "all" ):
5528 """Retrieve the tables of a single workspace."""
5629 tables = db .workspace_tables (workspace , type )
57- return stream (tables )
30+ return util . stream (tables )
5831
5932
6033@bp .route ("/workspaces/<workspace>/tables/<table>" , methods = ["GET" ])
6134@use_kwargs ({"offset" : fields .Int (), "limit" : fields .Int ()})
6235def get_table_rows (workspace , table , offset = 0 , limit = 30 ):
6336 """Retrieve the rows and headers of a table."""
6437 rows = db .workspace_table (workspace , table , offset , limit )
65- return stream (rows )
38+ return util . stream (rows )
6639
6740
6841@bp .route ("/workspaces/<workspace>/graphs" , methods = ["GET" ])
6942def get_workspace_graphs (workspace ):
7043 """Retrieve the graphs of a single workspace."""
7144 graphs = db .workspace_graphs (workspace )
72- return stream (graphs )
45+ return util . stream (graphs )
7346
7447
7548@bp .route ("/workspaces/<workspace>/graphs/<graph>" , methods = ["GET" ])
@@ -121,7 +94,7 @@ def aql(workspace):
12194 return (query , "400 Malformed Request Body" )
12295
12396 result = db .aql_query (workspace , query )
124- return stream (result )
97+ return util . stream (result )
12598
12699
127100@bp .route ("/workspaces/<workspace>" , methods = ["DELETE" ])
0 commit comments