Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gensupport): pass in headers via context #2052

Merged
merged 6 commits into from Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -8,7 +8,7 @@ require (
github.com/google/s2a-go v0.1.4
github.com/google/uuid v1.3.0
github.com/googleapis/enterprise-certificate-proxy v0.2.5
github.com/googleapis/gax-go/v2 v2.11.0
github.com/googleapis/gax-go/v2 v2.12.0
go.opencensus.io v0.24.0
golang.org/x/net v0.12.0
golang.org/x/oauth2 v0.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -61,8 +61,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.2.5 h1:UR4rDjcgpgEnqpIEvkiqTYKBCKLNmlge2eVjoZfySzM=
github.com/googleapis/enterprise-certificate-proxy v0.2.5/go.mod h1:RxW0N9901Cko1VOCW3SXCpWP+mlIEkk2tP7jnHy9a3w=
github.com/googleapis/gax-go/v2 v2.11.0 h1:9V9PWXEsWnPpQhu/PeQIkS4eGzMlTLGgt80cUUI8Ki4=
github.com/googleapis/gax-go/v2 v2.11.0/go.mod h1:DxmR61SGKkGLa2xigwuZIQpkCI2S5iydzRfb3peWZJI=
github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56etFpas=
github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
17 changes: 17 additions & 0 deletions internal/gensupport/send.go
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/google/uuid"
"github.com/googleapis/gax-go/v2"
"github.com/googleapis/gax-go/v2/callctx"
)

// Use this error type to return an error which allows introspection of both
Expand Down Expand Up @@ -43,6 +44,14 @@ func (e wrappedCallErr) Is(target error) bool {
// req.WithContext, then calls any functions returned by the hooks in
// reverse order.
func SendRequest(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
// Add headers set in context metadata.
headers := callctx.HeadersFromContext(ctx)
for k, vals := range headers {
for _, v := range vals {
req.Header.Add(k, v)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean by the nested loop...alternatively, you could do this:

for k, vals := range headers {
  req.Header.Set(k, append(r.Header.Get(k), vals...))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @noahdietz , I think I'll just stick with what I have since it's slightly clearer to look at to me if that's okay.

}

// Disallow Accept-Encoding because it interferes with the automatic gzip handling
// done by the default http.Transport. See https://github.com/google/google-api-go-client/issues/219.
if _, ok := req.Header["Accept-Encoding"]; ok {
Expand Down Expand Up @@ -77,6 +86,14 @@ func send(ctx context.Context, client *http.Client, req *http.Request) (*http.Re
// req.WithContext, then calls any functions returned by the hooks in
// reverse order.
func SendRequestWithRetry(ctx context.Context, client *http.Client, req *http.Request, retry *RetryConfig) (*http.Response, error) {
// Add headers set in context metadata.
headers := callctx.HeadersFromContext(ctx)
for k, vals := range headers {
for _, v := range vals {
req.Header.Add(k, v)
}
}

// Disallow Accept-Encoding because it interferes with the automatic gzip handling
// done by the default http.Transport. See https://github.com/google/google-api-go-client/issues/219.
if _, ok := req.Header["Accept-Encoding"]; ok {
Expand Down
36 changes: 36 additions & 0 deletions internal/gensupport/send_test.go
Expand Up @@ -7,6 +7,9 @@ package gensupport
import (
"context"
"errors"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gax-go/v2/callctx"
"net/http"
"testing"
)
Expand All @@ -31,6 +34,39 @@ func TestSendRequestWithRetry(t *testing.T) {
}
}

type headerRoundTripper struct {
wantHeader http.Header
}

func (rt *headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
// Ignore x-goog headers sent by SendRequestWithRetry
r.Header.Del("X-Goog-Api-Client")
r.Header.Del("X-Goog-Gcs-Idempotency-Token")
if diff := cmp.Diff(r.Header, rt.wantHeader); diff != "" {
return nil, fmt.Errorf("headers don't match: %v", diff)
}
return &http.Response{StatusCode: 200}, nil
}

// Ensure that headers set via the context are passed through to the request as expected.
func TestSendRequestHeader(t *testing.T) {
ctx := context.Background()
ctx = callctx.SetHeaders(ctx, "foo", "100", "bar", "200")
client := http.Client{
Transport: &headerRoundTripper{
wantHeader: map[string][]string{"Foo": {"100"}, "Bar": {"200"}},
},
}
req, _ := http.NewRequest("GET", "url", nil)
if _, err := SendRequest(ctx, &client, req); err != nil {
t.Errorf("SendRequest: %v", err)
}
req2, _ := http.NewRequest("GET", "url", nil)
if _, err := SendRequestWithRetry(ctx, &client, req2, nil); err != nil {
t.Errorf("SendRequest: %v", err)
}
}

type brokenRoundTripper struct{}

func (t *brokenRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
Expand Down