Skip to content

Commit 16f944c

Browse files
committed
refactor(parser): Improve SQS models with examples and descriptions
Enhances the SQS parser models with field descriptions and examples using Pydantic's Field() functionality. This improvement provides better documentation and metadata for SQS event parsing, following the pattern established in PR #7100. All field descriptions are based on official AWS SQS documentation and include realistic examples from actual test events. Closes #7116
1 parent bb3c13e commit 16f944c

File tree

1 file changed

+137
-26
lines changed
  • aws_lambda_powertools/utilities/parser/models

1 file changed

+137
-26
lines changed
Lines changed: 137 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,78 @@
11
from datetime import datetime
22
from typing import Dict, List, Literal, Optional, Sequence, Type, Union
33

4-
from pydantic import BaseModel
4+
from pydantic import BaseModel, Field
55

66

77
class SqsAttributesModel(BaseModel):
8-
ApproximateReceiveCount: str
9-
ApproximateFirstReceiveTimestamp: datetime
10-
MessageDeduplicationId: Optional[str] = None
11-
MessageGroupId: Optional[str] = None
12-
SenderId: str
13-
SentTimestamp: datetime
14-
SequenceNumber: Optional[str] = None
15-
AWSTraceHeader: Optional[str] = None
16-
DeadLetterQueueSourceArn: Optional[str] = None
8+
ApproximateReceiveCount: str = Field(
9+
description="The number of times a message has been received across all queues but not deleted.",
10+
examples=["1", "2"],
11+
)
12+
ApproximateFirstReceiveTimestamp: datetime = Field(
13+
description="The time the message was first received from the queue (epoch time in milliseconds).",
14+
examples=["1545082649185", "1545082650649", "1713185156612"],
15+
)
16+
MessageDeduplicationId: Optional[str] = Field(
17+
default=None,
18+
description="Returns the value provided by the producer that calls the SendMessage action.",
19+
examples=["msg-dedup-12345", "unique-msg-abc123", None],
20+
)
21+
MessageGroupId: Optional[str] = Field(
22+
default=None,
23+
description="Returns the value provided by the producer that calls the SendMessage action.",
24+
examples=["order-processing", "user-123-updates", None],
25+
)
26+
SenderId: str = Field(
27+
description="The user ID for IAM users or the role ID for IAM roles that sent the message.",
28+
examples=["AIDAIENQZJOLO23YVJ4VO", "AMCXIENQZJOLO23YVJ4VO"],
29+
)
30+
SentTimestamp: datetime = Field(
31+
description="The time the message was sent to the queue (epoch time in milliseconds).",
32+
examples=["1545082649183", "1545082650636", "1713185156609"],
33+
)
34+
SequenceNumber: Optional[str] = Field(
35+
default=None,
36+
description="Returns the value provided by Amazon SQS.",
37+
examples=["18849496460467696128", "18849496460467696129", None],
38+
)
39+
AWSTraceHeader: Optional[str] = Field(
40+
default=None,
41+
description="The AWS X-Ray trace header for request tracing.",
42+
examples=["Root=1-5e1b4151-5ac6c58239c1e5b4", None],
43+
)
44+
DeadLetterQueueSourceArn: Optional[str] = Field(
45+
default=None,
46+
description="The ARN of the dead-letter queue from which the message was moved.",
47+
examples=["arn:aws:sqs:eu-central-1:123456789012:sqs-redrive-SampleQueue-RNvLCpwGmLi7", None],
48+
)
1749

1850

1951
class SqsMsgAttributeModel(BaseModel):
20-
stringValue: Optional[str] = None
21-
binaryValue: Optional[str] = None
22-
stringListValues: List[str] = []
23-
binaryListValues: List[str] = []
24-
dataType: str
52+
stringValue: Optional[str] = Field(
53+
default=None,
54+
description="The string value of the message attribute.",
55+
examples=["100", "active", "user-12345", None],
56+
)
57+
binaryValue: Optional[str] = Field(
58+
default=None,
59+
description="The binary value of the message attribute, base64-encoded.",
60+
examples=["base64Str", "SGVsbG8gV29ybGQ=", None],
61+
)
62+
stringListValues: List[str] = Field(
63+
default=[],
64+
description="A list of string values for the message attribute.",
65+
examples=[["item1", "item2"], ["tag1", "tag2", "tag3"], []],
66+
)
67+
binaryListValues: List[str] = Field(
68+
default=[],
69+
description="A list of binary values for the message attribute, each base64-encoded.",
70+
examples=[["dmFsdWUx", "dmFsdWUy"], ["aGVsbG8="], []],
71+
)
72+
dataType: str = Field(
73+
description="The data type of the message attribute (String, Number, Binary, or custom data type).",
74+
examples=["String", "Number", "Binary", "String.custom", "Number.float"],
75+
)
2576

