diff --git a/buildkite/client.go b/buildkite/client.go index 7f53519d..667ddb74 100644 --- a/buildkite/client.go +++ b/buildkite/client.go @@ -2,7 +2,6 @@ package buildkite import ( "bytes" - "context" "encoding/json" "fmt" "io" @@ -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 @@ -21,7 +19,6 @@ type Client struct { http *http.Client organization string restUrl string - userAgent string } type clientConfig struct { @@ -32,10 +29,25 @@ 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), @@ -43,10 +55,28 @@ func NewClient(config *clientConfig) *Client { 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) +} + func (client *Client) makeRequest(method string, path string, postData interface{}, responseObject interface{}) error { var bodyBytes io.Reader if postData != nil { @@ -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) diff --git a/go.mod b/go.mod index bfb00f02..07c2767c 100644 --- a/go.mod +++ b/go.mod @@ -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 ( @@ -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 )