-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig_api.py
116 lines (87 loc) · 2.6 KB
/
config_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
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
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
@app.route('/config/<app_name>', methods=['GET', 'POST', 'DELETE'])
def hello_world(app_name):
msg = {}
try:
f = open(app.config['config_name'], 'r')
config = json.load(f)
apps = config['apps']
f.close()
except IOError:
msg = {
'status': 2,
'content': 'File <{}> not found'.format(app.config['config_name'])
}
return jsonify(msg)
except Exception as e:
msg = {
'status': 2,
'content': str(e)
}
return jsonify(msg)
if request.method == 'GET':
flag = 0
for spark_app in apps:
if spark_app['app_name'] == app_name:
msg = {
'status': 0,
'content': spark_app
}
flag = 1
if flag == 0:
msg = {
'status': 2,
'content': '{0} not in {1}'.format(app_name, app.config['config_name'])
}
return jsonify(msg)
# Supporting insert and update
elif request.method == 'POST':
try:
body = json.loads(request.get_data())
except Exception as e:
msg = {
'status': 2,
'content': str(e)
}
return jsonify(msg)
flag = 0
for spark_app in apps:
if spark_app['app_name'] == app_name:
info = spark_app
for key in body.keys():
info[key] = body[key]
flag = 1
if flag == 0:
body['app_name'] = app_name
apps.append(body)
f = open(app.config['config_name'], 'w')
f.write(json.dumps(config, indent=4))
f.close()
msg = {
"status": 0
}
return jsonify(msg)
elif request.method == 'DELETE':
for i in range(len(apps)):
if apps[i]['app_name'] == app_name:
del apps[i]
msg = {
'status': 0
}
f = open(app.config['config_name'], 'w')
f.write(json.dumps(config, indent=4))
f.close()
return jsonify(msg)
msg = {
'status': '2',
'content': '{0} not in {1}'.format(app_name, app.config['config_name'])
}
return jsonify(msg)
else:
msg = {
'status': 1,
'content': 'Unsupported Method'
}
return jsonify(msg)