Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 OnInvoke(self, request, context):
data=None
content_type=""
if request.method == 'my_method':
custom_response = response_messages.CustomResponse(isSuccess=True, code=200, message="Hello World - Success!")
data = Any()
data.Pack(custom_response)
else:
data = Any(value='METHOD_NOT_SUPPORTED'.encode('utf-8'))
content_type="text/plain"
print(data, flush=True)
print(content_type, flush=True)
return common_v1.InvokeResponse(data=data, content_type=content_type)
def OnInvoke(self, request, context):
data=None
content_type=""
if request.method == 'my-method':
data = Any(value='INVOKE_RECEIVED'.encode('utf-8'))
content_type = "text/plain; charset=UTF-8"
else:
data = Any(value='unsupported methods'.encode('utf-8'))
content_type = "text/plain; charset=UTF-8"
# Return response to caller
return common_v1.InvokeResponse(data=data, content_type=content_type)
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(
data=resp_data.proto, content_type=resp_data.content_type)
def OnInvoke(
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())