|
3 | 3 | """
|
4 | 4 | Middlewares for batch utilities
|
5 | 5 | """
|
6 |
| -import functools |
7 |
| -from typing import Callable, Dict |
| 6 | +from typing import Callable, Dict, Optional |
| 7 | + |
| 8 | +from botocore.config import Config |
8 | 9 |
|
9 | 10 | from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
|
10 | 11 |
|
@@ -57,4 +58,49 @@ def batch_processor(
|
57 | 58 | return handler(event, context)
|
58 | 59 |
|
59 | 60 |
|
60 |
| -sqs_batch_processor = functools.partial(batch_processor, processor=PartialSQSProcessor()) |
| 61 | +@lambda_handler_decorator |
| 62 | +def sqs_batch_processor( |
| 63 | + handler: Callable, event: Dict, context: Dict, record_handler: Callable, config: Optional[Config] = None |
| 64 | +): |
| 65 | + """ |
| 66 | + Middleware to handle batch event processing |
| 67 | +
|
| 68 | + Parameters |
| 69 | + ---------- |
| 70 | + handler: Callable |
| 71 | + Lambda's handler |
| 72 | + event: Dict |
| 73 | + Lambda's Event |
| 74 | + context: Dict |
| 75 | + Lambda's Context |
| 76 | + record_handler: Callable |
| 77 | + Callable to process each record from the batch |
| 78 | + config: Config |
| 79 | + botocore config object |
| 80 | +
|
| 81 | + Examples |
| 82 | + -------- |
| 83 | + **Processes Lambda's event with PartialSQSProcessor** |
| 84 | + >>> from aws_lambda_powertools.utilities.batch import sqs_batch_processor |
| 85 | + >>> |
| 86 | + >>> def record_handler(record): |
| 87 | + >>> return record["body"] |
| 88 | + >>> |
| 89 | + >>> @sqs_batch_processor(record_handler=record_handler) |
| 90 | + >>> def handler(event, context): |
| 91 | + >>> return {"StatusCode": 200} |
| 92 | +
|
| 93 | + Limitations |
| 94 | + ----------- |
| 95 | + * Async batch processors |
| 96 | +
|
| 97 | + """ |
| 98 | + config = config or Config() |
| 99 | + processor = PartialSQSProcessor(config=config) |
| 100 | + |
| 101 | + records = event["Records"] |
| 102 | + |
| 103 | + with processor(records, record_handler): |
| 104 | + processor.process() |
| 105 | + |
| 106 | + return handler(event, context) |
0 commit comments