Can I define a grpc call with a null request or response?

前端 未结 3 1545
生来不讨喜
生来不讨喜 2020-12-12 21:17

Does the rpc syntax in proto3 allow null requests or responses?

e.g. I want the equivalent of the following:

rpc Logout;
rpc Status returns (Status);         


        
相关标签:
3条回答
  • 2020-12-12 21:56

    You also can use predefined:

    import "google/protobuf/empty.proto";
    package MyPackage;
    
    service MyService {
      rpc Check(google.protobuf.Empty) returns (google.protobuf.Empty) {}
    }
    
    0 讨论(0)
  • 2020-12-12 21:59

    you can also use another bool property inside the Reply structure. like this

    message Reply {
      string result = 1;
      bool found = 2;
    }
    

    so if you dont find the result or some error happened you can return from the service class this

    return new Reply()
    {
       Found = false
    };
    
    0 讨论(0)
  • 2020-12-12 22:01

    Kenton's comment below is sound advice:

    ... we as developers are really bad at guessing what we might want in the future. So I recommend being safe by always defining custom params and results types for every method, even if they are empty.


    Answering my own question:

    Looking through the default proto files, I came across Empty that is exactly like the Null type I suggested above :)

    excerpt from that file:

    // A generic empty message that you can re-use to avoid defining duplicated
    // empty messages in your APIs. A typical example is to use it as the request
    // or the response type of an API method. For instance:
    //
    //     service Foo {
    //       rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);
    //     }
    //
    
    message Empty {
    
    }
    
    0 讨论(0)
提交回复
热议问题