Commit bed764d3e75e5d64f73d9e98c0c468ba87092ac7
Add few new API endpoints
- swtr.py 90 ----++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| | | | 16 | from urllib import unquote_plus | 16 | from urllib import unquote_plus |
---|
17 | import json | 17 | import json |
---|
18 | import conf | 18 | import 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 |
---|
19 | # configuration | 33 | # configuration |
---|
20 | DATABASE = 'alipiBlog' | 34 | DATABASE = 'alipiBlog' |
---|
21 | COLLECTION_NAME = 'posts' | 35 | COLLECTION_NAME = 'posts' |
---|
… | | … | |
---|
40 | DB_PORT = 27017 | 40 | DB_PORT = 27017 |
---|
41 | DB_HOST = 'localhost' | 41 | DB_HOST = 'localhost' |
---|
42 | URL = "http://localhost:5001" | 42 | URL = "http://localhost:5001" |
---|
| | 43 | |
---|
43 | # create our little application :) | 44 | # create our little application :) |
---|
| | 45 | # ^ ... It's going to be big now :P |
---|
44 | app = Flask(__name__) | 46 | app = Flask(__name__) |
---|
45 | app.config.from_object(__name__) | 47 | app.config.from_object(__name__) |
---|
46 | app.config.from_envvar('FLASKR_SETTINGS', silent=True) | 48 | app.config.from_envvar('FLASKR_SETTINGS', silent=True) |
---|
… | | … | |
---|
94 | | 94 | |
---|
95 | @app.route('/') | 95 | @app.route('/') |
---|
96 | def show_entries(): | 96 | def show_entries(): |
---|
| | 97 | print 'request:' |
---|
| | 98 | print request.method |
---|
97 | res = g.collection.find().sort('_id',direction=-1) | 99 | res = g.collection.find().sort('_id',direction=-1) |
---|
98 | entries = make_list(res) | 100 | entries = make_list(res) |
---|
99 | return render_template('show_entries.html', entries=entries) | 101 | return render_template('show_entries.html', entries=entries) |
---|
100 | | 102 | |
---|
101 | | 103 | |
---|
102 | @app.route('/add', methods=['POST']) | | @app.route('/add', methods=['POST']) |
---|
103 | def add_entry(): | | def 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']) |
---|
| | 107 | def 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 | |
---|
104 | response = make_response() | 119 | response = make_response() |
---|
105 | response.headers['Access-Control-Allow-Origin'] = '*' | 120 | response.headers['Access-Control-Allow-Origin'] = '*' |
---|
| | 121 | response.headers['Access-Control-Allow-Headers'] = 'Origin,\ |
---|
| | 122 | X-Requested-With, Content-Type, Accept' |
---|
106 | data = {} | 123 | data = {} |
---|
107 | data_list = [] | 124 | data_list = [] |
---|
| | 125 | # TODO: find a better way of handling reqeust sweets |
---|
108 | try: | 126 | try: |
---|
109 | payload = json.loads(request.form['data']) | 127 | payload = json.loads(request.form['data']) |
---|
110 | except: | 128 | except: |
---|
111 | payload = [{'who': request.form['who'], 'what': request.form['what'], | | payload = [{'who': request.form['who'], 'what': request.form['what'], |
---|
112 | 'where': request.form['where'], 'how': request.form['how']}] | | '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 | |
---|
113 | valid = validateSweet(payload) | 139 | valid = validateSweet(payload) |
---|
114 | if not valid: | 140 | if not valid: |
---|
115 | response.status_code = 400 | 141 | response.status_code = 400 |
---|
… | | … | |
---|
145 | print 'swt payload rcvd..' | 145 | print 'swt payload rcvd..' |
---|
146 | print payload | 146 | print payload |
---|
147 | for i in payload: | 147 | for i in payload: |
---|
| | 148 | data = i |
---|
148 | id = g.collection.insert(i) | 149 | id = g.collection.insert(i) |
---|
149 | data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id)) | 150 | data['permalink'] = app.config['URL'] + '/posts/' + str(ObjectId(id)) |
---|
150 | data['id'] = str(ObjectId(id)) | 151 | data['id'] = str(ObjectId(id)) |
---|
| | 152 | del(data['_id']) |
---|
| | 153 | print 'data', data |
---|
151 | data_list.append(data) | 154 | data_list.append(data) |
---|
152 | response.data = json.dumps(data_list) | 155 | response.data = json.dumps(data_list) |
---|
153 | print 'swt stored..' | 156 | print 'swt stored..' |
---|
… | | … | |
---|
172 | return render_template('login.html', error=error) | 172 | return render_template('login.html', error=error) |
---|
173 | | 173 | |
---|
174 | | 174 | |
---|
| | 175 | @app.route('/sweets/q', methods=['GET']) |
---|
| | 176 | def 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']) |
---|
175 | @app.route('/query/<post_id>',methods=['GET']) | 212 | @app.route('/query/<post_id>',methods=['GET']) |
---|
176 | def return_database_entry(post_id): | 213 | def return_database_entry(post_id): |
---|
177 | try: | 214 | try: |
---|