1
+ from __future__ import annotations
2
+
1
3
import warnings
2
4
from collections import defaultdict
3
- from typing import Any , Dict , List , Union
5
+ from typing import TYPE_CHECKING , Any
4
6
5
- from aws_lambda_powertools .shared .cookies import Cookie
7
+ if TYPE_CHECKING :
8
+ from aws_lambda_powertools .shared .cookies import Cookie
6
9
7
10
8
11
class BaseHeadersSerializer :
@@ -11,23 +14,23 @@ class BaseHeadersSerializer:
11
14
ALB and Lambda Function URL response payload.
12
15
"""
13
16
14
- def serialize (self , headers : Dict [str , Union [ str , List [str ]]] , cookies : List [Cookie ]) -> Dict [str , Any ]:
17
+ def serialize (self , headers : dict [str , str | list [str ]], cookies : list [Cookie ]) -> dict [str , Any ]:
15
18
"""
16
19
Serializes headers and cookies according to the request type.
17
20
Returns a dict that can be merged with the response payload.
18
21
19
22
Parameters
20
23
----------
21
- headers: Dict [str, List [str]]
24
+ headers: dict [str, str | list [str]]
22
25
A dictionary of headers to set in the response
23
- cookies: List[str ]
26
+ cookies: list[Cookie ]
24
27
A list of cookies to set in the response
25
28
"""
26
29
raise NotImplementedError ()
27
30
28
31
29
32
class HttpApiHeadersSerializer (BaseHeadersSerializer ):
30
- def serialize (self , headers : Dict [str , Union [ str , List [str ]]] , cookies : List [Cookie ]) -> Dict [str , Any ]:
33
+ def serialize (self , headers : dict [str , str | list [str ]], cookies : list [Cookie ]) -> dict [str , Any ]:
31
34
"""
32
35
When using HTTP APIs or LambdaFunctionURLs, everything is taken care automatically for us.
33
36
We can directly assign a list of cookies and a dict of headers to the response payload, and the
@@ -39,7 +42,7 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
39
42
40
43
# Format 2.0 doesn't have multiValueHeaders or multiValueQueryStringParameters fields.
41
44
# Duplicate headers are combined with commas and included in the headers field.
42
- combined_headers : Dict [str , str ] = {}
45
+ combined_headers : dict [str , str ] = {}
43
46
for key , values in headers .items ():
44
47
# omit headers with explicit null values
45
48
if values is None :
@@ -54,7 +57,7 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
54
57
55
58
56
59
class MultiValueHeadersSerializer (BaseHeadersSerializer ):
57
- def serialize (self , headers : Dict [str , Union [ str , List [str ]]] , cookies : List [Cookie ]) -> Dict [str , Any ]:
60
+ def serialize (self , headers : dict [str , str | list [str ]], cookies : list [Cookie ]) -> dict [str , Any ]:
58
61
"""
59
62
When using REST APIs, headers can be encoded using the `multiValueHeaders` key on the response.
60
63
This is also the case when using an ALB integration with the `multiValueHeaders` option enabled.
@@ -63,7 +66,7 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
63
66
https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
64
67
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers-response
65
68
"""
66
- payload : Dict [str , List [str ]] = defaultdict (list )
69
+ payload : dict [str , list [str ]] = defaultdict (list )
67
70
for key , values in headers .items ():
68
71
# omit headers with explicit null values
69
72
if values is None :
@@ -83,14 +86,14 @@ def serialize(self, headers: Dict[str, Union[str, List[str]]], cookies: List[Coo
83
86
84
87
85
88
class SingleValueHeadersSerializer (BaseHeadersSerializer ):
86
- def serialize (self , headers : Dict [str , Union [ str , List [str ]]] , cookies : List [Cookie ]) -> Dict [str , Any ]:
89
+ def serialize (self , headers : dict [str , str | list [str ]], cookies : list [Cookie ]) -> dict [str , Any ]:
87
90
"""
88
91
The ALB integration has `multiValueHeaders` disabled by default.
89
92
If we try to set multiple headers with the same key, or more than one cookie, print a warning.
90
93
91
94
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#respond-to-load-balancer
92
95
"""
93
- payload : Dict [str , Dict [str , str ]] = {}
96
+ payload : dict [str , dict [str , str ]] = {}
94
97
payload .setdefault ("headers" , {})
95
98
96
99
if cookies :
0 commit comments