Skip to content

Commit

Permalink
Set User-Agent header on both REST and graphql requests
Browse files Browse the repository at this point in the history
In #256 we starting setting a custom User-Agent header on REST API
calls, to help us understand the ways our API is used. It looks like this:

    Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.0.3 buildkite/0.15.0

This builds on that work and sets the same User-Agent on graphql API
requests as well.

It drops the unnescary use of golang.org/x/oauth2 to set a static bearer
token header, and uses the tripperware pattern [1] to set both headers we
care about:

1. the static bearer token for auth
2. the user agent

The same http.Client is used for both REST and graphql, so we'll get
identical behaviour on both APIs.

[1] https://dev.to/stevenacoffman/tripperwares-http-client-middleware-chaining-roundtrippers-3o00
  • Loading branch information
yob committed Apr 18, 2023
1 parent 0d41bd6 commit c9bf2d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
45 changes: 37 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,55 @@ 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
var rt http.RoundTripper
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) (resp *http.Response, err 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 {
Expand All @@ -64,8 +95,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

0 comments on commit c9bf2d5

Please sign in to comment.