How to use the dapr.clients.grpc._response.InvokeServiceResponse function in dapr

To help you get started, we’ve selected a few dapr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dapr / python-sdk / tests / clients / test_dapr_grpc_response.py View on Github external
def test_is_proto_for_non_protobuf(self):
        test_data = GrpcAny(value=b'hello dapr')
        resp = InvokeServiceResponse(
            data=test_data,
            content_type='application/json')
        self.assertFalse(resp.is_proto())
github dapr / python-sdk / tests / clients / test_dapr_grpc_response.py View on Github external
def test_unpack(self):
        # arrange
        fake_req = common_v1.InvokeRequest(method="test")
        test_data = GrpcAny()
        test_data.Pack(fake_req)

        # act
        resp = InvokeServiceResponse(data=test_data)
        resp_proto = common_v1.InvokeRequest()
        resp.unpack(resp_proto)

        # assert
        self.assertEqual("test", resp_proto.method)
github dapr / python-sdk / tests / clients / test_dapr_grpc_response.py View on Github external
def test_non_protobuf_message(self):
        with self.assertRaises(ValueError):
            resp = InvokeServiceResponse(data="invalid_datatype")
            self.assertIsNone(resp, 'This should not be reached.')
github dapr / python-sdk / tests / clients / test_dapr_grpc_response.py View on Github external
def test_proto(self):
        fake_req = common_v1.InvokeRequest(method="test")
        test_data = GrpcAny()
        test_data.Pack(fake_req)
        resp = InvokeServiceResponse(data=test_data)
        self.assertIsNotNone(resp.data)
github dapr / python-sdk / tests / clients / test_dapr_grpc_response.py View on Github external
def test_is_proto_for_protobuf(self):
        fake_req = common_v1.InvokeRequest(method="test")
        test_data = GrpcAny()
        test_data.Pack(fake_req)
        resp = InvokeServiceResponse(data=test_data)
        self.assertTrue(resp.is_proto())
github dapr / python-sdk / tests / clients / test_dapr_grpc_response.py View on Github external
def test_data(self):
        test_data = GrpcAny(value=b'hello dapr')
        resp = InvokeServiceResponse(
            data=test_data,
            content_type='application/json')
        self.assertEqual(b'hello dapr', resp.content)
        self.assertEqual('hello dapr', resp.text())
        self.assertEqual('application/json', resp.content_type)
github dapr / python-sdk / dapr / clients / grpc / client.py View on Github external
http_ext = None
        if http_verb:
            http_ext = self._get_http_extension(http_verb, http_querystring)

        req = api_v1.InvokeServiceRequest(
            id=id,
            message=common_v1.InvokeRequest(
                method=method,
                data=req_data.data,
                content_type=req_data.content_type,
                http_extension=http_ext)
        )

        response, call = self._stub.InvokeService.with_call(req, metadata=metadata)

        return InvokeServiceResponse(
            response.data, response.content_type,
            call.initial_metadata(), call.trailing_metadata())
github dapr / python-sdk / ext / dapr-ext-grpc / dapr / ext / grpc / _servicier.py View on Github external
self,
            request: common_v1.InvokeRequest,
            context: grpc.ServicerContext) -> common_v1.InvokeResponse:
        """Invokes service method with InvokeRequest."""
        if request.method not in self._invoke_method_map:
            context.set_code(grpc.StatusCode.UNIMPLEMENTED)
            raise NotImplementedError(f'{request.method} method not implemented!')

        req = InvokeServiceRequest(request.data, request.content_type)
        req.metadata = context.invocation_metadata()
        resp = self._invoke_method_map[request.method](req)

        if not resp:
            return common_v1.InvokeResponse()

        resp_data = InvokeServiceResponse()
        if isinstance(resp, (bytes, str)):
            resp_data.set_data(resp)
            resp_data.content_type = DEFAULT_JSON_CONTENT_TYPE
        elif isinstance(resp, GrpcMessage):
            resp_data.set_data(resp)
        elif isinstance(resp, InvokeServiceResponse):
            resp_data = resp
        else:
            context.set_code(grpc.StatusCode.OUT_OF_RANGE)
            context.set_details(f'{type(resp)} is the invalid return type.')
            raise NotImplementedError(f'{request.method} method not implemented!')

        if len(resp_data.get_headers()) > 0:
            context.send_initial_metadata(resp_data.get_headers())

        return common_v1.InvokeResponse(
github dapr / python-sdk / dapr / clients / grpc / _response.py View on Github external
content_type: Optional[str] = None,
            headers: Optional[MetadataTuple] = (),
            trailers: Optional[MetadataTuple] = ()):
        """Initializes InvokeServiceReponse from :obj:`common_v1.InvokeResponse`.

        Args:
            data (:obj:`google.protobuf.any_pb2.Any`): the response data from Dapr response
            content_type (str, optional): the content type of the bytes data
            headers (Tuple, optional): the headers from Dapr gRPC response
            trailers (Tuple, optional): the trailers from Dapr gRPC response

        Raises:
            ValueError: if the response data is not :class:`google.protobuf.any_pb2.Any`
                object.
        """
        super(InvokeServiceResponse, self).__init__(headers, trailers)
        if not isinstance(data, GrpcAny):
            raise ValueError('data is not protobuf message.')
        self._proto_any = data
        self._content_type = content_type