Source code for eclypse.builders.application.sock_shop.rest_services.frontend

"""The `FrontendService` class.

It serves as the user interface for the SockShop application,
providing the user-facing components of the store.

- Key Responsibilities:
    - Displays product catalogs, shopping carts, and order information to users.
    - Interacts with backend services (e.g., `CatalogService`,
      `UserService`, `OrderService`) to display real-time data.
    - Manages user input and interactions such as product searches,
      cart updates, and order placements.
"""

from eclypse.remote.service import Service
from eclypse.utils import format_log_kv


[docs] class FrontendService(Service): """Example workflow of the Frontend service."""
[docs] def __init__(self, name, store_step: bool = False): """Initialise the Frontend service with the REST interface.""" super().__init__( name, communication_interface="rest", store_step=store_step, ) self.user_id = 12345
[docs] async def step(self): """Example workflow of the `Frontend` service. It starts with fetching the catalog, user data, and cart items, then placing an order. """ catalog_r = await self.rest.get("CatalogService/catalog") user_r = await self.rest.get("UserService/user", user_id=self.user_id) cart_r = await self.rest.get("CartService/cart") self.logger.info( "Received response | " + format_log_kv(source="CatalogService", body=catalog_r.body) ) self.logger.info( "Received response | " + format_log_kv(source="UserService", body=user_r.body) ) self.logger.info( "Received response | " + format_log_kv(source="CartService", body=cart_r.body) ) products = catalog_r.body.get("products", []) items = cart_r.body.get("items", []) order_items = [ { "id": item["id"], "amount": next( ( product["price"] * item["quantity"] for product in products if product["id"] == item["id"] ), None, ), } for item in items ] order_r = await self.rest.post("OrderService/order", items=order_items) self.logger.info( "Received response | " + format_log_kv(source="OrderService", body=order_r.body) )