def test_unpack(self): # arrange fake_req = common_v1.InvokeRequest(method="test") # act resp = InvokeServiceResponse(data=fake_req) resp_proto = common_v1.InvokeRequest() resp.unpack(resp_proto) # assert self.assertEqual("test", resp_proto.method)
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, )
def test_proto_message_data(self): # arrange fake_req = common_v1.InvokeRequest(method="test") # act req = InvokeMethodRequest(data=fake_req) # assert self.assertIsNotNone(req.proto) self.assertEqual( 'type.googleapis.com/dapr.proto.common.v1.InvokeRequest', req.proto.type_url) self.assertIsNotNone(req.proto.value) self.assertIsNone(req.content_type)
def test_proto_message_data(self): # arrange fake_req = common_v1.InvokeRequest(method="test") # act data = InvokeServiceRequestData(data=fake_req) # assert self.assertIsNotNone(data.data) self.assertEqual( 'type.googleapis.com/dapr.proto.common.v1.InvokeRequest', data.data.type_url) self.assertIsNotNone(data.data.value) self.assertIsNone(data.content_type)
def test_proto(self): fake_req = common_v1.InvokeRequest(method="test") resp = InvokeServiceResponse(data=fake_req) self.assertIsNotNone(resp.proto)
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_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)
def invoke_method( self, app_id: str, method_name: str, data: Union[bytes, str, GrpcMessage], content_type: Optional[str] = None, metadata: Optional[MetadataTuple] = None, http_verb: Optional[str] = None, http_querystring: Optional[MetadataTuple] = None ) -> InvokeMethodResponse: """Invokes the target service to call method. This can invoke the specified target service to call method with bytes array data or custom protocol buffer message. If your callee application uses http appcallback, http_verb and http_querystring must be specified. Otherwise, Dapr runtime will return error. The example calls `callee` service with bytes data, which implements grpc appcallback: from dapr.clients import DaprClient with DaprClient() as d: resp = d.invoke_method( app_id='callee', method_name='method', data=b'message', content_type='text/plain', metadata=( ('header1', 'value1') ), ) # resp.content includes the content in bytes. # resp.content_type specifies the content type of resp.content. # Thus, resp.content can be deserialized properly. When sending custom protocol buffer message object, it doesn't requires content_type: from dapr.clients import DaprClient req_data = dapr_example_v1.CustomRequestMessage(data='custom') with DaprClient() as d: resp = d.invoke_method( app_id='callee', method_name='method', data=req_data, metadata=( ('header1', 'value1') ), ) # Create protocol buffer object resp_data = dapr_example_v1.CustomResponseMessage() # Deserialize to resp_data resp.unpack(resp_data) The example calls `callee` service which implements http appcallback: from dapr.clients import DaprClient with DaprClient() as d: resp = d.invoke_method( app_id='callee', method_name='method', data=b'message', content_type='text/plain', metadata=( ('header1', 'value1') ), http_verb='POST', http_querystring=( ('key1', 'value1') ), ) # resp.content includes the content in bytes. # resp.content_type specifies the content type of resp.content. # Thus, resp.content can be deserialized properly. Args: app_id (str): the callee app id method (str): the method name which is called data (bytes or :obj:`google.protobuf.message.Message`): bytes or Message for data which will send to id metadata (tuple, optional): custom metadata http_verb (str, optional): http method verb to call HTTP callee application http_querystring (tuple, optional): the tuple to represent query string Returns: :class:`InvokeMethodResponse` object returned from callee """ req_data = InvokeMethodRequest(data, content_type) http_ext = None if http_verb: http_ext = self._get_http_extension(http_verb, http_querystring) req = api_v1.InvokeServiceRequest( id=app_id, message=common_v1.InvokeRequest(method=method_name, data=req_data.proto, content_type=req_data.content_type, http_extension=http_ext)) response, call = self._stub.InvokeService.with_call(req, metadata=metadata) resp_data = InvokeMethodResponse(response.data, response.content_type) resp_data.headers = call.initial_metadata() # type: ignore return resp_data