From 3851580f04f9fa53bf6e4398d1b3c091cb67432d Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Wed, 13 May 2020 22:07:33 -0600 Subject: [PATCH 1/3] Option for custom response headers --- lambda_proxy/proxy.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lambda_proxy/proxy.py b/lambda_proxy/proxy.py index c16a363..fd1a8ab 100644 --- a/lambda_proxy/proxy.py +++ b/lambda_proxy/proxy.py @@ -546,6 +546,7 @@ def response( b64encode: bool = False, ttl: int = None, cache_control: str = None, + custom_headers: dict = None, ): """Return HTTP response. @@ -582,6 +583,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( From 9a0f3e157d4ba1168dbfb65a2ce276d1f3c3f25f Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Wed, 13 May 2020 22:15:40 -0600 Subject: [PATCH 2/3] Hide complexity error --- lambda_proxy/proxy.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lambda_proxy/proxy.py b/lambda_proxy/proxy.py index fd1a8ab..4814fd5 100644 --- a/lambda_proxy/proxy.py +++ b/lambda_proxy/proxy.py @@ -494,7 +494,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.""" @@ -534,7 +534,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, From 43a1705775d204f50b261b13cf896c0b936bb9e7 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Wed, 13 May 2020 22:51:24 -0600 Subject: [PATCH 3/3] Fix custom_headers --- lambda_proxy/proxy.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lambda_proxy/proxy.py b/lambda_proxy/proxy.py index 4814fd5..a1bda9d 100644 --- a/lambda_proxy/proxy.py +++ b/lambda_proxy/proxy.py @@ -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: @@ -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"]: @@ -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) @@ -400,6 +403,7 @@ def _add_route(self, path: str, endpoint: Callable, **kwargs) -> None: binary_encode, ttl, cache_control, + custom_headers, description, tag, ) @@ -719,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, )