Skip to content

Commit 4c24ee7

Browse files
authored
refactor(parser): Improve SQS models with examples and descriptions (#7286)
1 parent 0cf950b commit 4c24ee7

File tree

1 file changed

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

1 file changed

+134
-26
lines changed
Lines changed: 134 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,74 @@ 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.",
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+
],
117+
)
118+
body: Union[str, Type[BaseModel], BaseModel] = Field(
119+
description="The message's contents (not URL-encoded). Can be plain text or JSON.",
120+
examples=[
121+
"Test message.",
122+
'{"message": "foo1"}',
123+
"hello world",
124+
],
125+
)
126+
attributes: SqsAttributesModel = Field(
127+
description="A map of the attributes requested in ReceiveMessage to their respective values.",
128+
)
129+
messageAttributes: Dict[str, SqsMsgAttributeModel] = Field(
130+
description="User-defined message attributes as key-value pairs.",
131+
examples=[
132+
{"testAttr": {"stringValue": "100", "binaryValue": "base64Str", "dataType": "Number"}},
133+
{},
134+
],
135+
)
136+
md5OfBody: str = Field(
137+
description="An MD5 digest of the non-URL-encoded message body string.",
138+
examples=[
139+
"e4e68fb7bd0e697a0ae8f1bb342846b3",
140+
"6a204bd89f3c8348afd5c77c717a097a",
141+
],
142+
)
143+
md5OfMessageAttributes: Optional[str] = Field(
144+
default=None,
145+
description="An MD5 digest of the non-URL-encoded message attribute string.",
146+
examples=[
147+
"00484c68...59e48fb7",
148+
"b25f48e8...f4e4f0bb",
149+
None,
150+
],
151+
)
152+
eventSource: Literal["aws:sqs"] = Field(
153+
description="The AWS service that invoked the function.",
154+
examples=["aws:sqs"],
155+
)
156+
eventSourceARN: str = Field(
157+
description="The Amazon Resource Name (ARN) of the SQS queue.",
158+
examples=[
159+
"arn:aws:sqs:us-east-2:123456789012:my-queue",
160+
"arn:aws:sqs:eu-central-1:123456789012:sqs-redrive-SampleDLQ-Emgp9MFSLBZm",
161+
],
162+
)
163+
awsRegion: str = Field(
164+
description="The AWS region where the SQS queue is located.",
165+
examples=["us-east-1", "us-east-2", "eu-central-1"],
166+
)
62167

63168

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

0 commit comments

Comments
 (0)