Skip to content

Commit

Permalink
feat(gengapic): add x-goog-api-version header (#1498)
Browse files Browse the repository at this point in the history
  • Loading branch information
quartzmo committed Apr 29, 2024
1 parent e78d789 commit 9e22b2c
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 13 deletions.
6 changes: 6 additions & 0 deletions internal/gengapic/client_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,10 @@ func TestClientInit(t *testing.T) {
Options: &descriptorpb.MethodOptions{},
},
},
Options: &descriptorpb.ServiceOptions{},
}
proto.SetExtension(servPlain.Options, annotations.E_ApiVersion, "v1_20240425")

servLRO := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("Foo"),
Method: []*descriptorpb.MethodDescriptorProto{
Expand All @@ -341,7 +344,10 @@ func TestClientInit(t *testing.T) {
Options: &descriptorpb.MethodOptions{},
},
},
Options: &descriptorpb.ServiceOptions{},
}
proto.SetExtension(servLRO.Options, annotations.E_ApiVersion, "v1_20240425")

servDeprecated := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("Foo"),
Method: []*descriptorpb.MethodDescriptorProto{
Expand Down
9 changes: 8 additions & 1 deletion internal/gengapic/gengrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,21 @@ func (g *generator) grpcClientUtilities(serv *descriptorpb.ServiceDescriptorProt
p("}")
p("")

apiVersion := proto.GetExtension(serv.Options, annotations.E_ApiVersion).(string)

// setGoogleClientInfo method
p("// setGoogleClientInfo sets the name and version of the application in")
p("// the `x-goog-api-client` header passed on each request. Intended for")
p("// use by Google-written clients.")
p("func (c *%s) setGoogleClientInfo(keyval ...string) {", lowcaseServName)
p(` kv := append([]string{"gl-go", gax.GoVersion}, keyval...)`)
p(` kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)`)
p(` c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}`)
p(` c.xGoogHeaders = []string{`)
p(` "x-goog-api-client", gax.XGoogHeader(kv...),`)
if apiVersion != "" {
p(` "x-goog-api-version", %q,`, apiVersion)
}
p(" }")
p("}")
p("")

Expand Down
9 changes: 8 additions & 1 deletion internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,21 @@ func (g *generator) restClientUtilities(serv *descriptorpb.ServiceDescriptorProt

g.restClientOptions(serv, servName)

apiVersion := proto.GetExtension(serv.Options, annotations.E_ApiVersion).(string)

// setGoogleClientInfo method
p("// setGoogleClientInfo sets the name and version of the application in")
p("// the `x-goog-api-client` header passed on each request. Intended for")
p("// use by Google-written clients.")
p("func (c *%s) setGoogleClientInfo(keyval ...string) {", lowcaseServName)
p(` kv := append([]string{"gl-go", gax.GoVersion}, keyval...)`)
p(` kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")`)
p(` c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}`)
p(` c.xGoogHeaders = []string{`)
p(` "x-goog-api-client", gax.XGoogHeader(kv...),`)
if apiVersion != "" {
p(` "x-goog-api-version", %q,`, apiVersion)
}
p(" }")
p("}")
p("")

Expand Down
4 changes: 3 additions & 1 deletion internal/gengapic/testdata/custom_op_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, e
func (c *restClient) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
c.xGoogHeaders = []string{
"x-goog-api-client", gax.XGoogHeader(kv...),
}
}

// Close closes the connection to the API service. The user should invoke this when
Expand Down
8 changes: 6 additions & 2 deletions internal/gengapic/testdata/deprecated_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ func (c *gRPCClient) Connection() *grpc.ClientConn {
func (c *gRPCClient) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
c.xGoogHeaders = []string{
"x-goog-api-client", gax.XGoogHeader(kv...),
}
}

// Close closes the connection to the API service. The user should invoke this when
Expand Down Expand Up @@ -168,7 +170,9 @@ func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, e
func (c *restClient) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
c.xGoogHeaders = []string{
"x-goog-api-client", gax.XGoogHeader(kv...),
}
}

// Close closes the connection to the API service. The user should invoke this when
Expand Down
10 changes: 8 additions & 2 deletions internal/gengapic/testdata/empty_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ func (c *gRPCClient) Connection() *grpc.ClientConn {
func (c *gRPCClient) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
c.xGoogHeaders = []string{
"x-goog-api-client", gax.XGoogHeader(kv...),
"x-goog-api-version", "v1_20240425",
}
}

// Close closes the connection to the API service. The user should invoke this when
Expand Down Expand Up @@ -162,7 +165,10 @@ func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, e
func (c *restClient) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
c.xGoogHeaders = []string{
"x-goog-api-client", gax.XGoogHeader(kv...),
"x-goog-api-version", "v1_20240425",
}
}

