-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapi_v1.py
More file actions
226 lines (192 loc) · 7.93 KB
/
api_v1.py
File metadata and controls
226 lines (192 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import flask
from relation_engine_server.utils import (
arango_client,
spec_loader,
auth,
bulk_import,
pull_spec,
config,
parse_json,
ensure_specs,
)
from relation_engine_server.utils.json_validation import run_validator
from relation_engine_server.exceptions import InvalidParameters
api_v1 = flask.Blueprint("api_v1", __name__)
@api_v1.route("/data_sources", methods=["GET"])
def list_data_sources():
# note the custom response format is used by the frontend, so this endpoint is provided
# in addition to the /specs/data_sources endpoint
data_sources = spec_loader.get_names("data_sources")
return flask.jsonify({"data_sources": data_sources})
@api_v1.route("/data_sources/<name>", methods=["GET"])
def fetch_data_source(name):
data_source = spec_loader.get_schema("data_source", name)
return flask.jsonify({"data_source": data_source})
@api_v1.route("/specs/data_sources", methods=["GET"])
def show_data_sources():
"""Show the current data sources loaded from the spec."""
name = flask.request.args.get("name")
if name:
return flask.jsonify(spec_loader.get_schema("data_source", name))
return flask.jsonify(spec_loader.get_names("data_sources"))
@api_v1.route("/specs/stored_queries", methods=["GET"])
def show_stored_queries():
"""Show the current stored query names loaded from the spec."""
name = flask.request.args.get("name")
if name:
return flask.jsonify(
{"stored_query": spec_loader.get_schema("stored_query", name)}
)
return flask.jsonify(spec_loader.get_names("stored_query"))
@api_v1.route("/specs/collections", methods=["GET"])
@api_v1.route("/specs/schemas", methods=["GET"])
def show_collections():
"""Show the names of the (document) collections (edges and vertices) loaded from the spec."""
name = flask.request.args.get("name")
doc_id = flask.request.args.get("doc_id")
if name:
return flask.jsonify(spec_loader.get_schema("collection", name))
elif doc_id:
return flask.jsonify(spec_loader.get_schema_for_doc(doc_id))
else:
return flask.jsonify(spec_loader.get_names("collection"))
@api_v1.route("/query_results", methods=["POST"])
def run_query():
"""
Run a stored query as a query against the database.
Auth:
- only kbase re admins for ad-hoc queries
- public stored queries (these have access controls within them based on params)
"""
json_body = parse_json.get_json_body() or {}
# fetch number of documents to return
batch_size = int(flask.request.args.get("batch_size", 10000))
full_count = flask.request.args.get("full_count", False)
if "query" in json_body:
# Run an adhoc query for a sysadmin
auth.require_auth_token(roles=["RE_ADMIN"])
query_text = _preprocess_stored_query(json_body["query"], json_body)
del json_body["query"]
if "ws_ids" in query_text:
# Fetch any authorized workspace IDs using a KBase auth token, if present
auth_token = auth.get_auth_header()
json_body["ws_ids"] = auth.get_workspace_ids(auth_token)
resp_body = arango_client.run_query(
query_text=query_text,
bind_vars=json_body,
batch_size=batch_size,
full_count=full_count,
)
return flask.jsonify(resp_body)
if "stored_query" in flask.request.args or "view" in flask.request.args:
# Run a query from a query name
# Note: we are maintaining backwards compatibility here with the "view" arg.
# "stored_query" is the more accurate name
query_name = flask.request.args.get("stored_query") or flask.request.args.get(
"view"
)
stored_query = spec_loader.get_stored_query(query_name)
if "params" in stored_query:
# Validate the user params for the query
stored_query_path = spec_loader.get_stored_query(query_name, path_only=True)
run_validator(
schema_file=stored_query_path, data=json_body, validate_at="/params"
)
stored_query_source = _preprocess_stored_query(
stored_query["query"], stored_query
)
if "ws_ids" in stored_query_source:
# Fetch any authorized workspace IDs using a KBase auth token, if present
auth_token = auth.get_auth_header()
json_body["ws_ids"] = auth.get_workspace_ids(auth_token)
resp_body = arango_client.run_query(
query_text=stored_query_source,
bind_vars=json_body,
batch_size=batch_size,
full_count=full_count,
)
return flask.jsonify(resp_body)
if "cursor_id" in flask.request.args:
# Run a query from a cursor ID
cursor_id = flask.request.args["cursor_id"]
resp_body = arango_client.run_query(cursor_id=cursor_id)
return flask.jsonify(resp_body)
# No valid options were passed
raise InvalidParameters("Pass in a query name or a cursor_id")
@api_v1.route("/specs", methods=["PUT"])
def update_specs():
"""
Manually check for updates, download spec releases, and init new collections.
Auth: admin
"""
auth.require_auth_token(["RE_ADMIN"])
init_collections = "init_collections" in flask.request.args
release_url = flask.request.args.get("release_url")
update_name = pull_spec.download_specs(init_collections, release_url, reset=True)
return flask.jsonify(
{
"status": "updated",
"updated_from": update_name,
}
)
@api_v1.route("/documents", methods=["PUT"])
def save_documents():
"""
Create, update, or replace many documents in a batch.
Auth: admin
"""
auth.require_auth_token(["RE_ADMIN"])
collection_name = flask.request.args["collection"]
query = {"collection": collection_name, "type": "documents"}
if flask.request.args.get("display_errors"):
# Display an array of error messages
query["details"] = "true"
if flask.request.args.get("on_duplicate"):
query["onDuplicate"] = flask.request.args["on_duplicate"]
if flask.request.args.get("overwrite"):
query["overwrite"] = "true"
resp = bulk_import.bulk_import(query)
if resp.get("errors") > 0:
return (flask.jsonify(resp), 400)
else:
return flask.jsonify(resp)
@api_v1.route("/config", methods=["GET"])
def show_config():
"""Show public config data."""
conf = config.get_config()
return flask.jsonify(
{
"auth_url": conf["auth_url"],
"workspace_url": conf["workspace_url"],
"kbase_endpoint": conf["kbase_endpoint"],
"db_url": conf["db_url"],
"db_name": conf["db_name"],
"spec_repo_url": conf["spec_repo_url"],
"spec_release_url": conf["spec_release_url"],
"spec_release_path": conf["spec_release_path"],
}
)
@api_v1.route("/ensure_specs", methods=["GET"])
def ensure_all_specs():
"""
Ensure that the local index/view/analyzer specs under spec/ have a
corresponding spec on the server.
This endpoint is not strictly necessary, as the ensure_specs.ensure_all()
code should triggered in startup scripts. This is more insurance in case
one wishes to ensure the specs without re-deployment
Example ensure_specs.ensure_all() return value:
{
"indexes": [],
"views": ["Compounds/arangosearch", "Reactions/arangosearch"],
"analyzers": ["icu_tokenize/text"]
}
"""
failed_names = ensure_specs.ensure_all()
if any([name for schema_type, names in failed_names.items() for name in names]):
return flask.jsonify(failed_names), 500
else:
return flask.jsonify(failed_names)
def _preprocess_stored_query(query_text, config):
"""Inject some default code into each stored query."""
ws_id_text = " LET ws_ids = @ws_ids " if "ws_ids" in query_text else ""
return "\n".join([config.get("query_prefix", ""), ws_id_text, query_text])