-
Notifications
You must be signed in to change notification settings - Fork 1
/
serve_api.py
67 lines (47 loc) · 1.71 KB
/
serve_api.py
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
# Python 3.5+ required
import os
import sys
import subprocess
from flask import Flask, request, make_response, jsonify
from path import Path
app = Flask(__name__)
if 'ZEPS_CORE_JAR' not in os.environ:
print('Please specify ZePS-Core JAR with the ZEPS_CORE_JAR environment variable.', file=sys.stderr)
sys.exit(1)
zeps_core = Path(os.environ.get('ZEPS_CORE_JAR')).expand()
java_exec = Path(os.environ.get('JAVA_EXEC', 'java')).expand()
if not zeps_core.exists():
print('ZePS Core JAR not found. Check the ZEPS_CORE_JAR environment variable.', file=sys.stderr)
sys.exit(1)
def call_core(command, *args):
process = subprocess.run(
[java_exec, '-jar', zeps_core, command, *[str(arg) for arg in args]],
env=dict(os.environ, LANG="fr_FR.UTF-8"),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
try:
process.check_returncode()
return make_response(process.stdout, 200, {'Content-Type': 'application/json; charset=utf-8'})
except Exception:
return jsonify(error=str(process.stderr), exit_code=process.returncode)
@app.route('/list')
def list():
return call_core('list')
@app.route('/list/network')
def list_with_network():
return call_core('list', 'true')
@app.route('/path/<int:from_id>/<int:to_id>')
def path(from_id, to_id):
official = 'official' in request.args
accessible = 'accessible' in request.args
return call_core('pathfinder', from_id, to_id, official, accessible)
@app.route('/colors')
def colors():
return call_core('colors')
@app.route('/version')
def version():
return jsonify(
version=zeps_core.stem.replace('-jar-with-dependencies', ''),
sha256=str(zeps_core.read_hexhash('sha256'))
)