I'm trying to wire two Python apps using grpcio 1.0.1. In the scenario that I'm going to describe below, the client asks for a determined book and the server wants to communicate that the book has not ben found. What would be the current way to do that with the following proto?
}
message Book {
string id = 1;
}
message BookGetRequest {
string id = 1;
}
message BookGetResponse {
Book book = 1;
}
I've tried the following snippet but it won't work because I can't return `None` or the channel will fail to serialize the response:
class LibraryServer(object):
def BookGet(self, request, context):
""" Book not found. """
context.set_code(grpc.StatusCode.NOT_FOUND)
If I return an empty message *and* set the status code to NOT_FOUND then I see an internal exception (message = "_Rendezvous of RPC that terminated with (StatusCode.NOT_FOUND, )") is raised on the client side:
class LibraryServer(object):
def BookGet(self, request, context):
""" Book not found. """
context.set_code(grpc.StatusCode.NOT_FOUND)
I've also attempted these two approaches:
class LibraryServer(object):
def BookGet(self, request, context):
""" Book not found. """
return library_mcp2.BookGetResponse()
class LibraryServer(object):
def BookGet(self, request, context):
""" Book not found. """
return library_mcp2.BookGetResponse(book=None)
However, this is what I am seeing on the client:
resp = self.stub.BookGet(library_pb2.BookGetRequest(id='12345'))
resp is None # False
resp.book is None # False
So I could only make use of the last assertion but that seems far from ideal, right? What am I doing wrong?
Thank you for your help!
--
Jesús García Crespo