-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
68 lines (51 loc) · 1.9 KB
/
app.py
File metadata and controls
68 lines (51 loc) · 1.9 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
# post_service.py
from flask import Flask, jsonify, request
import requests
app = Flask(__name__)
posts = {
'1': {'user_id': '1', 'post': 'Hello, world!'},
'2': {'user_id': '2', 'post': 'My first blog post'}
}
@app.route('/')
def hello():
return "i am live now"
@app.route('/post/<id>')
def post(id):
post_info = posts.get(id, {})
if post_info:
response = requests.get(f'https://ankituserservice.azurewebsites.net/user/{post_info["user_id"]}')
if response.status_code == 200:
post_info['user'] = response.json()
return jsonify(post_info)
@app.route('/post', methods=['POST'])
def create_post():
new_post = request.get_json()
required_keys = ['user_id', 'post']
if all(key in new_post for key in required_keys):
posts[str(len(posts.keys()) + 1)] = new_post
print(posts)
return jsonify({"success":True})
else:
return jsonify({"success":False, "msg": "Please pass all the data"})
@app.route('/post/<id>', methods=['PUT'])
def update_post(id):
if id in posts:
updated_post = request.get_json()
required_keys = ['user_id', 'post']
if all(key in updated_post for key in required_keys):
posts[id] = updated_post
print(posts)
return jsonify({"success": True, "msg": "post updated successfully"})
else:
return jsonify({"success": False, "msg": "Please pass all the required data for update"}), 400
else:
return jsonify({"success": False, "msg": "post not found"}), 404
@app.route('/post/<id>', methods=['DELETE'])
def delete_post(id):
if id in posts:
del posts[id]
return jsonify({"success": True, "msg": "post deleted successfully"})
else:
return jsonify({"success": False, "msg": "post not found"}), 404
if __name__ == '__main__':
app.run('0.0.0.0',port=5001)