Skip to content

Option for custom response headers #51

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
13 changes: 11 additions & 2 deletions lambda_proxy/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(
binary_b64encode: bool = False,
ttl=None,
cache_control=None,
custom_headers: dict = None,
description: str = None,
tag: Tuple = None,
) -> None:
Expand All @@ -105,6 +106,7 @@ def __init__(
self.b64encode = binary_b64encode
self.ttl = ttl
self.cache_control = cache_control
self.custom_headers = custom_headers
self.description = description or self.endpoint.__doc__
self.tag = tag
if self.compression and self.compression not in ["gzip", "zlib", "deflate"]:
Expand Down Expand Up @@ -367,6 +369,7 @@ def _add_route(self, path: str, endpoint: Callable, **kwargs) -> None:
binary_encode = kwargs.pop("binary_b64encode", False)
ttl = kwargs.pop("ttl", None)
cache_control = kwargs.pop("cache_control", None)
custom_headers = kwargs.pop("custom_headers", None)
description = kwargs.pop("description", None)
tag = kwargs.pop("tag", None)

Expand Down Expand Up @@ -400,6 +403,7 @@ def _add_route(self, path: str, endpoint: Callable, **kwargs) -> None:
binary_encode,
ttl,
cache_control,
custom_headers,
description,
tag,
)
Expand Down Expand Up @@ -494,7 +498,7 @@ def new_func(*args, **kwargs) -> Callable:

def setup_docs(self) -> None:
"""Add default documentation routes."""
openapi_url = f"/openapi.json"
openapi_url = "/openapi.json"

def _openapi() -> Tuple[str, str, str]:
"""Return OpenAPI json."""
Expand Down Expand Up @@ -534,7 +538,7 @@ def _redoc_ui_html() -> Tuple[str, str, str]:

self._add_route("/redoc", _redoc_ui_html, cors=True, tag=["documentation"])

def response(
def response( # noqa: C901
self,
status: Union[int, str],
content_type: str,
Expand All @@ -546,6 +550,7 @@ def response(
b64encode: bool = False,
ttl: int = None,
cache_control: str = None,
custom_headers: dict = None,
):
"""Return HTTP response.

Expand Down Expand Up @@ -582,6 +587,9 @@ def response(
"headers": {"Content-Type": content_type},
}

if custom_headers:
messageData["headers"].update(custom_headers)

if cors:
messageData["headers"]["Access-Control-Allow-Origin"] = "*"
messageData["headers"]["Access-Control-Allow-Methods"] = ",".join(
Expand Down Expand Up @@ -715,4 +723,5 @@ def __call__(self, event, context):
b64encode=route_entry.b64encode,
ttl=route_entry.ttl,
cache_control=route_entry.cache_control,
custom_headers=route_entry.custom_headers,
)