1
+ from __future__ import annotations
2
+
1
3
import enum
2
4
import re
3
- from typing import Any , Dict , List , Optional
5
+ from typing import Any
4
6
5
7
from aws_lambda_powertools .utilities .data_classes .common import (
6
8
BaseRequestContext ,
@@ -141,19 +143,19 @@ def http_method(self) -> str:
141
143
return self ["httpMethod" ]
142
144
143
145
@property
144
- def headers (self ) -> Dict [str , str ]:
146
+ def headers (self ) -> dict [str , str ]:
145
147
return CaseInsensitiveDict (self ["headers" ])
146
148
147
149
@property
148
- def query_string_parameters (self ) -> Dict [str , str ]:
150
+ def query_string_parameters (self ) -> dict [str , str ]:
149
151
return self ["queryStringParameters" ]
150
152
151
153
@property
152
- def path_parameters (self ) -> Dict [str , str ]:
154
+ def path_parameters (self ) -> dict [str , str ]:
153
155
return self ["pathParameters" ]
154
156
155
157
@property
156
- def stage_variables (self ) -> Dict [str , str ]:
158
+ def stage_variables (self ) -> dict [str , str ]:
157
159
return self ["stageVariables" ]
158
160
159
161
@property
@@ -193,7 +195,7 @@ def parsed_arn(self) -> APIGatewayRouteArn:
193
195
return parse_api_gateway_arn (self .route_arn )
194
196
195
197
@property
196
- def identity_source (self ) -> List [str ]:
198
+ def identity_source (self ) -> list [str ]:
197
199
"""The identity source for which authorization is requested.
198
200
199
201
For a REQUEST authorizer, this is optional. The value is a set of one or more mapping expressions of the
@@ -217,29 +219,29 @@ def raw_query_string(self) -> str:
217
219
return self ["rawQueryString" ]
218
220
219
221
@property
220
- def cookies (self ) -> List [str ]:
222
+ def cookies (self ) -> list [str ]:
221
223
"""Cookies"""
222
224
return self ["cookies" ]
223
225
224
226
@property
225
- def headers (self ) -> Dict [str , str ]:
227
+ def headers (self ) -> dict [str , str ]:
226
228
"""Http headers"""
227
229
return CaseInsensitiveDict (self ["headers" ])
228
230
229
231
@property
230
- def query_string_parameters (self ) -> Dict [str , str ]:
232
+ def query_string_parameters (self ) -> dict [str , str ]:
231
233
return self ["queryStringParameters" ]
232
234
233
235
@property
234
236
def request_context (self ) -> BaseRequestContextV2 :
235
237
return BaseRequestContextV2 (self ._data )
236
238
237
239
@property
238
- def path_parameters (self ) -> Dict [str , str ]:
240
+ def path_parameters (self ) -> dict [str , str ]:
239
241
return self .get ("pathParameters" ) or {}
240
242
241
243
@property
242
- def stage_variables (self ) -> Dict [str , str ]:
244
+ def stage_variables (self ) -> dict [str , str ]:
243
245
return self .get ("stageVariables" ) or {}
244
246
245
247
@@ -253,7 +255,7 @@ class APIGatewayAuthorizerResponseV2:
253
255
is authorized to make calls to the GraphQL API. If this value is
254
256
true, execution of the GraphQL API continues. If this value is false,
255
257
an UnauthorizedException is raised
256
- context: Dict [str, Any], optional
258
+ context: dict [str, Any], optional
257
259
A JSON object visible as `event.requestContext.authorizer` lambda event
258
260
259
261
The context object only supports key-value pairs. Nested keys are not supported.
@@ -264,14 +266,14 @@ class APIGatewayAuthorizerResponseV2:
264
266
def __init__ (
265
267
self ,
266
268
authorize : bool = False ,
267
- context : Optional [ Dict [ str , Any ]] = None ,
269
+ context : dict [ str , Any ] | None = None ,
268
270
):
269
271
self .authorize = authorize
270
272
self .context = context
271
273
272
274
def asdict (self ) -> dict :
273
275
"""Return the response as a dict"""
274
- response : Dict = {"isAuthorized" : self .authorize }
276
+ response : dict = {"isAuthorized" : self .authorize }
275
277
276
278
if self .context :
277
279
response ["context" ] = self .context
@@ -329,8 +331,8 @@ def __init__(
329
331
aws_account_id : str ,
330
332
api_id : str ,
331
333
stage : str ,
332
- context : Optional [ Dict ] = None ,
333
- usage_identifier_key : Optional [ str ] = None ,
334
+ context : dict | None = None ,
335
+ usage_identifier_key : str | None = None ,
334
336
partition : str = "aws" ,
335
337
):
336
338
"""
@@ -357,7 +359,7 @@ def __init__(
357
359
greedily expand over '/' or other separators.
358
360
See https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_resource.html for more
359
361
details.
360
- context : Dict , optional
362
+ context : dict , optional
361
363
Optional, context.
362
364
Note: only names of type string and values of type int, string or boolean are supported
363
365
usage_identifier_key: str, optional
@@ -375,18 +377,18 @@ def __init__(
375
377
self .stage = stage
376
378
self .context = context
377
379
self .usage_identifier_key = usage_identifier_key
378
- self ._allow_routes : List [ Dict ] = []
379
- self ._deny_routes : List [ Dict ] = []
380
+ self ._allow_routes : list [ dict ] = []
381
+ self ._deny_routes : list [ dict ] = []
380
382
self ._resource_pattern = re .compile (self .path_regex )
381
383
self .partition = partition
382
384
383
385
@staticmethod
384
386
def from_route_arn (
385
387
arn : str ,
386
388
principal_id : str ,
387
- context : Optional [ Dict ] = None ,
388
- usage_identifier_key : Optional [ str ] = None ,
389
- ) -> " APIGatewayAuthorizerResponse" :
389
+ context : dict | None = None ,
390
+ usage_identifier_key : str | None = None ,
391
+ ) -> APIGatewayAuthorizerResponse :
390
392
parsed_arn = parse_api_gateway_arn (arn )
391
393
return APIGatewayAuthorizerResponse (
392
394
principal_id ,
@@ -398,7 +400,7 @@ def from_route_arn(
398
400
usage_identifier_key ,
399
401
)
400
402
401
- def _add_route (self , effect : str , http_method : str , resource : str , conditions : Optional [ List [ Dict ]] = None ):
403
+ def _add_route (self , effect : str , http_method : str , resource : str , conditions : list [ dict ] | None = None ):
402
404
"""Adds a route to the internal lists of allowed or denied routes. Each object in
403
405
the internal list contains a resource ARN and a condition statement. The condition
404
406
statement can be null."""
@@ -427,17 +429,17 @@ def _add_route(self, effect: str, http_method: str, resource: str, conditions: O
427
429
self ._deny_routes .append (route )
428
430
429
431
@staticmethod
430
- def _get_empty_statement (effect : str ) -> Dict [str , Any ]:
432
+ def _get_empty_statement (effect : str ) -> dict [str , Any ]:
431
433
"""Returns an empty statement object prepopulated with the correct action and the desired effect."""
432
434
return {"Action" : "execute-api:Invoke" , "Effect" : effect .capitalize (), "Resource" : []}
433
435
434
- def _get_statement_for_effect (self , effect : str , routes : List [ Dict ]) -> List [ Dict ]:
436
+ def _get_statement_for_effect (self , effect : str , routes : list [ dict ]) -> list [ dict ]:
435
437
"""This function loops over an array of objects containing a `resourceArn` and
436
438
`conditions` statement and generates the array of statements for the policy."""
437
439
if not routes :
438
440
return []
439
441
440
- statements : List [ Dict ] = []
442
+ statements : list [ dict ] = []
441
443
statement = self ._get_empty_statement (effect )
442
444
443
445
for route in routes :
@@ -476,31 +478,31 @@ def deny_all_routes(self, http_method: str = HttpVerb.ALL.value):
476
478
477
479
self ._add_route (effect = "Deny" , http_method = http_method , resource = "*" )
478
480
479
- def allow_route (self , http_method : str , resource : str , conditions : Optional [ List [ Dict ]] = None ):
481
+ def allow_route (self , http_method : str , resource : str , conditions : list [ dict ] | None = None ):
480
482
"""Adds an API Gateway method (Http verb + Resource path) to the list of allowed
481
483
methods for the policy.
482
484
483
485
Optionally includes a condition for the policy statement. More on AWS policy
484
486
conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
485
487
self ._add_route (effect = "Allow" , http_method = http_method , resource = resource , conditions = conditions )
486
488
487
- def deny_route (self , http_method : str , resource : str , conditions : Optional [ List [ Dict ]] = None ):
489
+ def deny_route (self , http_method : str , resource : str , conditions : list [ dict ] | None = None ):
488
490
"""Adds an API Gateway method (Http verb + Resource path) to the list of denied
489
491
methods for the policy.
490
492
491
493
Optionally includes a condition for the policy statement. More on AWS policy
492
494
conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
493
495
self ._add_route (effect = "Deny" , http_method = http_method , resource = resource , conditions = conditions )
494
496
495
- def asdict (self ) -> Dict [str , Any ]:
497
+ def asdict (self ) -> dict [str , Any ]:
496
498
"""Generates the policy document based on the internal lists of allowed and denied
497
499
conditions. This will generate a policy with two main statements for the effect:
498
500
one statement for Allow and one statement for Deny.
499
501
Methods that includes conditions will have their own statement in the policy."""
500
502
if len (self ._allow_routes ) == 0 and len (self ._deny_routes ) == 0 :
501
503
raise ValueError ("No statements defined for the policy" )
502
504
503
- response : Dict [str , Any ] = {
505
+ response : dict [str , Any ] = {
504
506
"principalId" : self .principal_id ,
505
507
"policyDocument" : {"Version" : "2012-10-17" , "Statement" : []},
506
508
}
0 commit comments