grpc-gateway HTTP client generator

  • By Akuity
  • Last update: Aug 14, 2023
  • Comments: 2

grpc-gateway-client

The grpc-gateway-client is a high quality REST client generator for gRPC services that are fronted by grpc-gateway.

Features

  • Strongly typed client interface.
  • Supports all gRPC features including streaming.
  • Supports all grpc-gateway features including custom query parameters, and request body.
  • Battle tested by Akuity's production services.

Usage

  1. Install grpc-gateway-client:

    $ go install github.com/akuity/grpc-gateway-client/protoc-gen-grpc-gateway-client@latest
  2. Add plugin in your buf.gen.yaml:

    version: v1
    managed:
    enabled: true
    
    plugins:
      - name: gateway-client
        path: protoc-gen-grpc-gateway-client
        out: pkg/api/gen
        opt:
          - paths=source_relative
  3. Generate client using buf generate and use it in your code:

     client := grpc_gateway_client_example.NewGreeterGatewayClient(gateway.NewClient(baseURL))
     resp, err := client.SayHello(context.Background(), &grpc_gateway_client_example.HelloRequest{Name: "World"})
     if err != nil {
         panic(err)
     }
     fmt.Println(resp.Message)

See example for a complete example.

Download

grpc-gateway-client.zip

Comments(2)

  • 1

    Add context.Context parameter for Client.NewRequest interface

    Proposal add a context.Context parameter for method NewRequest in interface Client


    current:

    NewRequest(method, url string) *resty.Request
    

    proposal:

    NewRequest(ctx context.Context, method, url string) *resty.Request
    

    So that, we can initialize the testy.Request with some information from the Context, such as injecting trace ID and JWT token in http header .

  • 2

    Compatibility with native gRPC client

    Proposal

    Generate REST client that implements native gRPC client interface.

    Motivation

    The generated API client is great. However, it is slightly different from the native gRPC client interface.

    • grpc-gateway client interface:
    // GreeterGatewayClient is the interface for Greeter service client.
    type GreeterGatewayClient interface {
    	// Sends a greeting
    	SayHello(context.Context, *HelloRequest) (*HelloReply, error)
    }
    
    • native grpc client
    type GreeterClient interface {
    	// Sends a greeting
    	SayHello(context.Context, *HelloRequest,  ...grpc.CallOption) (*HelloReply, error)
    }
    

    If grpc-gateway client uses the same interface as native gRPC then it can be used as a drop-in replacement, making adoption much easier.