from flask import Flask, jsonify, request import requests app = Flask(__name__) SHELVES = [] @app.route('/shelves', methods=['GET']) def get_shelves(): book_service_url = "http://book_service:5000/books" response = requests.get(book_service_url) books = response.json() shelves = {} for book in books: if book["shelf"] not in shelves: shelves[book["shelf"]] = [] shelves[book["shelf"]].append(book) return jsonify(shelves) @app.route('/shelves', methods=['POST']) def create_shelf(): new_shelf = request.get_json() SHELVES.append(new_shelf) return jsonify(new_shelf), 201 @app.route('/shelves/', methods=['PUT']) def update_shelf(name): shelf = next((shelf for shelf in SHELVES if shelf['name'] == name), None) if shelf: shelf.update(request.get_json()) return jsonify(shelf) return jsonify({"message": "Shelf not found"}), 404 @app.route('/shelves/', methods=['DELETE']) def delete_shelf(name): global SHELVES SHELVES = [shelf for shelf in SHELVES if shelf['name'] != name] return '', 204 if __name__ == '__main__': app.run(host='0.0.0.0', port=5001)