public APIGatewayV2HTTPResponse handleRequest()

in software/products/src/main/java/software/amazonaws/example/product/entrypoints/ApiGatewayGetProductRequestHandler.java [38:73]


    public APIGatewayV2HTTPResponse handleRequest(APIGatewayV2HTTPEvent event, Context context) {
        String id = event.getPathParameters().get("id");
        if (id == null) {
            logger.warn("Missing 'id' parameter in path");
            return APIGatewayV2HTTPResponse.builder()
                    .withStatusCode(400)
                    .withHeaders(Map.of(CONTENT_TYPE, "application/json"))
                    .withBody("{ \"message\": \"Missing 'id' parameter in path\" }")
                    .build();
        }

        logger.info("Fetching product {}", id);

        Optional<Product> product = productStore.getProduct(id);
        if (product.isEmpty()) {
            logger.warn("No product with id: {}", id);
            return APIGatewayV2HTTPResponse.builder()
                    .withStatusCode(404)
                    .withBody("{\"message\": \"Product not found\"}")
                    .build();
        }

        logger.info(product.toString());

        try {
            return APIGatewayV2HTTPResponse.builder()
                    .withStatusCode(200)
                    .withHeaders(Map.of(CONTENT_TYPE, "application/json"))
                    .withBody(objectMapper.writeValueAsString(product.get()))
                    .build();
        } catch (JsonProcessingException e) {
            return APIGatewayV2HTTPResponse.builder()
                    .withStatusCode(500)
                    .build();
        }
    }