Skip to content
This repository was archived by the owner on Feb 23, 2022. It is now read-only.

Commit f1daea8

Browse files
committed
Move utility functions into own module
1 parent 8f84c6d commit f1daea8

File tree

6 files changed

+53
-42
lines changed

6 files changed

+53
-42
lines changed

multinet/api.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,19 @@
11
"""Flask blueprint for Multinet REST API."""
2-
import json
3-
4-
from flask import Blueprint, request, Response
2+
from flask import Blueprint, request
53
from webargs import fields
64
from webargs.flaskparser import use_kwargs
75

8-
from . import db
6+
from . import db, util
97
from .errors import ValidationFailed
108

119
bp = 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"])
4114
def 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):
5427
def 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()})
6235
def 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"])
6942
def 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"])

multinet/errors.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,13 @@ def __init__(self, errors):
119119
self.errors = errors
120120

121121
def flask_response(self):
122-
"""Generate a 400 error for the bad argument."""
122+
"""Generate a 400 error."""
123123
return ({"errors": self.errors}, "400 Validation Failed")
124+
125+
126+
class DatabaseNotLive(ServerError):
127+
"""Exception for when Arango database is not live."""
128+
129+
def flask_response(self):
130+
"""Generate a 500 error."""
131+
return ("", "500 Database Not Live")

multinet/uploaders/csv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from io import StringIO
44
import re
55

6-
from .. import db, api
6+
from .. import db, util
77

88
from flask import Blueprint, request
99
from flask import current_app as app
1010

1111
bp = Blueprint("csv", __name__)
12-
bp.before_request(api.require_db)
12+
bp.before_request(util.require_db)
1313

1414

1515
def validate_csv(rows):

multinet/uploaders/nested_json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
import itertools
33
import json
44

5-
from .. import db, api
5+
from .. import db, util
66

77
from flask import Blueprint, request
88

99
bp = Blueprint("nested_json", __name__)
10-
bp.before_request(api.require_db)
10+
bp.before_request(util.require_db)
1111

1212

1313
def analyze_nested_json(data, int_table_name, leaf_table_name):

multinet/uploaders/newick.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import uuid
33
import newick
44

5-
from .. import db, api
5+
from .. import db, util
66

77
from flask import Blueprint, request
88
from flask import current_app as app
99

1010
bp = Blueprint("newick", __name__)
11-
bp.before_request(api.require_db)
11+
bp.before_request(util.require_db)
1212

1313

1414
@bp.route("/<workspace>/<table>", methods=["POST"])

multinet/util.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Utility functions."""
2+
import json
3+
4+
from flask import Response
5+
6+
from . import db
7+
from .errors import DatabaseNotLive
8+
9+
10+
def generate(iterator):
11+
"""Return a generator that yields an iterator's contents into a JSON list."""
12+
yield "["
13+
14+
comma = ""
15+
for row in iterator:
16+
yield f"{comma}{json.dumps(row)}"
17+
comma = ","
18+
19+
yield "]"
20+
21+
22+
def stream(iterator):
23+
"""Convert an iterator to a Flask response."""
24+
return Response(generate(iterator), mimetype="application/json")
25+
26+
27+
def require_db():
28+
"""Check if the db is live."""
29+
if not db.check_db():
30+
raise DatabaseNotLive()

0 commit comments

Comments
 (0)