1
1
import base64
2
2
import json
3
+ import logging
3
4
import re
4
5
import zlib
5
6
from enum import Enum
10
11
from aws_lambda_powertools .utilities .data_classes .common import BaseProxyEvent
11
12
from aws_lambda_powertools .utilities .typing import LambdaContext
12
13
14
+ logger = logging .getLogger (__name__ )
15
+
13
16
14
17
class ProxyEventType (Enum ):
15
18
"""An enumerations of the supported proxy event types."""
@@ -170,6 +173,7 @@ def _compress(self):
170
173
"""Compress the response body, but only if `Accept-Encoding` headers includes gzip."""
171
174
self .response .headers ["Content-Encoding" ] = "gzip"
172
175
if isinstance (self .response .body , str ):
176
+ logger .debug ("Converting string response to bytes before compressing it" )
173
177
self .response .body = bytes (self .response .body , "utf-8" )
174
178
gzip = zlib .compressobj (9 , zlib .DEFLATED , zlib .MAX_WBITS | 16 )
175
179
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
190
194
self ._route (event , cors )
191
195
192
196
if isinstance (self .response .body , bytes ):
197
+ logger .debug ("Encoding bytes response with base64" )
193
198
self .response .base64_encoded = True
194
199
self .response .body = base64 .b64encode (self .response .body ).decode ()
195
200
return {
@@ -256,7 +261,7 @@ def get(self, rule: str, cors: bool = True, compress: bool = False, cache_contro
256
261
```python
257
262
from aws_lambda_powertools import Tracer
258
263
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
259
- >>>
264
+
260
265
tracer = Tracer()
261
266
app = ApiGatewayResolver()
262
267
@@ -380,8 +385,10 @@ def route(self, rule: str, method: str, cors: bool = True, compress: bool = Fals
380
385
"""Route decorator includes parameter `method`"""
381
386
382
387
def register_resolver (func : Callable ):
388
+ logger .debug (f"Adding route using rule { rule } and method { method .upper ()} " )
383
389
self ._routes .append (Route (method , self ._compile_regex (rule ), func , cors , compress , cache_control ))
384
390
if cors :
391
+ logger .debug (f"Registering method { method .upper ()} to Allow Methods in CORS" )
385
392
self ._cors_methods .add (method .upper ())
386
393
return func
387
394
@@ -417,9 +424,12 @@ def _compile_regex(rule: str):
417
424
def _to_proxy_event (self , event : Dict ) -> BaseProxyEvent :
418
425
"""Convert the event dict to the corresponding data class"""
419
426
if self ._proxy_type == ProxyEventType .APIGatewayProxyEvent :
427
+ logger .debug ("Converting event to API Gateway REST API contract" )
420
428
return APIGatewayProxyEvent (event )
421
429
if self ._proxy_type == ProxyEventType .APIGatewayProxyEventV2 :
430
+ logger .debug ("Converting event to API Gateway HTTP API contract" )
422
431
return APIGatewayProxyEventV2 (event )
432
+ logger .debug ("Converting event to ALB contract" )
423
433
return ALBEvent (event )
424
434
425
435
def _resolve (self ) -> ResponseBuilder :
@@ -431,17 +441,21 @@ def _resolve(self) -> ResponseBuilder:
431
441
continue
432
442
match : Optional [re .Match ] = route .rule .match (path )
433
443
if match :
444
+ logger .debug ("Found a registered route. Calling function" )
434
445
return self ._call_route (route , match .groupdict ())
435
446
447
+ logger .debug (f"No match found for path { path } and method { method } " )
436
448
return self ._not_found (method )
437
449
438
450
def _not_found (self , method : str ) -> ResponseBuilder :
439
451
"""Called when no matching route was found and includes support for the cors preflight response"""
440
452
headers = {}
441
453
if self ._cors :
454
+ logger .debug ("CORS is enabled, updating headers." )
442
455
headers .update (self ._cors .to_dict ())
443
456
444
- if method == "OPTIONS" : # Preflight
457
+ if method == "OPTIONS" : # Pre-flight
458
+ logger .debug ("Pre-flight request detected. Returning CORS with null response" )
445
459
headers ["Access-Control-Allow-Methods" ] = "," .join (sorted (self ._cors_methods ))
446
460
return ResponseBuilder (Response (status_code = 204 , content_type = None , headers = headers , body = None ))
447
461
@@ -471,6 +485,7 @@ def _to_response(result: Union[Dict, Response]) -> Response:
471
485
if isinstance (result , Response ):
472
486
return result
473
487
488
+ logger .debug ("Simple response detected, serializing return before constructing final response" )
474
489
return Response (
475
490
status_code = 200 ,
476
491
content_type = "application/json" ,
0 commit comments