Exemplo n.º 1
0
 def set_data(self, val: Union[str, bytes, GrpcAny, GrpcMessage, None]) -> None:
     """Sets data to request data."""
     if val is None:
         self._data = GrpcAny()
     elif isinstance(val, (bytes, str)):
         self._data = GrpcAny(value=to_bytes(val))
     elif isinstance(val, (GrpcAny, GrpcMessage)):
         self.pack(val)
     else:
         raise ValueError(f'invalid data type {type(val)}')
 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.data)
     self.assertEqual('hello dapr', resp.text())
     self.assertEqual('application/json', resp.content_type)
Exemplo n.º 3
0
    def __init__(self,
                 data: Union[bytes, str, GrpcMessage],
                 content_type: Optional[str] = None):
        """Inits InvokeServiceRequestData with data and content_type.

        Args:
            data (bytes, str, or :obj:`google.protobuf.message.Message`): the data
                which is used for invoke_service request.
            content_type (str): the content_type of data when the data is bytes.
                The default content type is application/json.

        Raises:
            ValueError: data is not bytes or :obj:`google.protobuf.message.Message`.
        """
        self._data = GrpcAny()

        if isinstance(data, str):
            data = data.encode('utf-8')

        if isinstance(data, bytes):
            self._data.value = data
            self._content_type = content_type
            if not content_type:
                self._content_type = DEFAULT_JSON_CONTENT_TYPE
        elif isinstance(data, GrpcMessage):
            self._data.Pack(data)
            self._content_type = None
        else:
            raise ValueError(f'invalid data type {type(data)}')
    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)
Exemplo n.º 5
0
    def _on_invoke(self, method_name, method_cb):
        self._servicier.register_method(method_name, method_cb)

        # fake context
        fake_context = MagicMock()
        fake_context.invocation_metadata.return_value = (
            ('key1', 'value1'),
            ('key2', 'value1'),
        )

        return self._servicier.OnInvoke(
            common_v1.InvokeRequest(method=method_name, data=GrpcAny()),
            fake_context,
        )
Exemplo n.º 6
0
    def pack(self, val: Union[GrpcAny, GrpcMessage]) -> None:
        """Serializes protocol buffer message.

        Args:
            message (:class:`GrpcMessage`, :class:`GrpcAny`): the protocol buffer message object

        Raises:
            ValueError: message is neither GrpcAny nor GrpcMessage.
        """
        if isinstance(val, GrpcAny):
            self._data = val
        elif isinstance(val, GrpcMessage):
            self._data = GrpcAny()
            self._data.Pack(val)
        else:
            raise ValueError('invalid data type')
Exemplo n.º 7
0
    def InvokeService(self, request, context) -> common_v1.InvokeResponse:
        headers = ()
        trailers = ()

        for k, v in context.invocation_metadata():
            headers = headers + (('h' + k, v), )
            trailers = trailers + (('t' + k, v), )

        resp = GrpcAny()
        content_type = ''

        if request.message.method == 'bytes':
            resp.value = request.message.data.value
            content_type = request.message.content_type
        else:
            resp = request.message.data

        context.send_initial_metadata(headers)
        context.set_trailing_metadata(trailers)

        return common_v1.InvokeResponse(data=resp, content_type=content_type)
 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())
 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())
 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)