Skip to content

Commit

Permalink
Merge pull request #29 from customerio/custom_user_agent
Browse files Browse the repository at this point in the history
Adds a default user agent of "Customer.io Go Client" on requests made with the client and the option to set a custom user agent option.
  • Loading branch information
soris-codes authored Oct 4, 2021
2 parents 796eb94 + 04131c3 commit 2123497
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
15 changes: 9 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import (
)

type APIClient struct {
Key string
URL string
Client *http.Client
Key string
URL string
UserAgent string
Client *http.Client
}

// NewAPIClient prepares a client for use with the Customer.io API, see: https://customer.io/docs/api/#apicoreintroduction
// using an App API Key from https://fly.customer.io/settings/api_credentials?keyType=app
func NewAPIClient(key string, opts ...option) *APIClient {
client := &APIClient{
Key: key,
Client: http.DefaultClient,
URL: "https://api.customer.io",
Key: key,
Client: http.DefaultClient,
URL: "https://api.customer.io",
UserAgent: DefaultUserAgent,
}

for _, opt := range opts {
Expand All @@ -44,6 +46,7 @@ func (c *APIClient) doRequest(ctx context.Context, verb, requestPath string, bod

req.Header.Set("Authorization", "Bearer "+c.Key)
req.Header.Set("Content-Type", "application/json")
req.Header.Add("User-Agent", c.UserAgent)

resp, err := c.Client.Do(req)
if err != nil {
Expand Down
21 changes: 13 additions & 8 deletions customerio.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import (
"strings"
)

const DefaultUserAgent = "Customer.io Go Client/" + Version

// CustomerIO wraps the customer.io track API, see: https://customer.io/docs/api/#apitrackintroduction
type CustomerIO struct {
siteID string
apiKey string
URL string
Client *http.Client
siteID string
apiKey string
URL string
UserAgent string
Client *http.Client
}

// CustomerIOError is returned by any method that fails at the API level
Expand Down Expand Up @@ -48,10 +51,11 @@ func NewTrackClient(siteID, apiKey string, opts ...option) *CustomerIO {
},
}
c := &CustomerIO{
siteID: siteID,
apiKey: apiKey,
URL: "https://track.customer.io",
Client: client,
siteID: siteID,
apiKey: apiKey,
URL: "https://track.customer.io",
UserAgent: DefaultUserAgent,
Client: client,
}

for _, opt := range opts {
Expand Down Expand Up @@ -176,6 +180,7 @@ func (c *CustomerIO) request(method, url string, body interface{}) error {
return err
}

req.Header.Add("User-Agent", c.UserAgent)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Content-Length", strconv.Itoa(len(j)))
} else {
Expand Down
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ func WithHTTPClient(client *http.Client) option {
},
}
}

func WithUserAgent(ua string) option {
return option{
api: func(a *APIClient) {
a.UserAgent = ua
},
track: func(c *CustomerIO) {
c.UserAgent = ua
},
}
}
12 changes: 12 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func TestAPIOptions(t *testing.T) {
if !reflect.DeepEqual(client.Client, hc) {
t.Errorf("wrong http client. got: %#v, want: %#v", client.Client, hc)
}

customUserAgent := "Customer.io"
client = customerio.NewAPIClient("mykey", customerio.WithUserAgent(customUserAgent))
if client.UserAgent != customUserAgent {
t.Errorf("wrong user-agent. got: %s, want: %s", client.UserAgent, customUserAgent)
}
}

func TestTrackOptions(t *testing.T) {
Expand All @@ -44,4 +50,10 @@ func TestTrackOptions(t *testing.T) {
if !reflect.DeepEqual(client.Client, hc) {
t.Errorf("wrong http client. got: %#v, want: %#v", client.Client, hc)
}

customUserAgent := "Customer.io"
client = customerio.NewTrackClient("site_id", "api_key", customerio.WithUserAgent(customUserAgent))
if client.UserAgent != customUserAgent {
t.Errorf("wrong user-agent. got: %s, want: %s", client.UserAgent, customUserAgent)
}
}
3 changes: 3 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package customerio

const Version = "3.2.0"

0 comments on commit 2123497

Please sign in to comment.