-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·70 lines (61 loc) · 2.04 KB
/
Copy pathmain.py
File metadata and controls
executable file
·70 lines (61 loc) · 2.04 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!flask/bin/python
from flask import Flask, jsonify, abort, make_response, request
app = Flask(__name__)
posts = [
{
'id': 1,
'title': u'Getting Google AppEngine',
'description': u'You have to use this',
},
{
'id': 2,
'title': u'CSIS 604',
'description': u'This is the best class ever',
}
]
@app.route('/blog/api/posts', methods=['GET'])
def get_posts():
return jsonify({'posts': posts})
@app.route('/blog/api/posts/<int:post_id>', methods=['GET'])
def get_post(post_id):
post = [post for post in posts if post['id'] == post_id]
if len(post) == 0:
abort(404)
return jsonify({'post': post[0]})
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
@app.route('/blog/api/posts', methods=['POST'])
def create_post():
if not (request.json and 'title' in request.json):
abort(400)
post = {
'id': posts[-1]['id'] + 1,
'title': request.json['title'],
'description': request.json.get('description', "")
}
posts.append(post)
return jsonify({'post': post}), 201
@app.route('/blog/api/posts/<int:post_id>', methods=['PUT'])
def update_post(post_id):
post = [post for post in posts if post['id'] == post_id]
if len(post) == 0:
abort(404)
if not request.json:
abort(400)
if 'title' in request.json and type(request.json['title']) != unicode:
abort(400)
if 'description' in request.json and type(request.json['description']) is not unicode:
abort(400)
post[0]['title'] = request.json.get('title', post[0]['title'])
post[0]['description'] = request.json.get('description', post[0]['description'])
return jsonify({'post': post[0]})
@app.route('/blog/api/posts/<int:post_id>', methods=['DELETE'])
def delete_post(post_id):
post = [post for post in posts if post['id'] == post_id]
if len(post) == 0:
abort(404)
posts.remove(post[0])
return jsonify({'result': True})
if __name__ == '__main__':
app.run(debug=True)