Skip to content

Commit 91f9744

Browse files
committed
fix: add debugging statements
1 parent dc01e40 commit 91f9744

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import json
3+
import logging
34
import re
45
import zlib
56
from enum import Enum
@@ -10,6 +11,8 @@
1011
from aws_lambda_powertools.utilities.data_classes.common import BaseProxyEvent
1112
from aws_lambda_powertools.utilities.typing import LambdaContext
1213

14+
logger = logging.getLogger(__name__)
15+
1316

1417
class ProxyEventType(Enum):
1518
"""An enumerations of the supported proxy event types."""
@@ -170,6 +173,7 @@ def _compress(self):
170173
"""Compress the response body, but only if `Accept-Encoding` headers includes gzip."""
171174
self.response.headers["Content-Encoding"] = "gzip"
172175
if isinstance(self.response.body, str):
176+
logger.debug("Converting string response to bytes before compressing it")
173177
self.response.body = bytes(self.response.body, "utf-8")
174178
gzip = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
175179
self.response.body = gzip.compress(self.response.body) + gzip.flush()
@@ -190,6 +194,7 @@ def build(self, event: BaseProxyEvent, cors: CORSConfig = None) -> Dict[str, Any
190194
self._route(event, cors)
191195

192196
if isinstance(self.response.body, bytes):
197+
logger.debug("Encoding bytes response with base64")
193198
self.response.base64_encoded = True
194199
self.response.body = base64.b64encode(self.response.body).decode()
195200
return {
@@ -256,7 +261,7 @@ def get(self, rule: str, cors: bool = True, compress: bool = False, cache_contro
256261
```python
257262
from aws_lambda_powertools import Tracer
258263
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
259-
>>>
264+
260265
tracer = Tracer()
261266
app = ApiGatewayResolver()
262267
@@ -380,8 +385,10 @@ def route(self, rule: str, method: str, cors: bool = True, compress: bool = Fals
380385
"""Route decorator includes parameter `method`"""
381386

382387
def register_resolver(func: Callable):
388+
logger.debug(f"Adding route using rule {rule} and method {method.upper()}")
383389
self._routes.append(Route(method, self._compile_regex(rule), func, cors, compress, cache_control))
384390
if cors:
391+
logger.debug(f"Registering method {method.upper()} to Allow Methods in CORS")
385392
self._cors_methods.add(method.upper())
386393
return func
387394

@@ -417,9 +424,12 @@ def _compile_regex(rule: str):
417424
def _to_proxy_event(self, event: Dict) -> BaseProxyEvent:
418425
"""Convert the event dict to the corresponding data class"""
419426
if self._proxy_type == ProxyEventType.APIGatewayProxyEvent:
427+
logger.debug("Converting event to API Gateway REST API contract")
420428
return APIGatewayProxyEvent(event)
421429
if self._proxy_type == ProxyEventType.APIGatewayProxyEventV2:
430+
logger.debug("Converting event to API Gateway HTTP API contract")
422431
return APIGatewayProxyEventV2(event)
432+
logger.debug("Converting event to ALB contract")
423433
return ALBEvent(event)
424434

425435
def _resolve(self) -> ResponseBuilder:
@@ -431,17 +441,21 @@ def _resolve(self) -> ResponseBuilder:
431441
continue
432442
match: Optional[re.Match] = route.rule.match(path)
433443
if match:
444+
logger.debug("Found a registered route. Calling function")
434445
return self._call_route(route, match.groupdict())
435446

447+
logger.debug(f"No match found for path {path} and method {method}")
436448
return self._not_found(method)
437449

438450
def _not_found(self, method: str) -> ResponseBuilder:
439451
"""Called when no matching route was found and includes support for the cors preflight response"""
440452
headers = {}
441453
if self._cors:
454+
logger.debug("CORS is enabled, updating headers.")
442455
headers.update(self._cors.to_dict())
443456

444-
if method == "OPTIONS": # Preflight
457+
if method == "OPTIONS": # Pre-flight
458+
logger.debug("Pre-flight request detected. Returning CORS with null response")
445459
headers["Access-Control-Allow-Methods"] = ",".join(sorted(self._cors_methods))
446460
return ResponseBuilder(Response(status_code=204, content_type=None, headers=headers, body=None))
447461

@@ -471,6 +485,7 @@ def _to_response(result: Union[Dict, Response]) -> Response:
471485
if isinstance(result, Response):
472486
return result
473487

488+
logger.debug("Simple response detected, serializing return before constructing final response")
474489
return Response(
475490
status_code=200,
476491
content_type="application/json",

0 commit comments

Comments
 (0)