Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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")
test_data = GrpcAny()
test_data.Pack(fake_req)
resp = InvokeServiceResponse(data=test_data)
self.assertIsNotNone(resp.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)
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)
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 = api_service_v1.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 = api_v1.InvokeServiceRequest(
id="invoke-receiver",
message=common_v1.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()
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:`InvokeServiceResponse` object returned from callee
"""
req_data = InvokeServiceRequestData(data, content_type)
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())
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 = api_service_v1.DaprStub(channel)
print(f"Started gRPC client on DAPR_GRPC_PORT: {port}")
# Invoke the Receiver
req = api_v1.InvokeServiceRequest(
id="invoke-receiver",
message=common_v1.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()