15
15
Any ,
16
16
Callable ,
17
17
Dict ,
18
+ Generic ,
18
19
List ,
19
20
Match ,
20
21
Optional ,
23
24
Set ,
24
25
Tuple ,
25
26
Type ,
27
+ TypeVar ,
26
28
Union ,
27
29
cast ,
28
30
)
63
65
_DEFAULT_OPENAPI_RESPONSE_DESCRIPTION = "Successful Response"
64
66
_ROUTE_REGEX = "^{}$"
65
67
68
+ ResponseEventT = TypeVar ("ResponseEventT" , bound = BaseProxyEvent )
69
+
66
70
if TYPE_CHECKING :
67
71
from aws_lambda_powertools .event_handler .openapi .compat import (
68
72
JsonSchemaValue ,
@@ -691,14 +695,14 @@ def _generate_operation_id(self) -> str:
691
695
return operation_id
692
696
693
697
694
- class ResponseBuilder :
698
+ class ResponseBuilder ( Generic [ ResponseEventT ]) :
695
699
"""Internally used Response builder"""
696
700
697
701
def __init__ (self , response : Response , route : Optional [Route ] = None ):
698
702
self .response = response
699
703
self .route = route
700
704
701
- def _add_cors (self , event : BaseProxyEvent , cors : CORSConfig ):
705
+ def _add_cors (self , event : ResponseEventT , cors : CORSConfig ):
702
706
"""Update headers to include the configured Access-Control headers"""
703
707
self .response .headers .update (cors .to_dict (event .get_header_value ("Origin" )))
704
708
@@ -711,7 +715,7 @@ def _add_cache_control(self, cache_control: str):
711
715
def _has_compression_enabled (
712
716
route_compression : bool ,
713
717
response_compression : Optional [bool ],
714
- event : BaseProxyEvent ,
718
+ event : ResponseEventT ,
715
719
) -> bool :
716
720
"""
717
721
Checks if compression is enabled.
@@ -724,7 +728,7 @@ def _has_compression_enabled(
724
728
A boolean indicating whether compression is enabled or not in the route setting.
725
729
response_compression: bool, optional
726
730
A boolean indicating whether compression is enabled or not in the response setting.
727
- event: BaseProxyEvent
731
+ event: Generic[ResponseEventT]
728
732
The event object containing the request details.
729
733
730
734
Returns
@@ -754,7 +758,7 @@ def _compress(self):
754
758
gzip = zlib .compressobj (9 , zlib .DEFLATED , zlib .MAX_WBITS | 16 )
755
759
self .response .body = gzip .compress (self .response .body ) + gzip .flush ()
756
760
757
- def _route (self , event : BaseProxyEvent , cors : Optional [CORSConfig ]):
761
+ def _route (self , event : ResponseEventT , cors : Optional [CORSConfig ]):
758
762
"""Optionally handle any of the route's configure response handling"""
759
763
if self .route is None :
760
764
return
@@ -769,7 +773,7 @@ def _route(self, event: BaseProxyEvent, cors: Optional[CORSConfig]):
769
773
):
770
774
self ._compress ()
771
775
772
- def build (self , event : BaseProxyEvent , cors : Optional [CORSConfig ] = None ) -> Dict [str , Any ]:
776
+ def build (self , event : ResponseEventT , cors : Optional [CORSConfig ] = None ) -> Dict [str , Any ]:
773
777
"""Build the full response dict to be returned by the lambda"""
774
778
self ._route (event , cors )
775
779
@@ -1317,7 +1321,7 @@ def __init__(
1317
1321
self ._strip_prefixes = strip_prefixes
1318
1322
self .context : Dict = {} # early init as customers might add context before event resolution
1319
1323
self .processed_stack_frames = []
1320
- self ._response_builder_class = ResponseBuilder
1324
+ self ._response_builder_class = ResponseBuilder [ BaseProxyEvent ]
1321
1325
1322
1326
# Allow for a custom serializer or a concise json serialization
1323
1327
self ._serializer = serializer or partial (json .dumps , separators = ("," , ":" ), cls = Encoder )
0 commit comments