Skip to content

Commit

Permalink
feat(gengapic): use gax.BuildHeaders and gax.InsertMetadataIntoOutgoi…
Browse files Browse the repository at this point in the history
…ngContext (#1368)

closes: #1300
closes: #1301
  • Loading branch information
quartzmo committed Jul 12, 2023
1 parent 9201cdc commit 6f782f9
Show file tree
Hide file tree
Showing 38 changed files with 105 additions and 227 deletions.
12 changes: 3 additions & 9 deletions internal/gengapic/client_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ func TestClientInit(t *testing.T) {
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/grpc/metadata"}: true,
{Path: "google.golang.org/api/option"}: true,
{Name: "gtransport", Path: "google.golang.org/api/transport/grpc"}: true,
{Name: "iampb", Path: "cloud.google.com/go/iam/apiv1/iampb"}: true,
Expand All @@ -426,7 +425,6 @@ func TestClientInit(t *testing.T) {
{Path: "google.golang.org/api/option"}: true,
{Path: "google.golang.org/api/option/internaloption"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/grpc/metadata"}: true,
{Path: "net/http"}: true,
{Name: "httptransport", Path: "google.golang.org/api/transport/http"}: true,
},
Expand All @@ -443,7 +441,6 @@ func TestClientInit(t *testing.T) {
{Path: "net/http"}: true,
{Path: "context"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/grpc/metadata"}: true,
{Name: "gtransport", Path: "google.golang.org/api/transport/grpc"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Name: "httptransport", Path: "google.golang.org/api/transport/http"}: true,
Expand All @@ -463,10 +460,9 @@ func TestClientInit(t *testing.T) {
{Name: "longrunningpb", Path: "cloud.google.com/go/longrunning/autogen/longrunningpb"}: true,
{Name: "lroauto", Path: "cloud.google.com/go/longrunning/autogen"}: true,
{Name: "mypackagepb", Path: "github.com/googleapis/mypackage"}: true,
{Path: "context"}: true,
{Path: "google.golang.org/api/option"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/grpc/metadata"}: true,
{Path: "context"}: true,
{Path: "google.golang.org/api/option"}: true,
{Path: "google.golang.org/grpc"}: true,
},
wantNumSnps: 6,
},
Expand All @@ -483,7 +479,6 @@ func TestClientInit(t *testing.T) {
{Path: "google.golang.org/api/option"}: true,
{Path: "google.golang.org/api/option/internaloption"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/grpc/metadata"}: true,
{Path: "net/http"}: true,
},
wantNumSnps: 1,
Expand All @@ -499,7 +494,6 @@ func TestClientInit(t *testing.T) {
{Path: "google.golang.org/api/option"}: true,
{Path: "google.golang.org/api/option/internaloption"}: true,
{Path: "google.golang.org/grpc"}: true,
{Path: "google.golang.org/grpc/metadata"}: true,
{Path: "net/http"}: true,
{Name: "httptransport", Path: "google.golang.org/api/transport/http"}: true,
},
Expand Down
31 changes: 0 additions & 31 deletions internal/gengapic/doc_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
// it does not use g.commit().
func (g *generator) genDocFile(year int, scopes []string, serv *descriptor.ServiceDescriptorProto) {
p := g.printf
hasREST := containsTransport(g.opts.transports, rest)

p(license.Apache, year)
p("")
Expand Down Expand Up @@ -119,12 +118,8 @@ func (g *generator) genDocFile(year int, scopes []string, serv *descriptor.Servi

p("import (")
p("%s%q", "\t", "context")
if hasREST {
p("%s%q", "\t", "net/http")
}
p("")
p("%s%q", "\t", "google.golang.org/api/option")
p("%s%q", "\t", "google.golang.org/grpc/metadata")
p(")")
p("")

Expand All @@ -144,18 +139,6 @@ func (g *generator) genDocFile(year int, scopes []string, serv *descriptor.Servi
p("}")
p("")

p("func insertMetadata(ctx context.Context, mds ...metadata.MD) context.Context {")
p(" out, _ := metadata.FromOutgoingContext(ctx)")
p(" out = out.Copy()")
p(" for _, md := range mds {")
p(" for k, v := range md {")
p(" out[k] = append(out[k], v...)")
p(" }")
p(" }")
p(" return metadata.NewOutgoingContext(ctx, out)")
p("}")
p("")

p("// DefaultAuthScopes reports the default set of authentication scopes to use with this package.")
p("func DefaultAuthScopes() []string {")
p(" return []string{")
Expand All @@ -164,20 +147,6 @@ func (g *generator) genDocFile(year int, scopes []string, serv *descriptor.Servi
}
p(" }")
p("}")

if hasREST {
// buildHeaders from context and other metadata helper.
p("// buildHeaders extracts metadata from the outgoing context, joins it with any other")
p("// given metadata, and converts them into a http.Header. ")
p("func buildHeaders(ctx context.Context, mds ...metadata.MD) http.Header {")
p(" if cmd, ok := metadata.FromOutgoingContext(ctx); ok {")
p(" mds = append(mds, cmd)")
p(" }")
p(" md := metadata.Join(mds...)")
p(" return http.Header(md)")
p("}")
p("")
}
}

func collectScopes(servs []*descriptor.ServiceDescriptorProto) ([]string, error) {
Expand Down
16 changes: 10 additions & 6 deletions internal/gengapic/gengapic.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (g *generator) insertImplicitRequestHeaders(m *descriptor.MethodDescriptorP
// Trim the trailing comma and ampersand symbols.
f := formats.String()[:formats.Len()-1]
v := values.String()[:values.Len()-1]
g.printf("md := metadata.Pairs(\"x-goog-request-params\", fmt.Sprintf(%q,%s))", f, v)
g.printf("hds := []string{\"x-goog-request-params\", fmt.Sprintf(%q,%s)}", f, v)
return nil
}

Expand Down Expand Up @@ -355,7 +355,7 @@ func (g *generator) insertDynamicRequestHeaders(m *descriptor.MethodDescriptorPr
g.printf("}")
g.printf(`routingHeaders = strings.TrimSuffix(routingHeaders, "&")`)
g.imports[pbinfo.ImportSpec{Path: "strings"}] = true
g.printf("md := metadata.Pairs(\"x-goog-request-params\", routingHeaders)")
g.printf(`hds := []string{"x-goog-request-params", routingHeaders}`)
g.imports[pbinfo.ImportSpec{Path: "regexp"}] = true
return nil
}
Expand All @@ -380,9 +380,12 @@ func (g *generator) insertRequestHeaders(m *descriptor.MethodDescriptorProto, t
p("")
switch t {
case grpc:
p("ctx = insertMetadata(ctx, c.xGoogMetadata, md)")
p("hds = append(c.xGoogHeaders, hds...)")
p("ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)")
case rest:
p(`headers := buildHeaders(ctx, c.xGoogMetadata, md, metadata.Pairs("Content-Type", "application/json"))`)
p(`hds = append(c.xGoogHeaders, hds...)`)
p(`hds = append(hds, "Content-Type", "application/json")`)
p(`headers := gax.BuildHeaders(ctx, hds...)`)
}
g.imports[pbinfo.ImportSpec{Path: "fmt"}] = true
g.imports[pbinfo.ImportSpec{Path: "net/url"}] = true
Expand All @@ -391,9 +394,10 @@ func (g *generator) insertRequestHeaders(m *descriptor.MethodDescriptorProto, t
// No request-based header parameters.
switch t {
case grpc:
p("ctx = insertMetadata(ctx, c.xGoogMetadata)")
p("ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)")
case rest:
p(`headers := buildHeaders(ctx, c.xGoogMetadata, metadata.Pairs("Content-Type", "application/json"))`)
p(`hds := append(c.xGoogHeaders, "Content-Type", "application/json")`)
p(`headers := gax.BuildHeaders(ctx, hds...)`)
}
}

Expand Down
5 changes: 2 additions & 3 deletions internal/gengapic/gengrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,12 @@ func (g *generator) grpcClientInit(serv *descriptor.ServiceDescriptorProto, serv
g.mixinStubs()

p("// The x-goog-* metadata to be sent with each request.")
p("xGoogMetadata metadata.MD")
p("xGoogHeaders []string")

p("}")
p("")

g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc/metadata"}] = true
g.imports[imp] = true

g.grpcClientUtilities(serv, servName, imp, hasRPCForLRO)
Expand Down Expand Up @@ -378,7 +377,7 @@ func (g *generator) grpcClientUtilities(serv *descriptor.ServiceDescriptorProto,
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.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))`)
p(` c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}`)
p("}")
p("")

Expand Down
11 changes: 5 additions & 6 deletions internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func (g *generator) restClientInit(serv *descriptor.ServiceDescriptorProto, serv
p("operationClient *%sClient", opServName)
p("")
}
p(" // The x-goog-* metadata to be sent with each request.")
p(" xGoogMetadata metadata.MD")
p(" // The x-goog-* headers to be sent with each request.")
p(" xGoogHeaders []string")
p("")
p(" // Points back to the CallOptions field of the containing %sClient", servName)
p(" CallOptions **%sCallOptions", servName)
Expand All @@ -97,7 +97,6 @@ func (g *generator) restClientInit(serv *descriptor.ServiceDescriptorProto, serv
g.restClientUtilities(serv, servName, imp, hasRPCForLRO)

g.imports[pbinfo.ImportSpec{Path: "net/http"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc/metadata"}] = true
g.imports[pbinfo.ImportSpec{Name: "httptransport", Path: "google.golang.org/api/transport/http"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/option/internaloption"}] = true
}
Expand Down Expand Up @@ -208,7 +207,7 @@ func (g *generator) restClientUtilities(serv *descriptor.ServiceDescriptorProto,
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.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))`)
p(` c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}`)
p("}")
p("")

Expand Down Expand Up @@ -825,7 +824,8 @@ func (g *generator) pagingRESTCall(servName string, m *descriptor.MethodDescript
g.generateBaseURL(info, `return nil, "", err`)
g.generateQueryString(m)
p(" // Build HTTP headers from client and context metadata.")
p(` headers := buildHeaders(ctx, c.xGoogMetadata, metadata.Pairs("Content-Type", "application/json"))`)
p(` hds := append(c.xGoogHeaders, "Content-Type", "application/json")`)
p(` headers := gax.BuildHeaders(ctx, hds...)`)
p(" e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {")
p(` if settings.Path != "" {`)
p(" baseUrl.Path = settings.Path")
Expand Down Expand Up @@ -997,7 +997,6 @@ func (g *generator) lroRESTCall(servName string, m *descriptor.MethodDescriptorP
g.imports[pbinfo.ImportSpec{Path: "io"}] = true
g.imports[pbinfo.ImportSpec{Path: "cloud.google.com/go/longrunning"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/googleapi"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc/metadata"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/protobuf/encoding/protojson"}] = true

return nil
Expand Down
1 change: 0 additions & 1 deletion internal/gengapic/genrest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,6 @@ func TestGenRestMethod(t *testing.T) {
{Path: "cloud.google.com/go/longrunning"}: true,
{Path: "fmt"}: true,
{Path: "google.golang.org/api/googleapi"}: true,
{Path: "google.golang.org/grpc/metadata"}: true,
{Path: "google.golang.org/protobuf/encoding/protojson"}: true,
{Path: "io"}: true,
{Path: "net/url"}: true,
Expand Down
6 changes: 3 additions & 3 deletions internal/gengapic/testdata/custom_op_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type restClient struct {
// operationClient is used to call the operation-specific management service.
operationClient *FooOperationClient

// The x-goog-* metadata to be sent with each request.
xGoogMetadata metadata.MD
// The x-goog-* headers to be sent with each request.
xGoogHeaders []string

// Points back to the CallOptions field of the containing Client
CallOptions **CallOptions
Expand Down Expand Up @@ -102,7 +102,7 @@ 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.xGoogMetadata = metadata.Pairs("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: 5 additions & 5 deletions internal/gengapic/testdata/deprecated_client_init.want
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type gRPCClient struct {
client mypackagepb.FooClient

// The x-goog-* metadata to be sent with each request.
xGoogMetadata metadata.MD
xGoogHeaders []string
}

// NewClient creates a new foo client based on gRPC.
Expand Down Expand Up @@ -115,7 +115,7 @@ 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.xGoogMetadata = metadata.Pairs("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 All @@ -132,8 +132,8 @@ type restClient struct {
// The http client.
httpClient *http.Client

// The x-goog-* metadata to be sent with each request.
xGoogMetadata metadata.MD
// The x-goog-* headers to be sent with each request.
xGoogHeaders []string

// Points back to the CallOptions field of the containing Client
CallOptions **CallOptions
Expand Down Expand Up @@ -168,7 +168,7 @@ 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.xGoogMetadata = metadata.Pairs("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
23 changes: 0 additions & 23 deletions internal/gengapic/testdata/doc_file.want
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ package awesome // import "path/to/awesome"

import (
"context"
"net/http"

"google.golang.org/api/option"
"google.golang.org/grpc/metadata"
)

// For more information on implementing a client constructor hook, see
Expand All @@ -108,31 +106,10 @@ func getVersionClient() string {
return versionClient
}

func insertMetadata(ctx context.Context, mds ...metadata.MD) context.Context {
out, _ := metadata.FromOutgoingContext(ctx)
out = out.Copy()
for _, md := range mds {
for k, v := range md {
out[k] = append(out[k], v...)
}
}
return metadata.NewOutgoingContext(ctx, out)
}

// DefaultAuthScopes reports the default set of authentication scopes to use with this package.
func DefaultAuthScopes() []string {
return []string{
"https://foo.bar.com/auth",
"https://zip.zap.com/auth",
}
}
// buildHeaders extracts metadata from the outgoing context, joins it with any other
// given metadata, and converts them into a http.Header.
func buildHeaders(ctx context.Context, mds ...metadata.MD) http.Header {
if cmd, ok := metadata.FromOutgoingContext(ctx); ok {
mds = append(mds, cmd)
}
md := metadata.Join(mds...)
return http.Header(md)
}

23 changes: 0 additions & 23 deletions internal/gengapic/testdata/doc_file_alpha.want
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,8 @@ package awesome // import "path/to/awesome"

import (
"context"
"net/http"

"google.golang.org/api/option"
"google.golang.org/grpc/metadata"
)

// For more information on implementing a client constructor hook, see
Expand All @@ -110,31 +108,10 @@ func getVersionClient() string {
return versionClient
}

func insertMetadata(ctx context.Context, mds ...metadata.MD) context.Context {
out, _ := metadata.FromOutgoingContext(ctx)
out = out.Copy()
for _, md := range mds {
for k, v := range md {
out[k] = append(out[k], v...)
}
}
return metadata.NewOutgoingContext(ctx, out)
}

// DefaultAuthScopes reports the default set of authentication scopes to use with this package.
func DefaultAuthScopes() []string {
return []string{
"https://foo.bar.com/auth",
"https://zip.zap.com/auth",
}
}
// buildHeaders extracts metadata from the outgoing context, joins it with any other
// given metadata, and converts them into a http.Header.
func buildHeaders(ctx context.Context, mds ...metadata.MD) http.Header {
if cmd, ok := metadata.FromOutgoingContext(ctx); ok {
mds = append(mds, cmd)
}
md := metadata.Join(mds...)
return http.Header(md)
}

Loading

0 comments on commit 6f782f9

Please sign in to comment.