-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (38 loc) · 1.37 KB
/
main.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
import bottle
import time
from datetime import datetime
from bottle import Bottle, route, run, request, response
application = Bottle()
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
tt = int((te-ts)*1000)
ft = "timetaken - {0:4d} ms".format(tt)
if tt > 0:
print "timetaken - {}".format(ft)
return result
return timed
@application.route('/health', method=['OPTIONS', 'GET'])
def health():
"""health - to check health of the api"""
result = {"state":"healthy"}
return bottle.HTTPResponse(status=200, body=result)
@application.route('/test', method=['GET'])
@timeit
def test(**kwargs):
query_dict = request.query.__dict__
param_dict = query_dict['dict']
data = param_dict['id'][0]
wait = int(param_dict['wait'][0])
tag = param_dict['tag'][0]
print "got request for {}-{} at {}".format(tag, data, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
print "waiting for {}-{} for {} msec".format(tag, data, wait)
sec = wait/1000
time.sleep(sec)
print "send response for {}-{} at {}".format(tag, data, datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
result = bottle.HTTPResponse(status=200, body={"message":data})
return result
if __name__ == '__main__':
run(application, host='127.0.0.1', port=80)