Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(api_gateway): new event handler for API Gateway and ALB #418

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class CORSConfig(object):
def __init__(
self,
allow_origin: str = "*",
allow_headers: List[str] = None,
expose_headers: List[str] = None,
max_age: int = None,
allow_headers: Optional[List[str]] = None,
expose_headers: Optional[List[str]] = None,
max_age: Optional[int] = None,
allow_credentials: bool = False,
):
"""
Expand All @@ -77,13 +77,13 @@ def __init__(
allow_origin: str
The value of the `Access-Control-Allow-Origin` to send in the response. Defaults to "*", but should
only be used during development.
allow_headers: str
allow_headers: Optional[List[str]]
The list of additional allowed headers. This list is added to list of
built in allowed headers: `Authorization`, `Content-Type`, `X-Amz-Date`,
`X-Api-Key`, `X-Amz-Security-Token`.
expose_headers: str
expose_headers: Optional[List[str]]
A list of values to return for the Access-Control-Expose-Headers
max_age: int
max_age: Optional[int]
The value for the `Access-Control-Max-Age`
allow_credentials: bool
A boolean value that sets the value of `Access-Control-Allow-Credentials`
Expand Down Expand Up @@ -247,27 +247,27 @@ def __init__(self, proxy_type: Enum = ProxyEventType.APIGatewayProxyEvent, cors:
self._cors = cors
self._cors_methods: Set[str] = {"OPTIONS"}

def get(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def get(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Get route decorator with GET `method`"""
return self.route(rule, "GET", cors, compress, cache_control)

def post(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def post(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Post route decorator with POST `method`"""
return self.route(rule, "POST", cors, compress, cache_control)

def put(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def put(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Put route decorator with PUT `method`"""
return self.route(rule, "PUT", cors, compress, cache_control)

def delete(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def delete(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
heitorlessa marked this conversation as resolved.
Show resolved Hide resolved
"""Delete route decorator with DELETE `method`"""
return self.route(rule, "DELETE", cors, compress, cache_control)

def patch(self, rule: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def patch(self, rule: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Patch route decorator with PATCH `method`"""
return self.route(rule, "PATCH", cors, compress, cache_control)

def route(self, rule: str, method: str, cors: bool = False, compress: bool = False, cache_control: str = None):
def route(self, rule: str, method: str, cors: bool = True, compress: bool = False, cache_control: str = None):
"""Route decorator includes parameter `method`"""

def register_resolver(func: Callable):
Expand Down Expand Up @@ -361,11 +361,9 @@ def _to_response(result: Union[Dict, Response]) -> Response:
"""
if isinstance(result, Response):
return result
elif isinstance(result, dict):
return Response(
status_code=200,
content_type="application/json",
body=json.dumps(result, separators=(",", ":"), cls=Encoder),
)
else: # Tuple[int, str, Union[bytes, str]]
return Response(*result)

return Response(
status_code=200,
content_type="application/json",
body=json.dumps(result, separators=(",", ":"), cls=Encoder),
)
Loading