Skip to content

docs(batch): use newly supported Json model #2100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions docs/utilities/batch.md
Original file line number Diff line number Diff line change
Expand Up @@ -693,27 +693,22 @@ Inheritance is importance because we need to access message IDs and sequence num

=== "SQS"

```python hl_lines="5 13 22 28"
```python hl_lines="5 14 23 29"
import json

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
from aws_lambda_powertools.utilities.parser.models import SqsRecordModel
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.utilities.parser import validator, BaseModel
from aws_lambda_powertools.utilities.parser import BaseModel
from aws_lambda_powertools.utilities.parser.types import Json


class Order(BaseModel):
item: dict

class OrderSqsRecord(SqsRecordModel):
body: Order

# auto transform json string
# so Pydantic can auto-initialize nested Order model
@validator("body", pre=True)
def transform_body_to_dict(cls, value: str):
return json.loads(value)
body: Json[Order] # deserialize order data from JSON string

processor = BatchProcessor(event_type=EventType.SQS, model=OrderSqsRecord)
tracer = Tracer()
Expand All @@ -732,13 +727,14 @@ Inheritance is importance because we need to access message IDs and sequence num

=== "Kinesis Data Streams"

```python hl_lines="5 14 25 29 35"
```python hl_lines="5 15 19 23 29 36"
import json

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
from aws_lambda_powertools.utilities.parser.models import KinesisDataStreamRecordPayload, KinesisDataStreamRecord
from aws_lambda_powertools.utilities.parser import BaseModel, validator
from aws_lambda_powertools.utilities.parser.types import Json
from aws_lambda_powertools.utilities.typing import LambdaContext


Expand All @@ -747,14 +743,7 @@ Inheritance is importance because we need to access message IDs and sequence num


class OrderKinesisPayloadRecord(KinesisDataStreamRecordPayload):
data: Order

# auto transform json string
# so Pydantic can auto-initialize nested Order model
@validator("data", pre=True)
def transform_message_to_dict(cls, value: str):
# Powertools KinesisDataStreamRecord already decodes b64 to str here
return json.loads(value)
data: Json[Order]


class OrderKinesisRecord(KinesisDataStreamRecord):
Expand Down