diff --git a/api.go b/api.go index 64d2008..8b74f59 100644 --- a/api.go +++ b/api.go @@ -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 { @@ -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 { diff --git a/customerio.go b/customerio.go index 276ca4c..db542ad 100644 --- a/customerio.go +++ b/customerio.go @@ -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 @@ -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 { @@ -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 { diff --git a/options.go b/options.go index ffe9c55..9b15896 100644 --- a/options.go +++ b/options.go @@ -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 + }, + } +} diff --git a/options_test.go b/options_test.go index 58bf9b8..f1e1163 100644 --- a/options_test.go +++ b/options_test.go @@ -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) { @@ -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) + } } diff --git a/version.go b/version.go new file mode 100644 index 0000000..e122cd4 --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package customerio + +const Version = "3.2.0"