Exemplo n.º 1
0
    def _do_unary_response(self, rpc_method, start_response, wrap_message,
                           context, headers, resp):
        if resp:
            message_data = wrap_message(False, False,
                                        rpc_method.response_serializer(resp))
        else:
            message_data = b""

        if context._initial_metadata:
            headers.extend(context._initial_metadata)

        trailers = [("grpc-status", str(context.code.value[0]))]

        if context.details:
            trailers.append(
                ("grpc-message", quote(context.details.encode("utf8"))))

        if context._trailing_metadata:
            trailers.extend(context._trailing_metadata)

        trailer_message = protocol.pack_trailers(trailers)
        trailer_data = wrap_message(True, False, trailer_message)

        content_length = len(message_data) + len(trailer_data)

        headers.append(("content-length", str(content_length)))

        start_response("200 OK", headers)

        yield message_data

        yield trailer_data
Exemplo n.º 2
0
    def _do_streaming_response(self, rpc_method, start_response, wrap_message,
                               context, headers, resp):
        try:
            first_message = next(resp)
        except grpc.RpcError:
            pass

        if context._initial_metadata:
            headers.extend(context._initial_metadata)

        start_response("200 OK", headers)

        yield wrap_message(False, False,
                           rpc_method.response_serializer(first_message))

        try:
            for message in resp:
                yield wrap_message(False, False,
                                   rpc_method.response_serializer(message))
        except grpc.RpcError:
            pass

        trailers = [("grpc-status", str(context.code.value[0]))]

        if context.details:
            trailers.append(
                ("grpc-message", quote(context.details.encode("utf8"))))

        if context._trailing_metadata:
            trailers.extend(context._trailing_metadata)

        trailer_message = protocol.pack_trailers(trailers)

        yield wrap_message(True, False, trailer_message)
Exemplo n.º 3
0
    async def _do_unary_response(self, rpc_method, receive, send, wrap_message,
                                 context, coroutine):
        headers = context._response_headers

        if coroutine is None:
            message = None
        else:
            message = await coroutine

        status = 200

        if context._initial_metadata:
            headers.extend(context._initial_metadata)

        if message is not None:
            message_data = wrap_message(
                False, False, rpc_method.response_serializer(message))
        else:
            message_data = b""

        trailers = [(b"grpc-status", str(context.code.value[0]).encode())]

        if context.details:
            trailers.append(
                (b"grpc-message",
                 quote(context.details.encode("utf8")).encode("ascii")))

        if context._trailing_metadata:
            trailers.extend(context._trailing_metadata)

        trailer_message = protocol.pack_trailers(trailers)
        trailer_data = wrap_message(True, False, trailer_message)

        content_length = len(message_data) + len(trailer_data)

        headers.append((b"content-length", str(content_length).encode()))

        await send({
            "type": "http.response.start",
            "status": status,
            "headers": headers
        })

        await send({
            "type": "http.response.body",
            "body": message_data,
            "more_body": True
        })

        await send({
            "type": "http.response.body",
            "body": trailer_data,
            "more_body": False
        })
Exemplo n.º 4
0
    async def _do_streaming_response(self, rpc_method, receive, send,
                                     wrap_message, context, coroutine):
        headers = context._response_headers

        if coroutine:
            message = await anext(coroutine)
        else:
            message = b""

        status = 200

        body = wrap_message(False, False,
                            rpc_method.response_serializer(message))

        if context._initial_metadata:
            headers.extend(context._initial_metadata)

        await send({
            "type": "http.response.start",
            "status": status,
            "headers": headers
        })

        await send({
            "type": "http.response.body",
            "body": body,
            "more_body": True
        })

        async for message in coroutine:
            body = wrap_message(False, False,
                                rpc_method.response_serializer(message))

            send_task = asyncio.create_task(
                send({
                    "type": "http.response.body",
                    "body": body,
                    "more_body": True
                }))

            recv_task = asyncio.create_task(receive())

            done, pending = await asyncio.wait(
                {send_task, recv_task}, return_when=asyncio.FIRST_COMPLETED)

            if recv_task in done:
                send_task.cancel()
                result = recv_task.result()
                if result["type"] == "http.disconnect":
                    break
            else:
                recv_task.cancel()

        trailers = [("grpc-status", str(context.code.value[0]))]

        if context.details:
            trailers.append(("grpc-message", quote(context.details)))

        if context._trailing_metadata:
            trailers.extend(context._trailing_metadata)

        trailer_message = protocol.pack_trailers(trailers)
        body = wrap_message(True, False, trailer_message)
        await send({
            "type": "http.response.body",
            "body": body,
            "more_body": False
        })