Skip to content

Commit f5a613a

Browse files
committed
refactor: tech debt overload on get_header_value for middleware example work properly
1 parent d8cf386 commit f5a613a

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,11 @@ def _has_compression_enabled(
388388
bool
389389
True if compression is enabled and the "gzip" encoding is accepted, False otherwise.
390390
"""
391-
encoding: str = event.get_header_value(name="accept-encoding", default_value="", case_sensitive=False) # type: ignore[assignment] # noqa: E501
391+
encoding: str = event.get_header_value(
392+
name="accept-encoding",
393+
default_value="",
394+
case_sensitive=False,
395+
) # noqa: E501
392396
if "gzip" in encoding:
393397
if response_compression is not None:
394398
return response_compression # e.g., Response(compress=False/True))

aws_lambda_powertools/utilities/data_classes/common.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import base64
22
import json
33
from collections.abc import Mapping
4-
from typing import Any, Callable, Dict, Iterator, List, Optional
4+
from typing import Any, Callable, Dict, Iterator, List, Optional, overload
55

66
from aws_lambda_powertools.shared.headers_serializer import BaseHeadersSerializer
77
from aws_lambda_powertools.utilities.data_classes.shared_functions import (
@@ -156,7 +156,24 @@ def get_query_string_value(self, name: str, default_value: Optional[str] = None)
156156
default_value=default_value,
157157
)
158158

159-
# Maintenance: missing @overload to ensure return type is a str when default_value is set
159+
@overload
160+
def get_header_value(
161+
self,
162+
name: str,
163+
default_value: str,
164+
case_sensitive: Optional[bool] = False,
165+
) -> str:
166+
...
167+
168+
@overload
169+
def get_header_value(
170+
self,
171+
name: str,
172+
default_value: Optional[str] = None,
173+
case_sensitive: Optional[bool] = False,
174+
) -> Optional[str]:
175+
...
176+
160177
def get_header_value(
161178
self,
162179
name: str,

aws_lambda_powertools/utilities/data_classes/vpc_lattice.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional
1+
from typing import Any, Dict, Optional, overload
22

33
from aws_lambda_powertools.shared.headers_serializer import (
44
BaseHeadersSerializer,
@@ -91,6 +91,24 @@ def get_query_string_value(self, name: str, default_value: Optional[str] = None)
9191
default_value=default_value,
9292
)
9393

94+
@overload
95+
def get_header_value(
96+
self,
97+
name: str,
98+
default_value: str,
99+
case_sensitive: Optional[bool] = False,
100+
) -> str:
101+
...
102+
103+
@overload
104+
def get_header_value(
105+
self,
106+
name: str,
107+
default_value: Optional[str] = None,
108+
case_sensitive: Optional[bool] = False,
109+
) -> Optional[str]:
110+
...
111+
94112
def get_header_value(
95113
self,
96114
name: str,

examples/event_handler_rest/src/split_route_module.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ def get_todos():
2525
@router.get("/todos/<todo_id>")
2626
@tracer.capture_method
2727
def get_todo_by_id(todo_id: str): # value come as str
28-
api_key: str = router.current_event.get_header_value(name="X-Api-Key", case_sensitive=True, default_value="") # type: ignore[assignment] # sentinel typing # noqa: E501
28+
api_key: str = router.current_event.get_header_value(
29+
name="X-Api-Key",
30+
case_sensitive=True,
31+
default_value="",
32+
) # noqa: E501
2933

3034
todos: Response = requests.get(f"{endpoint}/{todo_id}", headers={"X-Api-Key": api_key})
3135
todos.raise_for_status()

examples/event_handler_rest/src/split_route_prefix_module.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ def get_todos():
2525
@router.get("/<todo_id>")
2626
@tracer.capture_method
2727
def get_todo_by_id(todo_id: str): # value come as str
28-
api_key: str = router.current_event.get_header_value(name="X-Api-Key", case_sensitive=True, default_value="") # type: ignore[assignment] # sentinel typing # noqa: E501
28+
api_key: str = router.current_event.get_header_value(
29+
name="X-Api-Key",
30+
case_sensitive=True,
31+
default_value="",
32+
) # sentinel typing # noqa: E501
2933

3034
todos: Response = requests.get(f"{endpoint}/{todo_id}", headers={"X-Api-Key": api_key})
3135
todos.raise_for_status()

0 commit comments

Comments
 (0)