@@ -55,11 +55,11 @@ def with_cors():
55
55
)
56
56
app = ApiGatewayResolver(cors=cors_config)
57
57
58
- @app.get("/my/path", cors=True )
58
+ @app.get("/my/path")
59
59
def with_cors():
60
60
return {"message": "Foo"}
61
61
62
- @app.get("/another-one")
62
+ @app.get("/another-one", cors=False )
63
63
def without_cors():
64
64
return {"message": "Foo"}
65
65
"""
@@ -249,9 +249,10 @@ def __init__(self, proxy_type: Enum = ProxyEventType.APIGatewayProxyEvent, cors:
249
249
self ._proxy_type = proxy_type
250
250
self ._routes : List [Route ] = []
251
251
self ._cors = cors
252
+ self ._cors_enabled : bool = cors is not None
252
253
self ._cors_methods : Set [str ] = {"OPTIONS" }
253
254
254
- def get (self , rule : str , cors : bool = True , compress : bool = False , cache_control : str = None ):
255
+ def get (self , rule : str , cors : bool = None , compress : bool = False , cache_control : str = None ):
255
256
"""Get route decorator with GET `method`
256
257
257
258
Examples
@@ -276,7 +277,7 @@ def lambda_handler(event, context):
276
277
"""
277
278
return self .route (rule , "GET" , cors , compress , cache_control )
278
279
279
- def post (self , rule : str , cors : bool = True , compress : bool = False , cache_control : str = None ):
280
+ def post (self , rule : str , cors : bool = None , compress : bool = False , cache_control : str = None ):
280
281
"""Post route decorator with POST `method`
281
282
282
283
Examples
@@ -302,7 +303,7 @@ def lambda_handler(event, context):
302
303
"""
303
304
return self .route (rule , "POST" , cors , compress , cache_control )
304
305
305
- def put (self , rule : str , cors : bool = True , compress : bool = False , cache_control : str = None ):
306
+ def put (self , rule : str , cors : bool = None , compress : bool = False , cache_control : str = None ):
306
307
"""Put route decorator with PUT `method`
307
308
308
309
Examples
@@ -317,7 +318,7 @@ def put(self, rule: str, cors: bool = True, compress: bool = False, cache_contro
317
318
app = ApiGatewayResolver()
318
319
319
320
@app.put("/put-call")
320
- def simple_post ():
321
+ def simple_put ():
321
322
put_data: dict = app.current_event.json_body
322
323
return {"message": put_data["value"]}
323
324
@@ -328,7 +329,7 @@ def lambda_handler(event, context):
328
329
"""
329
330
return self .route (rule , "PUT" , cors , compress , cache_control )
330
331
331
- def delete (self , rule : str , cors : bool = True , compress : bool = False , cache_control : str = None ):
332
+ def delete (self , rule : str , cors : bool = None , compress : bool = False , cache_control : str = None ):
332
333
"""Delete route decorator with DELETE `method`
333
334
334
335
Examples
@@ -353,7 +354,7 @@ def lambda_handler(event, context):
353
354
"""
354
355
return self .route (rule , "DELETE" , cors , compress , cache_control )
355
356
356
- def patch (self , rule : str , cors : bool = True , compress : bool = False , cache_control : str = None ):
357
+ def patch (self , rule : str , cors : bool = None , compress : bool = False , cache_control : str = None ):
357
358
"""Patch route decorator with PATCH `method`
358
359
359
360
Examples
@@ -381,13 +382,17 @@ def lambda_handler(event, context):
381
382
"""
382
383
return self .route (rule , "PATCH" , cors , compress , cache_control )
383
384
384
- def route (self , rule : str , method : str , cors : bool = True , compress : bool = False , cache_control : str = None ):
385
+ def route (self , rule : str , method : str , cors : bool = None , compress : bool = False , cache_control : str = None ):
385
386
"""Route decorator includes parameter `method`"""
386
387
387
388
def register_resolver (func : Callable ):
388
389
logger .debug (f"Adding route using rule { rule } and method { method .upper ()} " )
389
- self ._routes .append (Route (method , self ._compile_regex (rule ), func , cors , compress , cache_control ))
390
- if cors :
390
+ if cors is None :
391
+ cors_enabled = self ._cors_enabled
392
+ else :
393
+ cors_enabled = cors
394
+ self ._routes .append (Route (method , self ._compile_regex (rule ), func , cors_enabled , compress , cache_control ))
395
+ if cors_enabled :
391
396
logger .debug (f"Registering method { method .upper ()} to Allow Methods in CORS" )
392
397
self ._cors_methods .add (method .upper ())
393
398
return func
@@ -454,7 +459,7 @@ def _not_found(self, method: str) -> ResponseBuilder:
454
459
logger .debug ("CORS is enabled, updating headers." )
455
460
headers .update (self ._cors .to_dict ())
456
461
457
- if method == "OPTIONS" : # Pre-flight
462
+ if method == "OPTIONS" :
458
463
logger .debug ("Pre-flight request detected. Returning CORS with null response" )
459
464
headers ["Access-Control-Allow-Methods" ] = "," .join (sorted (self ._cors_methods ))
460
465
return ResponseBuilder (Response (status_code = 204 , content_type = None , headers = headers , body = None ))
0 commit comments