blob: 8616438e6b256291fad0354cb383d1eeb9f83006 [file] [log] [blame]
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -07001import black_cherry_bootstrap
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -07002from urllib2 import URLError, urlopen
3from urllib import urlencode
4from json import loads, dumps
5from bottle import route, run, template, static_file, request
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -07006
7# Core Routing
8@route('/')
9def jsontest():
10 return template('cherry')
11
12@route('/static/<filename:path>')
13def send_static(filename):
14 return static_file(filename, root='static')
15
16# API Helpers
17def build_response(endpoint, data):
18 api_endpoint = "http://localhost:19002/" + endpoint
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070019
genia.likes.science@gmail.com1b30f3d2013-08-17 23:53:37 -070020 try:
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070021 # Encode data into url string
22 urlresponse = urlopen(api_endpoint + '?' + urlencode(data))
23
24 # There are some weird bits passed in from the Asterix JSON.
25 # We will remove them here before we pass the result string
26 # back to the frontend.
27 urlresult = ""
28 CHUNK = 16 * 1024
29 while True:
30 chunk = urlresponse.read(CHUNK)
31 if not chunk: break
32 urlresult += chunk
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -070033
34 # QUERY ISSUE 2: UNCOMMENT THIS TO SEE WEIRD BUG
Eugenia Gabrielovacacc9f82013-10-21 14:10:21 -070035 urlresult = ','.join(urlresult.split(']}{"results":['))
36
37 # Create JSON dump of resulting response
38 return loads(urlresult)
39
40 except ValueError, e:
41 pass
42
43 except URLError, e:
44
45 # Here we report possible errors in request fulfillment.
46 if hasattr(e, 'reason'):
47 print 'Failed to reach a server.'
48 print 'Reason: ', e.reason
49
50 elif hasattr(e, 'code'):
51 print 'The server couldn\'t fulfill the request.'
52 print 'Error code: ', e.code
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070053
54# API Endpoints
55@route('/query')
56def run_asterix_query():
57 return (build_response("query", dict(request.query)))
58
59@route('/query/status')
60def run_asterix_query_status():
61 return (build_response("query/status", dict(request.query)))
62
63@route('/query/result')
64def run_asterix_query_result():
65 return (build_response("query/result", dict(request.query)))
66
genia.likes.science@gmail.comd42b4022013-08-09 05:05:23 -070067@route('/ddl')
68def run_asterix_ddl():
69 return (build_response("ddl", dict(request.query)))
70
71@route('/update')
72def run_asterix_update():
73 return (build_response("update", dict(request.query)))
74
75res = black_cherry_bootstrap.bootstrap()
Eugenia Gabrielovae11965b2013-10-29 19:07:42 -070076run(host='localhost', port=8080, debug=True)