2
2
from collections import defaultdict
3
3
from typing import Any , Dict , List , Union
4
4
5
+ from aws_lambda_powertools .event_handler .cookies import Cookie
6
+
5
7
6
8
class BaseHeadersSerializer :
7
9
"""
8
10
Helper class to correctly serialize headers and cookies for Amazon API Gateway,
9
11
ALB and Lambda Function URL response payload.
10
12
"""
11
13
12
- def serialize (self , headers : Dict [str , Union [str , List [str ]]], cookies : List [str ]) -> Dict [str , Any ]:
14
+ def serialize (self , headers : Dict [str , Union [str , List [str ]]], cookies : List [Cookie ]) -> Dict [str , Any ]:
13
15
"""
14
16
Serializes headers and cookies according to the request type.
15
17
Returns a dict that can be merged with the response payload.
@@ -25,7 +27,7 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[str
25
27
26
28
27
29
class HttpApiHeadersSerializer (BaseHeadersSerializer ):
28
- def serialize (self , headers : Dict [str , Union [str , List [str ]]], cookies : List [str ]) -> Dict [str , Any ]:
30
+ def serialize (self , headers : Dict [str , Union [str , List [str ]]], cookies : List [Cookie ]) -> Dict [str , Any ]:
29
31
"""
30
32
When using HTTP APIs or LambdaFunctionURLs, everything is taken care automatically for us.
31
33
We can directly assign a list of cookies and a dict of headers to the response payload, and the
@@ -44,11 +46,11 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[str
44
46
else :
45
47
combined_headers [key ] = ", " .join (values )
46
48
47
- return {"headers" : combined_headers , "cookies" : cookies }
49
+ return {"headers" : combined_headers , "cookies" : list ( map ( str , cookies )) }
48
50
49
51
50
52
class MultiValueHeadersSerializer (BaseHeadersSerializer ):
51
- def serialize (self , headers : Dict [str , Union [str , List [str ]]], cookies : List [str ]) -> Dict [str , Any ]:
53
+ def serialize (self , headers : Dict [str , Union [str , List [str ]]], cookies : List [Cookie ]) -> Dict [str , Any ]:
52
54
"""
53
55
When using REST APIs, headers can be encoded using the `multiValueHeaders` key on the response.
54
56
This is also the case when using an ALB integration with the `multiValueHeaders` option enabled.
@@ -69,13 +71,13 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[str
69
71
if cookies :
70
72
payload .setdefault ("Set-Cookie" , [])
71
73
for cookie in cookies :
72
- payload ["Set-Cookie" ].append (cookie )
74
+ payload ["Set-Cookie" ].append (str ( cookie ) )
73
75
74
76
return {"multiValueHeaders" : payload }
75
77
76
78
77
79
class SingleValueHeadersSerializer (BaseHeadersSerializer ):
78
- def serialize (self , headers : Dict [str , Union [str , List [str ]]], cookies : List [str ]) -> Dict [str , Any ]:
80
+ def serialize (self , headers : Dict [str , Union [str , List [str ]]], cookies : List [Cookie ]) -> Dict [str , Any ]:
79
81
"""
80
82
The ALB integration has `multiValueHeaders` disabled by default.
81
83
If we try to set multiple headers with the same key, or more than one cookie, print a warning.
@@ -93,7 +95,7 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[str
93
95
)
94
96
95
97
# We can only send one cookie, send the last one
96
- payload ["headers" ]["Set-Cookie" ] = cookies [- 1 ]
98
+ payload ["headers" ]["Set-Cookie" ] = str ( cookies [- 1 ])
97
99
98
100
for key , values in headers .items ():
99
101
if isinstance (values , str ):
0 commit comments