-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for `WithRegion` and the new `RegionEU` flag
- Loading branch information
Showing
7 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |