Skip to content

Commit 249b595

Browse files
author
Tom McCarthy
committed
chore: remove middlewares module, moving decorator functionality to base and sqs
1 parent 24acda9 commit 249b595

File tree

5 files changed

+109
-120
lines changed

5 files changed

+109
-120
lines changed

aws_lambda_powertools/utilities/batch/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
Batch processing utility
55
"""
66

7-
from .base import BasePartialProcessor
8-
from .middlewares import batch_processor, sqs_batch_processor
9-
from .sqs import PartialSQSProcessor
7+
from .base import BasePartialProcessor, batch_processor
8+
from .sqs import PartialSQSProcessor, sqs_batch_processor
109

1110
__all__ = ("BasePartialProcessor", "PartialSQSProcessor", "batch_processor", "sqs_batch_processor")

aws_lambda_powertools/utilities/batch/base.py

+48-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"""
66

77
from abc import ABC, abstractmethod
8-
from typing import Any, Callable, Iterable, List, Tuple
8+
from typing import Any, Callable, Dict, Iterable, List, Tuple
9+
10+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
911

1012

1113
class BasePartialProcessor(ABC):
@@ -93,3 +95,48 @@ def failure_handler(self, record: Any, exception: Exception):
9395
self.exceptions.append(exception)
9496
self.fail_messages.append(record)
9597
return entry
98+
99+
100+
@lambda_handler_decorator
101+
def batch_processor(
102+
handler: Callable, event: Dict, context: Dict, record_handler: Callable, processor: BasePartialProcessor = None
103+
):
104+
"""
105+
Middleware to handle batch event processing
106+
107+
Parameters
108+
----------
109+
handler: Callable
110+
Lambda's handler
111+
event: Dict
112+
Lambda's Event
113+
context: Dict
114+
Lambda's Context
115+
record_handler: Callable
116+
Callable to process each record from the batch
117+
processor: PartialSQSProcessor
118+
Batch Processor to handle partial failure cases
119+
120+
Examples
121+
--------
122+
**Processes Lambda's event with PartialSQSProcessor**
123+
>>> from aws_lambda_powertools.utilities.batch import batch_processor
124+
>>>
125+
>>> def record_handler(record):
126+
>>> return record["body"]
127+
>>>
128+
>>> @batch_processor(record_handler=record_handler, processor=PartialSQSProcessor())
129+
>>> def handler(event, context):
130+
>>> return {"StatusCode": 200}
131+
132+
Limitations
133+
-----------
134+
* Async batch processors
135+
136+
"""
137+
records = event["Records"]
138+
139+
with processor(records, record_handler):
140+
processor.process()
141+
142+
return handler(event, context)

aws_lambda_powertools/utilities/batch/middlewares.py

-113
This file was deleted.

aws_lambda_powertools/utilities/batch/sqs.py

+57-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
"""
44
Batch SQS utilities
55
"""
6-
from typing import List, Optional, Tuple
6+
from typing import Callable, Dict, List, Optional, Tuple
77

88
import boto3
99
from botocore.config import Config
1010

11+
from ...middleware_factory import lambda_handler_decorator
1112
from .base import BasePartialProcessor
1213
from .exceptions import SQSBatchProcessingError
1314

@@ -116,3 +117,58 @@ def _clean(self):
116117
raise SQSBatchProcessingError(list(self.exceptions))
117118

118119
return delete_message_response
120+
121+
122+
@lambda_handler_decorator
123+
def sqs_batch_processor(
124+
handler: Callable,
125+
event: Dict,
126+
context: Dict,
127+
record_handler: Callable,
128+
config: Optional[Config] = None,
129+
suppress_exception: bool = False,
130+
):
131+
"""
132+
Middleware to handle SQS batch event processing
133+
134+
Parameters
135+
----------
136+
handler: Callable
137+
Lambda's handler
138+
event: Dict
139+
Lambda's Event
140+
context: Dict
141+
Lambda's Context
142+
record_handler: Callable
143+
Callable to process each record from the batch
144+
config: Config
145+
botocore config object
146+
suppress_exception: bool, optional
147+
Supress exception raised if any messages fail processing, by default False
148+
149+
Examples
150+
--------
151+
**Processes Lambda's event with PartialSQSProcessor**
152+
>>> from aws_lambda_powertools.utilities.batch import sqs_batch_processor
153+
>>>
154+
>>> def record_handler(record):
155+
>>> return record["body"]
156+
>>>
157+
>>> @sqs_batch_processor(record_handler=record_handler)
158+
>>> def handler(event, context):
159+
>>> return {"StatusCode": 200}
160+
161+
Limitations
162+
-----------
163+
* Async batch processors
164+
165+
"""
166+
config = config or Config()
167+
processor = PartialSQSProcessor(config=config, suppress_exception=suppress_exception)
168+
169+
records = event["Records"]
170+
171+
with processor(records, record_handler):
172+
processor.process()
173+
174+
return handler(event, context)

tests/functional/test_utilities_batch.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def lambda_handler(event, context):
148148
stubber.assert_no_pending_responses()
149149

150150

151-
@patch("aws_lambda_powertools.utilities.batch.middlewares.PartialSQSProcessor")
151+
@patch("aws_lambda_powertools.utilities.batch.sqs.PartialSQSProcessor")
152152
def test_sqs_batch_processor_middleware(
153153
patched_sqs_processor, sqs_event_factory, record_handler, stubbed_partial_processor
154154
):
@@ -251,7 +251,7 @@ def test_partial_sqs_processor_suppressed_exceptions(sqs_event_factory, record_h
251251
assert partial_processor_suppressed.success_messages == [first_record]
252252

253253

254-
@patch("aws_lambda_powertools.utilities.batch.middlewares.PartialSQSProcessor")
254+
@patch("aws_lambda_powertools.utilities.batch.sqs.PartialSQSProcessor")
255255
def test_sqs_batch_processor_middleware_suppressed_exception(
256256
patched_sqs_processor, sqs_event_factory, record_handler, stubbed_partial_processor_suppressed
257257
):

0 commit comments

Comments
 (0)