Skip to content

Commit 2654d73

Browse files
author
Michael Brewer
committed
chore: minor optimizations
1 parent f267736 commit 2654d73

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,6 @@ class APIGatewayAuthorizerResponse:
351351
- https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
352352
"""
353353

354-
version = "2012-10-17"
355-
"""The policy version used for the evaluation. This should always be '2012-10-17'"""
356-
357354
path_regex = r"^[/.a-zA-Z0-9-\*]+$"
358355
"""The regular expression used to validate resource paths for the policy"""
359356

@@ -408,6 +405,7 @@ def __init__(
408405
self.usage_identifier_key = usage_identifier_key
409406
self._allow_routes: List[Dict] = []
410407
self._deny_routes: List[Dict] = []
408+
self.resource_pattern = re.compile(self.path_regex)
411409

412410
def _add_route(self, effect: str, http_method: str, resource: str, conditions: Optional[List[Dict]] = None):
413411
"""Adds a route to the internal lists of allowed or denied routes. Each object in
@@ -417,8 +415,7 @@ def _add_route(self, effect: str, http_method: str, resource: str, conditions: O
417415
allowed_values = [verb.value for verb in HttpVerb]
418416
raise ValueError(f"Invalid HTTP verb: '{http_method}'. Use either '{allowed_values}'")
419417

420-
resource_pattern = re.compile(self.path_regex)
421-
if not resource_pattern.match(resource):
418+
if not self.resource_pattern.match(resource):
422419
raise ValueError(f"Invalid resource path: {resource}. Path should match {self.path_regex}")
423420

424421
if resource[:1] == "/":
@@ -443,7 +440,7 @@ def _get_empty_statement(effect: str) -> Dict[str, Any]:
443440
def _get_statement_for_effect(self, effect: str, routes: List[Dict]) -> List[Dict]:
444441
"""This function loops over an array of objects containing a `resourceArn` and
445442
`conditions` statement and generates the array of statements for the policy."""
446-
if len(routes) == 0:
443+
if not routes:
447444
return []
448445

449446
statements: List[Dict] = []
@@ -511,7 +508,7 @@ def asdict(self) -> Dict[str, Any]:
511508

512509
response: Dict[str, Any] = {
513510
"principalId": self.principal_id,
514-
"policyDocument": {"Version": self.version, "Statement": []},
511+
"policyDocument": {"Version": "2012-10-17", "Statement": []},
515512
}
516513

517514
response["policyDocument"]["Statement"].extend(self._get_statement_for_effect("Allow", self._allow_routes))

0 commit comments

Comments
 (0)