2677
# context on why it's commented: https://github.com/aws-powertools/powertools-lambda-python/pull/118
2778
# Amazon SQS supports the logical data types String, Number, and Binary with optional custom data type
@@ -49,17 +100,77 @@ class SqsMsgAttributeModel(BaseModel):
49100

50101

51102
class SqsRecordModel(BaseModel):
52-
messageId: str
53-
receiptHandle: str
54-
body: Union[str, Type[BaseModel], BaseModel]
55-
attributes: SqsAttributesModel
56-
messageAttributes: Dict[str, SqsMsgAttributeModel]
57-
md5OfBody: str
58-
md5OfMessageAttributes: Optional[str] = None
59-
eventSource: Literal["aws:sqs"]
60-
eventSourceARN: str
61-
awsRegion: str
103+
messageId: str = Field(
104+
description="A unique identifier for the message. A MessageId is considered unique across all AWS accounts for an extended period of time.",
105+
examples=[
106+
"059f36b4-87a3-44ab-83d2-661975830a7d",
107+
"2e1424d4-f796-459a-8184-9c92662be6da",
108+
"db37cc61-1bb0-4e77-b6f3-7cf87f44a72a",
109+
],
110+
)
111+
receiptHandle: str = Field(
112+
description="An identifier associated with the act of receiving the message, used for message deletion.",
113+
examples=[
114+
"AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
115+
"AQEBzWwaftRI0KuVm4tP+/7q1rGgNqicHq...",
116+
"AQEBl1pqxv+ZHkarVAWZUyWgj2mmqJGLBTo6YFOi/bw1QpBTpJBGJPLOTZrjKztKIbAB8EXkG7zHlbkn+Ze/AHMKKuhST9azHu8LyF4Ffu9uPkZc5xzggXlfFBWH3TUKyV+F5Obaj3esyX8YfM/zfgjbRuu5nc2tfPhvaSYEaTZsdMpzIB5tyKvHxAltLxK7upRHeoT768M9UrFYswarFTBn8piDbnsPsUhi8Q9G4Q4xSI0fLQANmryBsRJIzGQTVxenDad+MJ7XEL+hD3p2DmW+ycvv6WD7bdedqQuroQG8+ca1Dz7s3CBbXw9ZZnUziPa7LH1j1Lky5bAxpNF+BlurRS9pFBnomhwpylrGxtGfaEmUW1G7jnrG97sZNOLOFUykbQgroZPXmjzMBdvtgq9ZmQfCch3LOXN267+PKc56VR4=",
117+
],
118+
)
119+
body: Union[str, Type[BaseModel], BaseModel] = Field(
120+
description="The message's contents (not URL-encoded). Can be plain text or JSON.",
121+
examples=[
122+
"Test message.",
123+
'{"message": "foo1"}',
124+
"hello world",
125+
],
126+
)
127+
attributes: SqsAttributesModel = Field(
128+
description="A map of the attributes requested in ReceiveMessage to their respective values.",
129+
)
130+
messageAttributes: Dict[str, SqsMsgAttributeModel] = Field(
131+
description="User-defined message attributes as key-value pairs.",
132+
examples=[
133+
{"testAttr": {"stringValue": "100", "binaryValue": "base64Str", "dataType": "Number"}},
134+
{},
135+
],
136+
)
137+
md5OfBody: str = Field(
138+
description="An MD5 digest of the non-URL-encoded message body string.",
139+
examples=[
140+
"e4e68fb7bd0e697a0ae8f1bb342846b3",
141+
"6a204bd89f3c8348afd5c77c717a097a",
142+
],
143+
)
144+
md5OfMessageAttributes: Optional[str] = Field(
145+
default=None,
146+
description="An MD5 digest of the non-URL-encoded message attribute string. You can use this attribute to verify that Amazon SQS received the message correctly.",
147+
examples=[
148+
"00484c68...59e48fb7",
149+
"b25f48e8...f4e4f0bb",
150+
None,
151+
],
152+
)
153+
eventSource: Literal["aws:sqs"] = Field(
154+
description="The AWS service that invoked the function.",
155+
examples=["aws:sqs"],
156+
)
157+
eventSourceARN: str = Field(
158+
description="The Amazon Resource Name (ARN) of the SQS queue.",
159+
examples=[
160+
"arn:aws:sqs:us-east-2:123456789012:my-queue",
161+
"arn:aws:sqs:eu-central-1:123456789012:sqs-redrive-SampleDLQ-Emgp9MFSLBZm",
162+
],
163+
)
164+
awsRegion: str = Field(
165+
description="The AWS region where the SQS queue is located.",
166+
examples=["us-east-1", "us-east-2", "eu-central-1"],
167+
)
62168

63169

64170
class SqsModel(BaseModel):
65-
Records: Sequence[SqsRecordModel]
171+
Records: Sequence[SqsRecordModel] = Field(
172+
description="A list of SQS message records included in the event.",
173+
examples=[
174+
[{"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d", "body": "Test message."}]
175+
],
176+
)

0 commit comments

Comments
 (0)