Skip to content

Commit 6f6a55c

Browse files
author
Michael Brewer
committed
refactor(event-handler): Code cleanup
1 parent daaf137 commit 6f6a55c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,31 @@ def resolve(self, event: Dict, context: LambdaContext) -> Dict:
6161

6262
route, args = self._find_route(self.current_event.http_method, self.current_event.path)
6363
result = route.func(**args)
64+
status_code: int = result[0]
65+
content_type: str = result[1]
66+
body: Union[str, bytes] = result[2]
67+
headers = {"Content-Type": content_type}
6468

65-
status: int = result[0]
66-
response: Dict[str, Any] = {"statusCode": status}
67-
68-
headers = {"Content-Type": result[1]}
6969
if route.cors:
7070
headers["Access-Control-Allow-Origin"] = "*"
7171
headers["Access-Control-Allow-Methods"] = route.method
7272
headers["Access-Control-Allow-Credentials"] = "true"
73+
7374
if route.cache_control:
74-
headers["Cache-Control"] = route.cache_control if status == 200 else "no-cache"
75-
response["headers"] = headers
75+
headers["Cache-Control"] = route.cache_control if status_code == 200 else "no-cache"
7676

77-
body: Union[str, bytes] = result[2]
7877
if route.compress and "gzip" in (self.current_event.get_header_value("accept-encoding") or ""):
79-
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
8078
if isinstance(body, str):
8179
body = bytes(body, "utf-8")
80+
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
8281
body = gzip_compress.compress(body) + gzip_compress.flush()
82+
83+
base64_encoded = False
8384
if isinstance(body, bytes):
84-
response["isBase64Encoded"] = True
85+
base64_encoded = True
8586
body = base64.b64encode(body).decode()
86-
response["body"] = body
8787

88-
return response
88+
return {"statusCode": status_code, "headers": headers, "body": body, "isBase64Encoded": base64_encoded}
8989

9090
def _append(self, func: Callable, rule: str, method: str, cors: bool, compress: bool, cache_control: Optional[str]):
9191
self._routes.append(RouteEntry(method, self._build_rule_pattern(rule), func, cors, compress, cache_control))

0 commit comments

Comments
 (0)