0

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.

5
  • Interceptors are like middleware. They can also be added on the client-side (or server-side, too) to intercept a response. The gRPC docu compares them to middleware.
    – dan-kli
    Commented Jan 30 at 12:48
  • @dan-kli Yeah I see. Eventually I have to inherit an undocumented class 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.
    – imakak
    Commented Feb 1 at 17:23
  • I feel like writing a standard interceptor to encrypt / decrypt the data you are sending over the wire would solve your problem, but maybe I misunderstood it.
    – dan-kli
    Commented Feb 4 at 11:44
  • why not replicate the grpc request on http level rather than relying on the grpc module?
    – ahmed mani
    Commented Mar 1 at 6:08
  • @ahmedmani How could I send raw grpc requests on http level?
    – imakak
    Commented Mar 5 at 3:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.