Home > Enterprise >  How to translate a flask route with path parameters to gRPC service?
How to translate a flask route with path parameters to gRPC service?

Time:12-08

I'm a little confused on how a simple flask route can translate to a gRPC service reading the official gRPC introduction.

Suppose I have a flask route like

@app.route("/hello/<string:name>/")
def hello_name():
    return "PLACEHOLDER"

If I wanted to define that as a Service in gRPC how would that look? It does not take any request data but instead asks for the data directly in the path. Would it be defined directly as /hello/<string:name/? I couldn't seem to find docs on that.

CodePudding user response:

In Flask, these are called route params.

In gRPC transcoding (!) see below, you'll need to identify the variable name in the definition:

message HelloRequest {
    string name = 1;
}

rpc Hello(HelloRequest) returns (HelloResponse) {
    option (google.api.http) = {
        get: "/hello/{name}"
    };
}

In order to make an HTTP/1 e.g. GET request to an HTTP/2 gRPC service, you'll need to use gRPC HTTP Transcoding. This places a proxy between your e.g. Flask client and the gRPC service and converts the client's HTTP/1 e.g. GETs into HTTP/2 gRPC calls. It's non-trivial but it works.

See:

  • Related