// Close closes the connection to the API service. The user should invoke this when
Expand Down
5 changes: 4 additions & 1 deletion internal/gengapic/testdata/foo_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ func (c *fooGRPCClient) Connection() *grpc.ClientConn {
func (c *fooGRPCClient) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
c.xGoogHeaders = []string{
"x-goog-api-client", gax.XGoogHeader(kv...),
"x-goog-api-version", "v1_20240425",
}
}

// Close closes the connection to the API service. The user should invoke this when
Expand Down
5 changes: 4 additions & 1 deletion internal/gengapic/testdata/foo_rest_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ func NewFooRESTClient(ctx context.Context, opts ...option.ClientOption) (*FooCli
func (c *fooRESTClient) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
c.xGoogHeaders = []string{
"x-goog-api-client", gax.XGoogHeader(kv...),
"x-goog-api-version", "v1_20240425",
}
}

// Close closes the connection to the API service. The user should invoke this when
Expand Down
5 changes: 4 additions & 1 deletion internal/gengapic/testdata/lro_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ func (c *fooGRPCClient) Connection() *grpc.ClientConn {
func (c *fooGRPCClient) setGoogleClientInfo(keyval ...string) {
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
c.xGoogHeaders = []string{
"x-goog-api-client", gax.XGoogHeader(kv...),
"x-goog-api-version", "v1_20240425",
}
}

// Close closes the connection to the API service. The user should invoke this when
Expand Down
25 changes: 22 additions & 3 deletions showcase/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ func TestEchoHeader(t *testing.T) {
}
}

// Test gRPC trailers for required headers.
func TestEchoTrailers(t *testing.T) {
defer check(t)
req := &showcasepb.EchoRequest{Header: "potato"}
mdForTrailers := metadata.New(map[string]string{})
echo.Echo(context.Background(), req, gax.WithGRPCOptions(grpc.Trailer(&mdForTrailers)))
apiVersion := mdForTrailers.Get("x-goog-api-version")
if len(apiVersion) < 1 || apiVersion[0] == "" {
t.Error("Echo() x-goog-api-version header not present or empty")
}
}

func TestEchoHeaderREST(t *testing.T) {
defer check(t)
for _, tst := range []struct {
Expand Down Expand Up @@ -199,15 +211,22 @@ func TestXGoogeHeaders(t *testing.T) {
info := y.FieldByName("xGoogHeaders")

var goVersion string
var apiVersion string
vals := make([]string, 0)
for i := 0; i < info.Len(); i++ {
key := info.Index(i)
// Only check for the client info set by the generated setGoogleClientInfo()
if key.String() != "x-goog-api-client" {
continue
if key.String() == "x-goog-api-client" {
vals = append(vals, info.Index(i+1).String())
}
if key.String() == "x-goog-api-version" {
apiVersion = info.Index(i + 1).String()
}

}

vals = append(vals, info.Index(i+1).String())
if apiVersion == "" {
t.Error("expected x-goog-api-version to be present and non-empty")
}

for i := 0; goVersion == "" || i < len(vals); i++ {
Expand Down

0 comments on commit 9e22b2c

Please sign in to comment.