-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
executable file
·128 lines (80 loc) · 3.36 KB
/
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
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python
import os
from importlib import import_module
from ripozo import apimethod, ResourceBase
from opbeat.contrib.flask import Opbeat
from flask import Flask, json, jsonify, abort, request
from flask.ext.cache import Cache
from flask_ripozo import FlaskDispatcher
from utils import CustomJSONEncoder, JSONRipozoAdapter
app = Flask(__name__, instance_relative_config=True)
dispatcher = FlaskDispatcher(app, url_prefix='/v1')
dispatcher.register_adapters(JSONRipozoAdapter)
app.root_dir = os.path.dirname(os.path.abspath(__file__))
app.data_dir = os.path.join(app.root_dir, 'data')
app.json_encoder = CustomJSONEncoder
app.static_url_path = os.path.join(app.root_dir, 'templates')
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
opbeat = Opbeat(app, organization_id=os.environ.get('OPBEAT_ORG_ID'),
app_id=os.environ.get('OPBEAT_APP_ID'),
secret_token=os.environ.get('OPBEAT_SECRET'))
__version__ = '1.0.3'
def get_json_file(filename):
with open(os.path.join(app.data_dir, filename), 'r') as f:
return json.loads(f.read())
class IndexViewSet(ResourceBase):
resource_name = ''
@apimethod(methods=['GET'])
def index(cls, request, *args, **kwargs):
data = {}
data['me'] = get_json_file('me.json')
config = get_json_file('config.json')
data['routes'] = []
for route in config.get('modules').keys():
data['routes'] += ["{0}/{1}/".format(dispatcher.url_prefix, route)]
data['routes'].sort()
return cls(properties=data)
class ModuleViewSet(ResourceBase):
resource_name = ''
pks = ('module_name',)
@apimethod(methods=['GET'])
def view(cls, request, *args, **kwargs):
config = get_json_file('config.json')
module_name = request.get('module_name')
module_config = config.get('modules').get(module_name)
try:
middleware = import_module("middleware.module_" +
module_config.get('module'))
except ImportError:
abort(404)
data = middleware.main(app, module_config.get('data', {}))
return cls(properties=data)
class SubModuleViewSet(ResourceBase):
resource_name = ''
pks = ('module_name', 'sub_module_name')
@apimethod(methods=['GET'])
def view(cls, request, *args, **kwargs):
config = get_json_file('config.json')
module_name = request.get('module_name')
sub_module_name = request.get('sub_module_name')
module_config = config.get('modules').get(module_name)
module_children = module_config.get('children')
sub_module_confg = module_children.get(sub_module_name)
try:
middleware = import_module("middleware.module_" +
sub_module_confg.get('module'))
except ImportError:
abort(404)
data = middleware.main(app, sub_module_confg.get('data', {}))
return cls(properties=data)
@app.route('/robots.txt')
def send_text_file():
return app.send_static_file(request.path[1:])
@app.errorhandler(404)
def page_not_found(error):
res = jsonify(status=404, error=error.description)
res.headers['Access-Control-Allow-Origin'] = '*'
return res, 404
dispatcher.register_resources(IndexViewSet, ModuleViewSet, SubModuleViewSet)
if __name__ == "__main__":
app.run(debug=True)