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_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_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_non_protobuf_message(self):
with self.assertRaises(ValueError):
resp = InvokeServiceResponse(data="invalid_datatype")
self.assertIsNone(resp, 'This should not be reached.')
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_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_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)
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())
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(
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