-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·251 lines (200 loc) · 7.51 KB
/
app.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import cherrypy
from cherrypy.lib.static import serve_file
import config
from lib import shared
import api_repository
from uuid import uuid1
import time
import json
import timeit
shared.start()
db=shared.DB
api_repository.set_db(db)
url_map = {}
app_db = {}
class Root(object):
@cherrypy.expose
def index(self):
return open(r"./views/app.html",'rb').read()
#user signup
@cherrypy.expose
@cherrypy.tools.json_in(on = True)
@cherrypy.tools.json_out(on = True)
def signup(self):
try:
data=cherrypy.request.json
res = api_repository.signup(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
return res
#check_uniqueness
@cherrypy.expose
@cherrypy.tools.json_in(on = True)
@cherrypy.tools.json_out(on = True)
def check_uniqueness(self):
try:
data=cherrypy.request.json
res = api_repository.check_uniqueness(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
return res
#user signin
@cherrypy.expose
@cherrypy.tools.json_in(on = True)
@cherrypy.tools.json_out(on = True)
def signin(self):
try:
data=cherrypy.request.json
res = api_repository.signin(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
return res
#user get_user_info
@cherrypy.expose
@cherrypy.tools.json_out(on = True)
def get_user_info(self,*args,**kwargs):
try:
data=cherrypy.request.params
res = api_repository.get_user_info(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
return res
#Create new consumer key
@cherrypy.expose
@cherrypy.tools.json_out(on = True)
def create_consumer_key(self,*args,**kwargs):
try:
data=cherrypy.request.params
res = api_repository.create_consumer_key(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
return res
@cherrypy.expose
@cherrypy.tools.json_out(on = True)
def reset_consumer_key(self,*args,**kwargs):
try:
data=cherrypy.request.params
res = api_repository.reset_consumer_key(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
return res
#user Manage api he wants to use add or delete
@cherrypy.expose
@cherrypy.tools.json_out(on = True)
def manage_api(self,**kwargs):
try:
method=cherrypy.request.method
return api_repository.manage_api(method,kwargs)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
#user request for credit
@cherrypy.expose
@cherrypy.tools.json_in(on = True)
@cherrypy.tools.json_out(on = True)
def credit_request(self):
try:
data=cherrypy.request.json
return api_repository.credit_request(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
#user get report
@cherrypy.expose
@cherrypy.tools.json_in(on = True)
@cherrypy.tools.json_out(on = True)
def get_usage_stat(self,**kwargs):
try:
data=cherrypy.request.json
return api_repository.get_usage_stat(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
#get_api_log
@cherrypy.expose
@cherrypy.tools.json_in(on = True)
def get_api_log(self,**kwargs):
try:
data=cherrypy.request.json
res= api_repository.get_api_log(data)
return serve_file(res['result'], "application/x-download", "attachment")
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
@cherrypy.expose
@cherrypy.tools.json_out(on = True)
def load_apis(self):
try:
return api_repository.load_apis('')
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
@cherrypy.expose
@cherrypy.tools.json_out(on = True)
def api_readme(self,**kwargs):
try:
return api_repository.api_readme(kwargs)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
@cherrypy.expose
def help(self, **kwargs):
try:
html = api_repository.api_readme(kwargs)['result']
return '''<html><head><link href="../static/css/lib/bootstrap.min.css" rel="stylesheet"></head><body><div class='container'>{}</div></body></html>'''.format(html)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
@cherrypy.expose
@cherrypy.tools.json_in(on = True)
@cherrypy.tools.json_out(on = True)
def load_credit(self):
try:
data=cherrypy.request.json
return api_repository.load_credit(data)
except Exception,e:
print("Very Bad Exception ::{}".format(e))
raise cherrypy.HTTPError(666, "General Exception")
@cherrypy.expose
@cherrypy.tools.json_in(on = True)
@cherrypy.tools.json_out(on = True)
def api(self,*args,**kwargs):
start = timeit.default_timer()
res=api_repository.call_api(cherrypy.request, url_map, app_db)
stop = timeit.default_timer()
res['run_time']=stop-start
data=cherrypy.request
request_transaction=dict(
ip=cherrypy.request.remote.ip,
api_name=res.get('api','NA'),
api_id=res.get('api_id','NA'),
user_id=kwargs.get('user_id'),
request_line=json.dumps(cherrypy.request.request_line),
time=int(time.time()),
credit=res.get('credit','NA'),
credit_before_api_call=res.get('credit_before_api_call','NA'),
rescode=res.get('rescode','NA')
)
db.update_direct('logs',where={'time':'1'},set_criteria=request_transaction)
res.pop('credit_before_api_call',None)
res.pop('api_id',None)
return res
def main():
global url_map, app_db
api_repository.register_api({}, once=False)
import re
apis = list(db.db.apis.find({}))
url_map = {re.compile(pt, re.IGNORECASE):v['_id'] for v in apis for pt in (v['url_pattern']+['/'+v['name']]) }
app_db = {v['_id']:v for v in apis}
app=Root()
cherrypy.config.update(config.cherrypy_conf_update)
cherrypy.quickstart(app, '/', config.cherrypy_conf)
if __name__ == '__main__':
main()
pass