1+ from __future__ import annotations
2+
13import enum
24import re
3- from typing import Any , Dict , List , Optional
5+ from typing import Any
46
57from aws_lambda_powertools .utilities .data_classes .common import (
68 BaseRequestContext ,
@@ -141,19 +143,19 @@ def http_method(self) -> str:
141143 return self ["httpMethod" ]
142144
143145 @property
144- def headers (self ) -> Dict [str , str ]:
146+ def headers (self ) -> dict [str , str ]:
145147 return CaseInsensitiveDict (self ["headers" ])
146148
147149 @property
148- def query_string_parameters (self ) -> Dict [str , str ]:
150+ def query_string_parameters (self ) -> dict [str , str ]:
149151 return self ["queryStringParameters" ]
150152
151153 @property
152- def path_parameters (self ) -> Dict [str , str ]:
154+ def path_parameters (self ) -> dict [str , str ]:
153155 return self ["pathParameters" ]
154156
155157 @property
156- def stage_variables (self ) -> Dict [str , str ]:
158+ def stage_variables (self ) -> dict [str , str ]:
157159 return self ["stageVariables" ]
158160
159161 @property
@@ -193,7 +195,7 @@ def parsed_arn(self) -> APIGatewayRouteArn:
193195 return parse_api_gateway_arn (self .route_arn )
194196
195197 @property
196- def identity_source (self ) -> List [str ]:
198+ def identity_source (self ) -> list [str ]:
197199 """The identity source for which authorization is requested.
198200
199201 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:
217219 return self ["rawQueryString" ]
218220
219221 @property
220- def cookies (self ) -> List [str ]:
222+ def cookies (self ) -> list [str ]:
221223 """Cookies"""
222224 return self ["cookies" ]
223225
224226 @property
225- def headers (self ) -> Dict [str , str ]:
227+ def headers (self ) -> dict [str , str ]:
226228 """Http headers"""
227229 return CaseInsensitiveDict (self ["headers" ])
228230
229231 @property
230- def query_string_parameters (self ) -> Dict [str , str ]:
232+ def query_string_parameters (self ) -> dict [str , str ]:
231233 return self ["queryStringParameters" ]
232234
233235 @property
234236 def request_context (self ) -> BaseRequestContextV2 :
235237 return BaseRequestContextV2 (self ._data )
236238
237239 @property
238- def path_parameters (self ) -> Dict [str , str ]:
240+ def path_parameters (self ) -> dict [str , str ]:
239241 return self .get ("pathParameters" ) or {}
240242
241243 @property
242- def stage_variables (self ) -> Dict [str , str ]:
244+ def stage_variables (self ) -> dict [str , str ]:
243245 return self .get ("stageVariables" ) or {}
244246
245247
@@ -253,7 +255,7 @@ class APIGatewayAuthorizerResponseV2:
253255 is authorized to make calls to the GraphQL API. If this value is
254256 true, execution of the GraphQL API continues. If this value is false,
255257 an UnauthorizedException is raised
256- context: Dict [str, Any], optional
258+ context: dict [str, Any], optional
257259 A JSON object visible as `event.requestContext.authorizer` lambda event
258260
259261 The context object only supports key-value pairs. Nested keys are not supported.
@@ -264,14 +266,14 @@ class APIGatewayAuthorizerResponseV2:
264266 def __init__ (
265267 self ,
266268 authorize : bool = False ,
267- context : Optional [ Dict [ str , Any ]] = None ,
269+ context : dict [ str , Any ] | None = None ,
268270 ):
269271 self .authorize = authorize
270272 self .context = context
271273
272274 def asdict (self ) -> dict :
273275 """Return the response as a dict"""
274- response : Dict = {"isAuthorized" : self .authorize }
276+ response : dict = {"isAuthorized" : self .authorize }
275277
276278 if self .context :
277279 response ["context" ] = self .context
@@ -329,8 +331,8 @@ def __init__(
329331 aws_account_id : str ,
330332 api_id : str ,
331333 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 ,
334336 partition : str = "aws" ,
335337 ):
336338 """
@@ -357,7 +359,7 @@ def __init__(
357359 greedily expand over '/' or other separators.
358360 See https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_resource.html for more
359361 details.
360- context : Dict , optional
362+ context : dict , optional
361363 Optional, context.
362364 Note: only names of type string and values of type int, string or boolean are supported
363365 usage_identifier_key: str, optional
@@ -375,18 +377,18 @@ def __init__(
375377 self .stage = stage
376378 self .context = context
377379 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 ] = []
380382 self ._resource_pattern = re .compile (self .path_regex )
381383 self .partition = partition
382384
383385 @staticmethod
384386 def from_route_arn (
385387 arn : str ,
386388 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 :
390392 parsed_arn = parse_api_gateway_arn (arn )
391393 return APIGatewayAuthorizerResponse (
392394 principal_id ,
@@ -398,7 +400,7 @@ def from_route_arn(
398400 usage_identifier_key ,
399401 )
400402
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 ):
402404 """Adds a route to the internal lists of allowed or denied routes. Each object in
403405 the internal list contains a resource ARN and a condition statement. The condition
404406 statement can be null."""
@@ -427,17 +429,17 @@ def _add_route(self, effect: str, http_method: str, resource: str, conditions: O
427429 self ._deny_routes .append (route )
428430
429431 @staticmethod
430- def _get_empty_statement (effect : str ) -> Dict [str , Any ]:
432+ def _get_empty_statement (effect : str ) -> dict [str , Any ]:
431433 """Returns an empty statement object prepopulated with the correct action and the desired effect."""
432434 return {"Action" : "execute-api:Invoke" , "Effect" : effect .capitalize (), "Resource" : []}
433435
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 ]:
435437 """This function loops over an array of objects containing a `resourceArn` and
436438 `conditions` statement and generates the array of statements for the policy."""
437439 if not routes :
438440 return []
439441
440- statements : List [ Dict ] = []
442+ statements : list [ dict ] = []
441443 statement = self ._get_empty_statement (effect )
442444
443445 for route in routes :
@@ -476,31 +478,31 @@ def deny_all_routes(self, http_method: str = HttpVerb.ALL.value):
476478
477479 self ._add_route (effect = "Deny" , http_method = http_method , resource = "*" )
478480
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 ):
480482 """Adds an API Gateway method (Http verb + Resource path) to the list of allowed
481483 methods for the policy.
482484
483485 Optionally includes a condition for the policy statement. More on AWS policy
484486 conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
485487 self ._add_route (effect = "Allow" , http_method = http_method , resource = resource , conditions = conditions )
486488
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 ):
488490 """Adds an API Gateway method (Http verb + Resource path) to the list of denied
489491 methods for the policy.
490492
491493 Optionally includes a condition for the policy statement. More on AWS policy
492494 conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
493495 self ._add_route (effect = "Deny" , http_method = http_method , resource = resource , conditions = conditions )
494496
495- def asdict (self ) -> Dict [str , Any ]:
497+ def asdict (self ) -> dict [str , Any ]:
496498 """Generates the policy document based on the internal lists of allowed and denied
497499 conditions. This will generate a policy with two main statements for the effect:
498500 one statement for Allow and one statement for Deny.
499501 Methods that includes conditions will have their own statement in the policy."""
500502 if len (self ._allow_routes ) == 0 and len (self ._deny_routes ) == 0 :
501503 raise ValueError ("No statements defined for the policy" )
502504
503- response : Dict [str , Any ] = {
505+ response : dict [str , Any ] = {
504506 "principalId" : self .principal_id ,
505507 "policyDocument" : {"Version" : "2012-10-17" , "Statement" : []},
506508 }
0 commit comments