ict-architecture

Labo 08: Microservices Patronen

Voorbeelden van API Gateway en API Composition in een Node.js/Python ecosysteem.

Omgeving

docker compose up -d
Start de basis services (Customers, Orders, Products).

Bestanden & Configuraties

gateway.py
python
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
@app.route('/order-summary', methods=['GET'])
def get_order_summary():
email = request.args.get('email')
customer = requests.get(f"http://customers:3000/customers?email={email}").json()[0]
orders = requests.get(f"http://orders:3000/orders?customerId={customer['id']}").json()
full_orders = []
for order in orders:
product = requests.get(f"http://products:3000/products/{order['productId']}").json()
full_orders.append({
"productName": product['name'],
"priceAtPurchase": product['price']
})
return jsonify({
"customerName": customer['name'],
"orderHistory": full_orders
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Een Python API Gateway die data uit meerdere services combineert.