forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_gateway_proxy_event.py
236 lines (181 loc) · 7.57 KB
/
api_gateway_proxy_event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from typing import Any, Dict, List, Optional
from aws_lambda_powertools.utilities.data_classes.common import (
BaseProxyEvent,
BaseRequestContext,
BaseRequestContextV2,
DictWrapper,
)
class APIGatewayEventAuthorizer(DictWrapper):
@property
def claims(self) -> Optional[Dict[str, Any]]:
return self["requestContext"]["authorizer"].get("claims")
@property
def scopes(self) -> Optional[List[str]]:
return self["requestContext"]["authorizer"].get("scopes")
class APIGatewayEventRequestContext(BaseRequestContext):
@property
def connected_at(self) -> Optional[int]:
"""The Epoch-formatted connection time. (WebSocket API)"""
return self["requestContext"].get("connectedAt")
@property
def connection_id(self) -> Optional[str]:
"""A unique ID for the connection that can be used to make a callback to the client. (WebSocket API)"""
return self["requestContext"].get("connectionId")
@property
def event_type(self) -> Optional[str]:
"""The event type: `CONNECT`, `MESSAGE`, or `DISCONNECT`. (WebSocket API)"""
return self["requestContext"].get("eventType")
@property
def message_direction(self) -> Optional[str]:
"""Message direction (WebSocket API)"""
return self["requestContext"].get("messageDirection")
@property
def message_id(self) -> Optional[str]:
"""A unique server-side ID for a message. Available only when the `eventType` is `MESSAGE`."""
return self["requestContext"].get("messageId")
@property
def operation_name(self) -> Optional[str]:
"""The name of the operation being performed"""
return self["requestContext"].get("operationName")
@property
def route_key(self) -> Optional[str]:
"""The selected route key."""
return self["requestContext"].get("routeKey")
@property
def authorizer(self) -> APIGatewayEventAuthorizer:
return APIGatewayEventAuthorizer(self._data)
class APIGatewayProxyEvent(BaseProxyEvent):
"""AWS Lambda proxy V1
Documentation:
--------------
- https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
"""
@property
def version(self) -> str:
return self["version"]
@property
def resource(self) -> str:
return self["resource"]
@property
def multi_value_headers(self) -> Dict[str, List[str]]:
return self["multiValueHeaders"]
@property
def multi_value_query_string_parameters(self) -> Optional[Dict[str, List[str]]]:
return self.get("multiValueQueryStringParameters")
@property
def request_context(self) -> APIGatewayEventRequestContext:
return APIGatewayEventRequestContext(self._data)
@property
def path_parameters(self) -> Optional[Dict[str, str]]:
return self.get("pathParameters")
@property
def stage_variables(self) -> Optional[Dict[str, str]]:
return self.get("stageVariables")
class RequestContextV2AuthorizerIam(DictWrapper):
@property
def access_key(self) -> Optional[str]:
"""The IAM user access key associated with the request."""
return self.get("accessKey")
@property
def account_id(self) -> Optional[str]:
"""The AWS account ID associated with the request."""
return self.get("accountId")
@property
def caller_id(self) -> Optional[str]:
"""The principal identifier of the caller making the request."""
return self.get("callerId")
@property
def cognito_amr(self) -> Optional[List[str]]:
"""This represents how the user was authenticated.
AMR stands for Authentication Methods References as per the openid spec"""
return self["cognitoIdentity"].get("amr")
@property
def cognito_identity_id(self) -> Optional[str]:
"""The Amazon Cognito identity ID of the caller making the request.
Available only if the request was signed with Amazon Cognito credentials."""
return self["cognitoIdentity"].get("identityId")
@property
def cognito_identity_pool_id(self) -> Optional[str]:
"""The Amazon Cognito identity pool ID of the caller making the request.
Available only if the request was signed with Amazon Cognito credentials."""
return self["cognitoIdentity"].get("identityPoolId")
@property
def principal_org_id(self) -> Optional[str]:
"""The AWS organization ID."""
return self.get("principalOrgId")
@property
def user_arn(self) -> Optional[str]:
"""The Amazon Resource Name (ARN) of the effective user identified after authentication."""
return self.get("userArn")
@property
def user_id(self) -> Optional[str]:
"""The IAM user ID of the effective user identified after authentication."""
return self.get("userId")
class RequestContextV2Authorizer(DictWrapper):
@property
def jwt_claim(self) -> Dict[str, Any]:
return self["jwt"]["claims"]
@property
def jwt_scopes(self) -> List[str]:
return self["jwt"]["scopes"]
@property
def get_lambda(self) -> Optional[Dict[str, Any]]:
"""Lambda authorization context details"""
return self.get("lambda")
@property
def iam(self) -> Optional[RequestContextV2AuthorizerIam]:
"""IAM authorization details used for making the request."""
iam = self.get("iam")
return None if iam is None else RequestContextV2AuthorizerIam(iam)
class RequestContextV2(BaseRequestContextV2):
@property
def authorizer(self) -> Optional[RequestContextV2Authorizer]:
authorizer = self["requestContext"].get("authorizer")
return None if authorizer is None else RequestContextV2Authorizer(authorizer)
class APIGatewayProxyEventV2(BaseProxyEvent):
"""AWS Lambda proxy V2 event
Notes:
-----
Format 2.0 doesn't have multiValueHeaders or multiValueQueryStringParameters fields. Duplicate headers
are combined with commas and included in the headers field. Duplicate query strings are combined with
commas and included in the queryStringParameters field.
Format 2.0 includes a new cookies field. All cookie headers in the request are combined with commas and
added to the cookies field. In the response to the client, each cookie becomes a set-cookie header.
Documentation:
--------------
- https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html
"""
@property
def version(self) -> str:
return self["version"]
@property
def route_key(self) -> str:
return self["routeKey"]
@property
def raw_path(self) -> str:
return self["rawPath"]
@property
def raw_query_string(self) -> str:
return self["rawQueryString"]
@property
def cookies(self) -> Optional[List[str]]:
return self.get("cookies")
@property
def request_context(self) -> RequestContextV2:
return RequestContextV2(self._data)
@property
def path_parameters(self) -> Optional[Dict[str, str]]:
return self.get("pathParameters")
@property
def stage_variables(self) -> Optional[Dict[str, str]]:
return self.get("stageVariables")
@property
def path(self) -> str:
stage = self.request_context.stage
if stage != "$default":
return self.raw_path[len("/" + stage) :]
return self.raw_path
@property
def http_method(self) -> str:
"""The HTTP method used. Valid values include: DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT."""
return self.request_context.http.method