Skip to content

Commit

Permalink
EU Datacenter Support
Browse files Browse the repository at this point in the history
Adds support for `WithRegion` and the new `RegionEU` flag
  • Loading branch information
hownowstephen authored Mar 24, 2021
2 parents af26f3d + 26c99a3 commit c0b083d
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 16 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## March 24, 2021
### Added
- Support for EU region
- Allow using custom `*http.Client`
### Removed
### Changed
- `customerio.NewAPIClient` and `customerio.NewTrackClient` have a new variadic parameter for options in order to choose US/EU region and/or customer HTTP client.

## December 3, 2020
### Added
- Support for transactional api
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ You'll be able to integrate **fully** with [Customer.io](http://customer.io) wit

### Setup

Create an instance of the client with your [customer.io](http://customer.io) credentials
which can be found on the [customer.io integration screen](https://manage.customer.io/integration).
Create an instance of the client with your [Customer.io credentials](https://fly.customer.io/settings/api_credentials).

```go
track := customerio.NewTrackClient("YOUR SITE ID", "YOUR API SECRET KEY")
track := customerio.NewTrackClient("YOUR SITE ID", "YOUR API SECRET KEY", customerio.WithRegion(customerio.RegionUS))
```

Your account region—`RegionUS` or `RegionEU`—is optional. If you do not specify your region, we assume that your account is based in the US (`RegionUS`). If your account is based in the EU and you do not provide the correct region, we'll route requests from the US to `RegionEU` accordingly, however this may cause data to be logged in the US.

### Identify logged in customers

Tracking data of logged in customers is a key part of [Customer.io](http://customer.io). In order to
Expand Down Expand Up @@ -194,7 +195,7 @@ You can also send attachments with your message. Use `Attach` to encode attachme
```go
import "github.com/customerio/go-customerio"

client := customerio.NewAPIClient("<extapikey>");
client := customerio.NewAPIClient("<extapikey>", customerio.WithRegion(customerio.RegionUS));

// TransactionalMessageId — the ID of the transactional message you want to send.
// To — the email address of your recipients.
Expand Down
11 changes: 8 additions & 3 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ type APIClient struct {

// 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) *APIClient {
return &APIClient{
func NewAPIClient(key string, opts ...option) *APIClient {
client := &APIClient{
Key: key,
Client: http.DefaultClient,
URL: "https://api.customer.io",
}

for _, opt := range opts {
opt.api(client)
}
return client
}

func (c *APIClient) doRequest(ctx context.Context, verb, requestPath string, body interface{}) ([]byte, int, error) {
Expand All @@ -30,7 +35,7 @@ func (c *APIClient) doRequest(ctx context.Context, verb, requestPath string, bod
return nil, 0, err
}

req, err := http.NewRequest("POST", c.URL+requestPath, bytes.NewBuffer(b))
req, err := http.NewRequest(verb, c.URL+requestPath, bytes.NewBuffer(b))
if err != nil {
return nil, 0, err
}
Expand Down
22 changes: 14 additions & 8 deletions customerio.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,30 @@ func (e ParamError) Error() string { return e.Param + ": missing" }

// NewTrackClient prepares a client for use with the Customer.io track API, see: https://customer.io/docs/api/#apitrackintroduction
// using a Tracking Site ID and API Key pair from https://fly.customer.io/settings/api_credentials
func NewTrackClient(siteID, apiKey string) *CustomerIO {
return NewCustomerIO(siteID, apiKey)
}

// NewCustomerIO prepares a client for use with the Customer.io track API, see: https://customer.io/docs/api/#apitrackintroduction
// deprecated in favour of NewTrackClient
func NewCustomerIO(siteID, apiKey string) *CustomerIO {
func NewTrackClient(siteID, apiKey string, opts ...option) *CustomerIO {
client := &http.Client{
Transport: &http.Transport{
MaxIdleConnsPerHost: 100,
},
}
return &CustomerIO{
c := &CustomerIO{
siteID: siteID,
apiKey: apiKey,
URL: "https://track.customer.io",
Client: client,
}

for _, opt := range opts {
opt.track(c)
}

return c
}

// NewCustomerIO prepares a client for use with the Customer.io track API, see: https://customer.io/docs/api/#apitrackintroduction
// deprecated in favour of NewTrackClient
func NewCustomerIO(siteID, apiKey string) *CustomerIO {
return NewTrackClient(siteID, apiKey)
}

// Identify identifies a customer and sets their attributes
Expand Down
2 changes: 1 addition & 1 deletion examples/transactional.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {

ctx := context.Background()

client := customerio.NewAPIClient("<your-key-here>")
client := customerio.NewAPIClient("<your-key-here>", customerio.WithRegion(customerio.RegionUS))

req := customerio.SendEmailRequest{
Identifiers: map[string]string{
Expand Down
46 changes: 46 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package customerio

import "net/http"

type option struct {
api func(*APIClient)
track func(*CustomerIO)
}

type region struct {
apiURL string
trackURL string
}

var (
RegionUS = region{
apiURL: "https://api.customer.io",
trackURL: "https://track.customer.io",
}
RegionEU = region{
apiURL: "https://api-eu.customer.io",
trackURL: "https://track-eu.customer.io",
}
)

func WithRegion(r region) option {
return option{
api: func(a *APIClient) {
a.URL = r.apiURL
},
track: func(c *CustomerIO) {
c.URL = r.trackURL
},
}
}

func WithHTTPClient(client *http.Client) option {
return option{
api: func(a *APIClient) {
a.Client = client
},
track: func(c *CustomerIO) {
c.Client = client
},
}
}
45 changes: 45 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package customerio

import (
"net/http"
"reflect"
"testing"
)

func TestAPIOptions(t *testing.T) {

client := NewAPIClient("mykey")
if client.URL != RegionUS.apiURL {
t.Errorf("wrong default url. got: %s, want: %s", client.URL, RegionUS.apiURL)
}

client = NewAPIClient("mykey", WithRegion(RegionEU))
if client.URL != RegionEU.apiURL {
t.Errorf("wrong url. got: %s, want: %s", client.URL, RegionEU.apiURL)
}

hc := &http.Client{}
client = NewAPIClient("mykey", WithHTTPClient(hc))
if !reflect.DeepEqual(client.Client, hc) {
t.Errorf("wrong http client. got: %#v, want: %#v", client.Client, hc)
}
}

func TestTrackOptions(t *testing.T) {

client := NewTrackClient("site_id", "api_key")
if client.URL != RegionUS.trackURL {
t.Errorf("wrong default url. got: %s, want: %s", client.URL, RegionUS.trackURL)
}

client = NewTrackClient("site_id", "api_key", WithRegion(RegionEU))
if client.URL != RegionEU.trackURL {
t.Errorf("wrong url. got: %s, want: %s", client.URL, RegionEU.trackURL)
}

hc := &http.Client{}
client = NewTrackClient("site_id", "api_key", WithHTTPClient(hc))
if !reflect.DeepEqual(client.Client, hc) {
t.Errorf("wrong http client. got: %#v, want: %#v", client.Client, hc)
}
}

0 comments on commit c0b083d

Please sign in to comment.