@@ -59,7 +59,77 @@ These are simply Python classes that inherit from BaseModel. **Parser** enforces
59
59
60
60
### Extending built-in models
61
61
62
- ** TBW**
62
+ Parser comes with the following built-in models:
63
+
64
+ Model name | Description
65
+ ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------
66
+ ** DynamoDBStreamModel** | Lambda Event Source payload for Amazon DynamoDB Streams
67
+ ** EventBridgeModel** | Lambda Event Source payload for Amazon EventBridge
68
+ ** SqsModel** | Lambda Event Source payload for Amazon SQS
69
+
70
+ You can extend them to include your own models, and yet have all other known fields parsed along the way.
71
+
72
+ ** EventBridge example**
73
+
74
+ ``` python:title=extending_builtin_models.py
75
+ from aws_lambda_powertools.utilities.parser import parse, BaseModel
76
+ from aws_lambda_powertools.utilities.parser.models import EventBridgeModel
77
+
78
+ from typing import List, Optional
79
+
80
+ class OrderItem (BaseModel ):
81
+ id : int
82
+ quantity: int
83
+ description: str
84
+
85
+ class Order (BaseModel ):
86
+ id : int
87
+ description: str
88
+ items: List[OrderItem]
89
+
90
+ # highlight-start
91
+ class OrderEventModel (EventBridgeModel ):
92
+ detail: Order
93
+ # highlight-end
94
+
95
+ payload = {
96
+ " version" : " 0" ,
97
+ " id" : " 6a7e8feb-b491-4cf7-a9f1-bf3703467718" ,
98
+ " detail-type" : " OrderPurchased" ,
99
+ " source" : " OrderService" ,
100
+ " account" : " 111122223333" ,
101
+ " time" : " 2020-10-22T18:43:48Z" ,
102
+ " region" : " us-west-1" ,
103
+ " resources" : [" some_additional" ],
104
+ " detail" : { # highlight-line
105
+ " id" : 10876546789 ,
106
+ " description" : " My order" ,
107
+ " items" : [
108
+ {
109
+ " id" : 1015938732 ,
110
+ " quantity" : 1 ,
111
+ " description" : " item xpto"
112
+ }
113
+ ]
114
+ }
115
+ }
116
+
117
+ ret = parse(model = OrderEventModel, event = payload) # highlight-line
118
+
119
+ assert ret.source == " OrderService"
120
+ assert ret.detail.description == " My order"
121
+ assert ret.detail_type == " OrderPurchased" # we rename it to snake_case
122
+
123
+ for order_item in ret.detail.items:
124
+ ...
125
+ ```
126
+
127
+ ** What's going on here, you might ask** :
128
+
129
+ 1 . We imported our built-in model ` EventBridgeModel ` from the parser utility
130
+ 2 . Defined how our ` Order ` should look like
131
+ 3 . Defined how part of our EventBridge event should look like by overriding ` detail ` key within our ` OrderEventModel `
132
+ 4 . Parser parsed the original event against ` OrderEventModel `
63
133
64
134
## Parsing events
65
135
@@ -232,7 +302,7 @@ parse(model=UserModel, event=payload)
232
302
233
303
## Envelopes
234
304
235
- Envelope parameter is useful when your actual payload is wrapped around a known structure, for example Lambda Event Sources like Amazon EventBridge.
305
+ Envelope parameter is useful when your actual payload is wrapped around a known structure, for example Lambda Event Sources like EventBridge.
236
306
237
307
Example of parsing a model found in an event coming from EventBridge, where all you want is what's inside the ` detail ` key.
238
308
@@ -262,15 +332,15 @@ payload = {
262
332
# highlight-end
263
333
}
264
334
265
- ret = parse(model = UserModel, envelope = envelopes.EventBridgeModel, payload = payload) # highlight-line
335
+ ret = parse(model = UserModel, envelope = envelopes.EventBridgeModel, event = payload) # highlight-line
266
336
267
337
# Parsed model only contains our actual model, not the entire EventBridge + Payload parsed
268
338
assert ret.password1 == ret.password2
269
339
```
270
340
271
341
** What's going on here, you might ask** :
272
342
273
- 1 . We imported built-in ` models ` from the parser utility
343
+ 1 . We imported built-in ` envelopes ` from the parser utility
274
344
2 . Used ` envelopes.EventBridgeModel ` as the envelope for our ` UserModel ` model
275
345
3 . Parser parsed the original event against the EventBridge model
276
346
4 Parser then parsed the ` detail ` key using ` UserModel `
0 commit comments