-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.py
56 lines (46 loc) · 1.76 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
# app.py
__author__ = "Naren_Aryan"
__written_date__ = "13-09-2015:Saturday"
__title__ = "Falcon and PyPy for building massively scalable RESTFul API"
import falcon
import json
from db_client import *
class NoteResource:
def on_get(self, req, resp):
"""Handles GET requests"""
# Return note for particular ID
if req.get_param("id"):
result = {'note': r.db(PROJECT_DB).table(PROJECT_TABLE).get(req.get_param("id")).run(db_connection)}
else:
note_cursor = r.db(PROJECT_DB).table(PROJECT_TABLE).run(db_connection)
result = {'notes': [i for i in note_cursor]}
print result
resp.body = json.dumps(result)
def on_post(self, req, resp):
"""Handles POST requests"""
try:
raw_json = req.stream.read()
print raw_json
except Exception as ex:
raise falcon.HTTPError(falcon.HTTP_400,
'Error',
ex.message)
try:
result = json.loads(raw_json, encoding='utf-8')
sid = r.db(PROJECT_DB).table(PROJECT_TABLE).insert({'title':result['title'],'body':result['body']}).run(db_connection)
resp.body = 'Successfully inserted %s'%sid
except ValueError:
raise falcon.HTTPError(falcon.HTTP_400,
'Invalid JSON',
'Could not decode the request body. The '
'JSON was incorrect.')
"""
def on_delete(self, req, resp):
"""Handles GET requests"""
# Return all notes
note_cursor = r.db(PROJECT_DB).table(PROJECT_TABLE).run(db_connection)
notes = [i for i in note_cursor]
resp.body = json.dumps({'notes':notes})
"""
api = falcon.API()
api.add_route('/notes', NoteResource())