Commit bed764d3e75e5d64f73d9e98c0c468ba87092ac7

Add few new API endpoints
swtr.py
(86 / 4)
  
1616from urllib import unquote_plus
1717import json
1818import conf
19
20# TODO:
21# restify
22# APIs as follows:
23# GET /sweets/q -> query sweets
24# args: who, where, what, how
25# GET /sweets/<id> -> get specific sweet
26# POST /sweets -> post sweets (one or a batch of)
27# OPTIONS /sweets - > CORS policy .. understand it better
28# classes!
29# sqlAlchemy
30# Postgres
31
32# TODO: move this in a config file
1933# configuration
2034DATABASE = 'alipiBlog'
2135COLLECTION_NAME = 'posts'
4040DB_PORT = 27017
4141DB_HOST = 'localhost'
4242URL = "http://localhost:5001"
43
4344# create our little application :)
45# ^ ... It's going to be big now :P
4446app = Flask(__name__)
4547app.config.from_object(__name__)
4648app.config.from_envvar('FLASKR_SETTINGS', silent=True)
9494
9595@app.route('/')
9696def show_entries():
97 print 'request:'
98 print request.method
9799 res = g.collection.find().sort('_id',direction=-1)
98100 entries = make_list(res)
99101 return render_template('show_entries.html', entries=entries)
100102
101103
102@app.route('/add', methods=['POST'])
103def add_entry():
104# TODO: understand if we really need the OPTIONS
105@app.route('/sweets', methods=['POST', 'OPTIONS'])
106@app.route('/add', methods=['POST', 'OPTIONS'])
107def addSweets():
108 print request.method
109
110 if request.method == 'OPTIONS':
111 response = make_response()
112 response.status_code = 200
113 response.headers['Access-Control-Allow-Origin'] = '*'
114 response.headers['Access-Control-Max-Age'] = '20days'
115 response.headers['Access-Control-Allow-Headers'] = 'Origin,\
116 X-Requested-With, Content-Type, Accept'
117 return response
118
104119 response = make_response()
105120 response.headers['Access-Control-Allow-Origin'] = '*'
121 response.headers['Access-Control-Allow-Headers'] = 'Origin,\
122 X-Requested-With, Content-Type, Accept'
106123 data = {}
107124 data_list = []
125 # TODO: find a better way of handling reqeust sweets
108126 try:
109127 payload = json.loads(request.form['data'])
110128 except:
111 payload = [{'who': request.form['who'], 'what': request.form['what'],
112 'where': request.form['where'], 'how': request.form['how']}]
129 try:
130 payload = [{'who': request.form['who'], 'what': request.form['what'],
131 'where': request.form['where'], 'how': request.form['how']}]
132 except:
133 try:
134 payload = request.json
135 except:
136 payload = json.loads(request.data)
137
138
113139 valid = validateSweet(payload)
114140 if not valid:
115141 response.status_code = 400
145145 print 'swt payload rcvd..'
146146 print payload
147147 for i in payload:
148 data = i
148149 id = g.collection.insert(i)
149150 data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id))
150151 data['id'] = str(ObjectId(id))
152 del(data['_id'])
153 print 'data', data
151154 data_list.append(data)
152155 response.data = json.dumps(data_list)
153156 print 'swt stored..'
172172 return render_template('login.html', error=error)
173173
174174
175@app.route('/sweets/q', methods=['GET'])
176def searchSweets():
177 response = make_response()
178 response.status_code = 200
179 response.headers['Access-Control-Allow-Origin'] = '*'
180 response.headers['Access-Control-Max-Age'] = '20days'
181 response.headers['Access-Control-Allow-Headers'] = 'Origin,\
182 X-Requested-With, Content-Type, Accept'
183
184 args = request.args
185
186 if args is None:
187 reponse.status_code = 400
188 return response
189
190 if args['where'] is None:
191 reponse.status_code = 400
192 return response
193
194 res = g.collection.find(args)
195
196 if res.count() < 1:
197 response.status_code = 404
198 return response
199
200 swt_list = []
201 for swt in res:
202 _id = swt['_id']
203 del(swt['_id'])
204 swt['id'] = str(_id)
205 swt_list.append(swt)
206
207 response.data = json.dumps(swt_list)
208 return response
209
210
211@app.route('/sweets/<post_id>', methods=['GET'])
175212@app.route('/query/<post_id>',methods=['GET'])
176213def return_database_entry(post_id):
177214 try: