2 years ago

#72415

test-img

fredrik

How to determine if an argument was passed with a gRPC call or not?

Question: When parsing a gRPC call's request in my gRPC endpoint, how can I make use of a certain request.<attribute> only if it was explicitly passed with the gRPC call?


I have this in my .proto:

message Object {
    bool enabled = 1;
}

I have this as my endpoint:

class CRUDService(CRUDServicer):
    @staticmethod
    def Create(request: Object_pb2.CreateReq, context) -> Object_pb2.CreateResp:

        options = {}

        if request.enabled:
            options["enabled"] = request.enabled

        obj = create_obj(options)
        
        return Object_pb2.CreateResp(serialized_obj=serialize(obj))

However, the if-condition does not check if enabled was passed with the call or not. It turns out enabled is always accessible on the request object. And with the snippet above, I will make it impossible to set options = {"enabled": False}, which I want to be able to do.

So, when querying request.enabled, I will get one of:

  1. The default value, explicitly passed with the call
  2. The default value, not passed with the call
  3. The non-default value, explicitly passed with the call

To further illustrate the problem, imagine setting up an Update endpoint (for the U in CRUD). Then I only want to update with whatever request fields were actually passed with the gRPC call - and not just update with a default value of something which was not even passed with the call.

python

grpc-python

0 Answers

Your Answer

Accepted video resources