From adab42c71bd3fdf1f9a6d8b16676c81663f1c30e Mon Sep 17 00:00:00 2001 From: Tim Holm Date: Wed, 27 Oct 2021 11:50:59 +1100 Subject: [PATCH] fix(faas): Fix http response handling. --- nitric/faas.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/nitric/faas.py b/nitric/faas.py index 9a92f27..214a888 100644 --- a/nitric/faas.py +++ b/nitric/faas.py @@ -14,6 +14,7 @@ ClientMessage, TriggerRequest, TriggerResponse, + HeaderValue, HttpResponseContext, TopicResponseContext, ) @@ -70,12 +71,13 @@ def _grpc_response_from_ctx(ctx: TriggerContext) -> TriggerResponse: """ if ctx.http(): ctx = ctx.http() + headers = {k: HeaderValue(value=v) for (k, v) in ctx.res.headers.items()} + headers_old = {k: v[0] for (k, v) in ctx.res.headers.items()} + data = ctx.res.body if ctx.res.body else bytes() + return TriggerResponse( - data=ctx.res.body, - http=HttpResponseContext( - status=ctx.res.status, - headers=ctx.res.headers, - ), + data=data, + http=HttpResponseContext(status=ctx.res.status, headers=headers, headers_old=headers_old), ) elif ctx.event(): ctx = ctx.event() @@ -115,7 +117,18 @@ def __init__(self, status: int = 200, headers: Record = None, body: bytes = None """Construct a new HttpResponse.""" self.status = status self.headers = headers if headers else {} - self.body = body if body else bytes() + self._body = body if body else bytes() + + @property + def body(self): + return self._body + + @body.setter + def body(self, value: Union[str, bytes]): + if isinstance(value, str): + self._body = value.encode("utf-8") + else: + self._body = value class HttpContext(TriggerContext): @@ -135,7 +148,7 @@ def http(self) -> HttpContext: def from_grpc_trigger_request(trigger_request: TriggerRequest) -> HttpContext: """Construct a new HttpContext from an Http trigger from the Nitric Membrane.""" if len(trigger_request.http.headers.keys()) > 0: - headers = {k: v[0].value for (k, v) in trigger_request.http.headers.items()} + headers = {k: v.value for (k, v) in trigger_request.http.headers.items()} else: headers = trigger_request.http.headers_old @@ -334,6 +347,7 @@ async def _run(self): else: func = self._any_handler response_ctx = (await func(ctx)) if asyncio.iscoroutinefunction(func) else func(ctx) + # Send function response back to server await request_channel.send( ClientMessage(