Promblem Formulation
Hello there, I am new to gRPC.
Recently I am trying to write a python client, which emulates gRPC connection with a remote server which has custom encryption and compression on it's request/response body.
The problem is that I don't know how to add post-processing on serialized body after reading the official python documentation.
Q1: Interceptors
In the official client written in C#, they made use of class called CallInvoker
which allow pre/post-processing during serialization.
I believe interceptors are the python equivalents. For example, in grpc.UnaryUnaryClientInterceptor
there is callback called intercept_unary_unary(continuation, client_call_details, request)
.
But the document never mentioned about what is request
supposed to be. And I looked into python examples in grpc repo, they only show how to manipulate metadata and field value before serialization, which is not what I need.
And it seems that interceptors can only intercept requests but not responses. I have no idea how to add custom callback on server responses before deserialization using this.
Q2: My Thoughts
grpc.Channel
has a series of abstract methods for gRPC calls (e.g., unary_unary
) which takes arguments request_serializer
and response_deserializer
.
Therefore I think maybe it's possible to override the grpc.Channel
class which add callback on these arguments, i.e.,
def encode(data):
pass # my serializer
def decode(data):
pass # my deserializer
class Channel(grpc.Channel):
def unary_unary(method, request_serializer, response_deserializer, _registered_method):
def request(data):
return encode(request_serializer(data))
def response(data):
return request_serializer(decode(data))
return super().unary_unary(method, request, response, _registered_method)
# ... and the same for other methods
But I have no idea if this will even work.
grpc._channel.Channel
, override those methods, then construct the class as official grpc APIs do by myself. Wish there was an easier way to do this.