The before code shows the client making separate calls to user and order services, increasing complexity and latency. The after code shows a single call to the API gateway, which internally routes and aggregates data, simplifying the client.
### Before: Client calls multiple services directly
import requests
def get_user_profile(user_id):
user = requests.get(f"http://user-service/users/{user_id}").json()
orders = requests.get(f"http://order-service/orders?user={user_id}").json()
return {"user": user, "orders": orders}
### After: Client calls API gateway once
import requests
def get_user_profile(user_id):
response = requests.get(f"http://api-gateway/profile/{user_id}")
return response.json()