From 3b492b9c8f837c41675626d55d32e8a2ed02dfb9 Mon Sep 17 00:00:00 2001 From: Soris Cox Date: Wed, 29 Sep 2021 17:04:41 -1000 Subject: [PATCH 1/6] specifiy a user agent to identify ourselves --- api.go | 1 + customerio.go | 1 + 2 files changed, 2 insertions(+) diff --git a/api.go b/api.go index 64d2008..937fd14 100644 --- a/api.go +++ b/api.go @@ -44,6 +44,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", "Customer.io Go Client/3.0") resp, err := c.Client.Do(req) if err != nil { diff --git a/customerio.go b/customerio.go index 276ca4c..dd35d7a 100644 --- a/customerio.go +++ b/customerio.go @@ -176,6 +176,7 @@ func (c *CustomerIO) request(method, url string, body interface{}) error { return err } + req.Header.Add("User-Agent", "Customer.io Go Client/3.0") req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Length", strconv.Itoa(len(j))) } else { From c9e63283429453ed925b1099bc2ef8d9a8999dfb Mon Sep 17 00:00:00 2001 From: Soris Cox Date: Wed, 29 Sep 2021 17:18:10 -1000 Subject: [PATCH 2/6] add ability to override user agent --- api.go | 16 +++++++++------- customerio.go | 20 +++++++++++--------- options.go | 11 +++++++++++ options_test.go | 6 ++++++ 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/api.go b/api.go index 937fd14..e8862be 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: "Customer.io Go Client/3.0", } for _, opt := range opts { @@ -44,7 +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", "Customer.io Go Client/3.0") + 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 dd35d7a..552db35 100644 --- a/customerio.go +++ b/customerio.go @@ -15,10 +15,11 @@ import ( // 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 +49,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: "Customer.io Go Client/3.0", + Client: client, } for _, opt := range opts { @@ -176,7 +178,7 @@ func (c *CustomerIO) request(method, url string, body interface{}) error { return err } - req.Header.Add("User-Agent", "Customer.io Go Client/3.0") + 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..49efda0 100644 --- a/options.go +++ b/options.go @@ -44,3 +44,14 @@ func WithHTTPClient(client *http.Client) option { }, } } + +func WithCustomUserAgent(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..59899b8 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.WithCustomUserAgent(customUserAgent)) + if client.UserAgent != customUserAgent { + t.Errorf("wrong user-agent. got: %s, want: %s", client.UserAgent, customUserAgent) + } } func TestTrackOptions(t *testing.T) { From fe6f1fbd5f490127f6e23634050b6960b385ca39 Mon Sep 17 00:00:00 2001 From: Soris Cox Date: Wed, 29 Sep 2021 17:24:47 -1000 Subject: [PATCH 3/6] add test for track and shorten method name --- options.go | 2 +- options_test.go | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/options.go b/options.go index 49efda0..9b15896 100644 --- a/options.go +++ b/options.go @@ -45,7 +45,7 @@ func WithHTTPClient(client *http.Client) option { } } -func WithCustomUserAgent(ua string) option { +func WithUserAgent(ua string) option { return option{ api: func(a *APIClient) { a.UserAgent = ua diff --git a/options_test.go b/options_test.go index 59899b8..f1e1163 100644 --- a/options_test.go +++ b/options_test.go @@ -27,7 +27,7 @@ func TestAPIOptions(t *testing.T) { } customUserAgent := "Customer.io" - client = customerio.NewAPIClient("mykey", customerio.WithCustomUserAgent(customUserAgent)) + client = customerio.NewAPIClient("mykey", customerio.WithUserAgent(customUserAgent)) if client.UserAgent != customUserAgent { t.Errorf("wrong user-agent. got: %s, want: %s", client.UserAgent, customUserAgent) } @@ -50,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) + } } From 700d0510939cd92f6c5d8103c4fbc51793dce400 Mon Sep 17 00:00:00 2001 From: Soris Cox Date: Mon, 4 Oct 2021 09:45:18 -1000 Subject: [PATCH 4/6] add const variable for version to have 1 place to update version that will be used in DefaultUserAgent --- api.go | 2 +- customerio.go | 2 ++ version.go | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 version.go diff --git a/api.go b/api.go index e8862be..8b74f59 100644 --- a/api.go +++ b/api.go @@ -22,7 +22,7 @@ func NewAPIClient(key string, opts ...option) *APIClient { Key: key, Client: http.DefaultClient, URL: "https://api.customer.io", - UserAgent: "Customer.io Go Client/3.0", + UserAgent: DefaultUserAgent, } for _, opt := range opts { diff --git a/customerio.go b/customerio.go index 552db35..9c6dcde 100644 --- a/customerio.go +++ b/customerio.go @@ -13,6 +13,8 @@ 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 diff --git a/version.go b/version.go new file mode 100644 index 0000000..b230258 --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package customerio + +const Version = "3.1.0" From 96ad42758b4cadc5182b3e66e997f6ee3e93657c Mon Sep 17 00:00:00 2001 From: Soris Cox Date: Mon, 4 Oct 2021 10:31:41 -1000 Subject: [PATCH 5/6] fix: use DefaultUserAgent --- customerio.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/customerio.go b/customerio.go index 9c6dcde..db542ad 100644 --- a/customerio.go +++ b/customerio.go @@ -54,7 +54,7 @@ func NewTrackClient(siteID, apiKey string, opts ...option) *CustomerIO { siteID: siteID, apiKey: apiKey, URL: "https://track.customer.io", - UserAgent: "Customer.io Go Client/3.0", + UserAgent: DefaultUserAgent, Client: client, } From 04131c352ad3d72d117c5f062958b63a8a42214d Mon Sep 17 00:00:00 2001 From: Soris Cox Date: Mon, 4 Oct 2021 11:26:47 -1000 Subject: [PATCH 6/6] update version to 3.2 as it will be correct as of this release --- version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.go b/version.go index b230258..e122cd4 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package customerio -const Version = "3.1.0" +const Version = "3.2.0"