Skip to content

Commit 6619ff8

Browse files
author
Tom McCarthy
committed
fix: add sqs_batch_processor as its own method
1 parent 9575b4c commit 6619ff8

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

aws_lambda_powertools/utilities/batch/middlewares.py

+49-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"""
44
Middlewares for batch utilities
55
"""
6-
import functools
7-
from typing import Callable, Dict
6+
from typing import Callable, Dict, Optional
7+
8+
from botocore.config import Config
89

910
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
1011

@@ -57,4 +58,49 @@ def batch_processor(
5758
return handler(event, context)
5859

5960

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

Comments
 (0)