def DaprInvoke(self, method, req, res_type): envelope = dapr_messages.InvokeServiceRequest( id=self.simId, message=commonv1pb.InvokeRequest( method=method, data=protobuf_helpers.to_any_pb(req), content_type="text/plain; charset=UTF-8" ) ) res = self.client.InvokeService(envelope) res = protobuf_helpers.from_any_pb(res_type, res.data) return res
from dapr.proto.common.v1 import common_pb2 as commonv1pb from dapr.proto.dapr.v1 import dapr_pb2 as dapr_messages from dapr.proto.dapr.v1 import dapr_pb2_grpc as dapr_services from google.protobuf.any_pb2 import Any # Start a gRPC client port = os.getenv('DAPR_GRPC_PORT') channel = grpc.insecure_channel(f"localhost:{port}") client = dapr_services.DaprStub(channel) print(f"Started gRPC client on DAPR_GRPC_PORT: {port}") # Create a typed message with content type and body test_message = Any(value='INVOKE_RECEIVED'.encode('utf-8')) # Invoke the method 'my-method' on receiver req = dapr_messages.InvokeServiceRequest( id="invoke-receiver", message=commonv1pb.InvokeRequest( method='my-method', data=test_message, content_type="text/plain; charset=UTF-8") ) response = client.InvokeService(req) # Print the response print(response.content_type) print(response.data.value) channel.close()
import proto.response_pb2 as response_messages from google.protobuf.any_pb2 import Any # Start a gRPC client port = os.getenv('DAPR_GRPC_PORT') channel = grpc.insecure_channel(f"localhost:{port}") client = dapr_services.DaprStub(channel) print(f"Started gRPC client on DAPR_GRPC_PORT: {port}") # Invoke the Receiver req = dapr_messages.InvokeServiceRequest( id="invoke-receiver", message=commonv1pb.InvokeRequest( method='my_method', data=Any(value='SOME_DATA'.encode('utf-8')), content_type="text/plain; charset=UTF-8")) response = client.InvokeService(req) # Unpack the response res = response_messages.CustomResponse() if response.data.Is(response_messages.CustomResponse.DESCRIPTOR): response.data.Unpack(res) print("test", flush=True) # Print Result print(res, flush=True) channel.close()
DAPR_PORT_GRPC = os.getenv('DAPR_GRPC_PORT', 50001) # Note: currently 50001 is always default logger.info(f"==================================================") logger.info( f"DAPR_PORT_GRPC: {DAPR_PORT_GRPC}; DAPR_PORT_HTTP: {DAPR_PORT_HTTP}") logger.info(f"==================================================") # Start a gRPC client channel = grpc.insecure_channel(f"localhost:{DAPR_PORT_GRPC}") client = dapr_services.DaprStub(channel) logger.info(f"Started Dapr Gateway client on DAPR_GRPC_PORT: {DAPR_PORT_GRPC}") req = dapr_messages.InvokeServiceRequest( id= "id-demo-grpc-server", # As described in the demo-grpc-client.yaml pod description message=commonv1pb.InvokeRequest( method="my_method", data=Any(value='CMSG_INVOKE_REQUEST'.encode('utf-8')), content_type="text/plain; charset=UTF-8")) logger.info(f"Invoking Service") res = client.InvokeService(req) logger.info(f"Invoked Service") # Print the response logger.info("================== RESPONSE ==================") logger.info(f"Content Type: {res.content_type}") logger.info(f"Message: {res.data.value}") channel.close()
from dapr.proto.common.v1 import common_pb2 from google.protobuf.any_pb2 import Any # Start a gRPC client port = os.getenv('DAPR_GRPC_PORT') channel = grpc.insecure_channel(f"localhost:{port}") client = dapr_services.DaprStub(channel) print(f"Started gRPC client on DAPR_GRPC_PORT: {port}") # Create a typed message with content type and body test_message = Any() test_message.Pack( commonv1pb.DataWithContentType(content_type="text/plain; charset=UTF-8", body='INVOKE_RECEIVED'.encode('utf-8'))) # Invoke the method 'my-method' on receiver req = dapr_messages.InvokeServiceRequest(id="invoke-receiver", message=common_pb2.InvokeRequest( method='my-method', data=test_message)) response = client.InvokeService(req) # Print the response if response.data.Is(commonv1pb.DataWithContentType.DESCRIPTOR): resp_data = commonv1pb.DataWithContentType() response.data.Unpack(resp_data) print(resp_data.content_type) print(resp_data.body) channel.close()