-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_web_api.py
44 lines (34 loc) · 1.18 KB
/
db_web_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
__author__ = 'shawn'
# flask interface to the couchdb
from db_store import DbStore
import json
from flask import Flask, request
from db_crossorigin import crossdomain
app = Flask(__name__)
couch = DbStore()
@app.route("/")
def root():
return "luc"
@app.route("/episode<key>", methods=['POST', 'GET', 'OPTIONS'])
@crossdomain(origin='http://localhost', headers='Content-Type')
def save_episode(key):
if request.method == 'POST':
data = json.loads(request.data)
doc_id = couch.save_episode(data)
# for CORS - if return type doesn't match the request, client will get an error
return str(doc_id)
elif request.method == 'GET':
query = request.args['key'] if 'key' in request.args else None
episode = couch.get_all_episodes(query)
return episode
@app.route("/grid", methods=['GET', 'OPTIONS'])
@crossdomain(origin='http://localhost', headers='Content-Type')
def display_grid():
data = couch.get_all_episodes()
transformed_data = []
for item in data[1]['rows']:
transformed_data.append(item['value'])
grid_data = json.dumps(transformed_data)
return grid_data
if __name__ == "__main__":
app.run(debug=False)