forked from Alexey/DAS_2023_1
44 lines
1015 B
Python
44 lines
1015 B
Python
from flask import Flask, jsonify, request
|
|
|
|
app = Flask(__name__)
|
|
|
|
# "clients"
|
|
clients_data = []
|
|
|
|
|
|
@app.route('/', methods=['GET'])
|
|
def get_index():
|
|
return "Hello from service_b"
|
|
|
|
|
|
@app.route('/clients', methods=['GET'])
|
|
def get_clients():
|
|
return jsonify(clients_data)
|
|
|
|
|
|
@app.route('/clients', methods=['POST'])
|
|
def create_client():
|
|
new_client = request.json
|
|
clients_data.append(new_client)
|
|
return jsonify(new_client), 201
|
|
|
|
|
|
@app.route('/clients/<int:id>', methods=['PUT'])
|
|
def update_client(id):
|
|
for client in clients_data:
|
|
if client['id'] == id:
|
|
client.update(request.json)
|
|
return jsonify(client), 200
|
|
return jsonify({'error': 'Client not found'}), 404
|
|
|
|
|
|
@app.route('/clients/<int:id>', methods=['DELETE'])
|
|
def delete_client(id):
|
|
global clients_data
|
|
clients_data = [client for client in clients_data if client['id'] != id]
|
|
return jsonify({'message': 'Client deleted'}), 200
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=5001)
|