|
| 1 | +from aws_lambda_powertools.event_handler import APIGatewayRestResolver |
| 2 | +from aws_lambda_powertools.utilities.feature_flags import AppConfigStore, FeatureFlags |
| 3 | +from aws_lambda_powertools.utilities.typing import LambdaContext |
| 4 | + |
| 5 | +app = APIGatewayRestResolver() |
| 6 | + |
| 7 | +app_config = AppConfigStore(environment="dev", application="product-catalogue", name="features") |
| 8 | + |
| 9 | +feature_flags = FeatureFlags(store=app_config) |
| 10 | + |
| 11 | + |
| 12 | +@app.get("/products") |
| 13 | +def list_products(): |
| 14 | + # getting fields from request |
| 15 | + # https://awslabs.github.io/aws-lambda-powertools-python/latest/core/event_handler/api_gateway/#accessing-request-details |
| 16 | + json_body = app.current_event.json_body |
| 17 | + headers = app.current_event.headers |
| 18 | + |
| 19 | + ctx = {**headers, **json_body} |
| 20 | + |
| 21 | + # getting price from payload |
| 22 | + price: float = float(json_body.get("price")) |
| 23 | + percent_discount: int = 0 |
| 24 | + |
| 25 | + # all_features is evaluated to ["premium_features", "geo_customer_campaign", "ten_percent_off_campaign"] |
| 26 | + all_features: list[str] = feature_flags.get_enabled_features(context=ctx) |
| 27 | + |
| 28 | + if "geo_customer_campaign" in all_features: |
| 29 | + # apply 20% discounts for customers in NL |
| 30 | + percent_discount += 20 |
| 31 | + |
| 32 | + if "ten_percent_off_campaign" in all_features: |
| 33 | + # apply additional 10% for all customers |
| 34 | + percent_discount += 10 |
| 35 | + |
| 36 | + price = price * (100 - percent_discount) / 100 |
| 37 | + |
| 38 | + return {"price": price} |
| 39 | + |
| 40 | + |
| 41 | +def lambda_handler(event: dict, context: LambdaContext): |
| 42 | + return app.resolve(event, context) |
0 commit comments