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

Set User-Agent header on both REST and graphql requests #259

Merged
merged 1 commit into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions buildkite/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package buildkite

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand All @@ -11,7 +10,6 @@ import (

genqlient "github.com/Khan/genqlient/graphql"
"github.com/shurcooL/graphql"
"golang.org/x/oauth2"
)

// Client can be used to interact with the Buildkite API
Expand All @@ -21,7 +19,6 @@ type Client struct {
http *http.Client
organization string
restUrl string
userAgent string
}

type clientConfig struct {
Expand All @@ -32,21 +29,54 @@ type clientConfig struct {
userAgent string
}

type headerRoundTripper struct {
next http.RoundTripper
Header http.Header
}

// NewClient creates a client to use for interacting with the Buildkite API
func NewClient(config *clientConfig) *Client {
token := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: config.apiToken})
httpClient := oauth2.NewClient(context.Background(), token)

// Setup a HTTP Client that can be used by all REST and graphql API calls,
// with suitable headers for authentication and user agent identification
rt := http.DefaultTransport
header := make(http.Header)
header.Set("Authorization", "Bearer "+config.apiToken)
header.Set("User-Agent", config.userAgent)
rt = newHeaderRoundTripper(rt, header)

httpClient := &http.Client{
Transport: rt,
}

return &Client{
graphql: graphql.NewClient(config.graphqlURL, httpClient),
genqlient: genqlient.NewClient(config.graphqlURL, httpClient),
http: httpClient,
organization: config.org,
restUrl: config.restURL,
userAgent: config.userAgent,
}
}

func newHeaderRoundTripper(next http.RoundTripper, header http.Header) *headerRoundTripper {
if next == nil {
next = http.DefaultTransport
}
return &headerRoundTripper{
next: next,
Header: header,
}
}

func (rt *headerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if rt.Header != nil {
for k, v := range rt.Header {
req.Header[k] = v
}
}
return rt.next.RoundTrip(req)
Copy link
Contributor

Choose a reason for hiding this comment

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

RoundTrip returns an error, so normally we'd want to wrap it to provide additional context.

Copy link
Contributor Author

@yob yob Apr 20, 2023

Choose a reason for hiding this comment

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

I've addressed the other feedback, but left this one. I could wrap the error, but... I really wasn't sure what to add that was meaningful.

}

func (client *Client) makeRequest(method string, path string, postData interface{}, responseObject interface{}) error {
var bodyBytes io.Reader
if postData != nil {
Expand All @@ -64,8 +94,6 @@ func (client *Client) makeRequest(method string, path string, postData interface
return fmt.Errorf("failed to create request: %w", err)
}

req.Header.Add("User-Agent", client.userAgent)

resp, err := client.http.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/Khan/genqlient v0.4.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.3
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f
golang.org/x/oauth2 v0.7.0
)

require (
Expand All @@ -17,6 +16,7 @@ require (
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/klauspost/compress v1.15.11 // indirect
golang.org/x/oauth2 v0.7.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
)

Expand Down