-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflask_api.py
More file actions
41 lines (34 loc) · 1.27 KB
/
flask_api.py
File metadata and controls
41 lines (34 loc) · 1.27 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
from flask import Flask, request, jsonify
app = Flask(__name__)
users = {}
# Create a user
@app.route('/users', methods=['POST'])
def create_user():
data = request.get_json()
if 'id' not in data or 'name' not in data:
return jsonify({'error': 'ID and name are required'}), 400
users[data['id']] = data['name']
return jsonify({'message': 'User created successfully'}), 201
# Read a user
@app.route('/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
if user_id not in users:
return jsonify({'error': 'User not found'}), 404
return jsonify({'id': user_id, 'name': users[user_id]}), 200
# Update a user
@app.route('/users/<int:user_id>', methods=['PUT'])
def update_user(user_id):
if user_id not in users:
return jsonify({'error': 'User not found'}), 404
data = request.get_json()
users[user_id] = data.get('name', users[user_id])
return jsonify({'message': 'User updated successfully'}), 200
# Delete a user
@app.route('/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
if user_id not in users:
return jsonify({'error': 'User not found'}), 404
del users[user_id]
return jsonify({'message': 'User deleted successfully'}), 200
if __name__ == '__main__':
app.run(debug=True)