Skip to content

Commit

Permalink
Added beta api, added MVP for collections
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed May 30, 2021
1 parent a86f877 commit 2adc038
Show file tree
Hide file tree
Showing 9 changed files with 508 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ View the generated [documentation](https://pkg.go.dev/github.com/mrz1836/go-cust
- [ ] **Beta API** (Activities)
- [ ] List activities
- [ ] **Beta API** (Collections)
- [ ] Create a collection
- [x] Create a collection
- [ ] List your collections
- [ ] Lookup a collection
- [ ] Delete a collection
- [ ] Update a collection
- [x] Update a collection
- [ ] Lookup collection contents
- [ ] Update the contents of a collection
- [ ] **Beta API** (Sender Identities)
Expand Down
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Client struct {
type clientOptions struct {
apiURL string // Regional API endpoint (URL)
appAPIKey string // App or Beta API key
betaURL string // Regional API endpoint (Beta URL)
httpTimeout time.Duration // Default timeout in seconds for GET requests
requestTracing bool // If enabled, it will trace the request timing
retryCount int // Default retry count for HTTP requests
Expand All @@ -36,17 +37,20 @@ type clientOptions struct {
// region is used for changing the location of the API endpoints
type region struct {
apiURL string
betaURL string
trackURL string
}

// Current regions available
var (
RegionUS = region{
apiURL: "https://api.customer.io",
betaURL: "https://beta-api.customer.io",
trackURL: "https://track.customer.io",
}
RegionEU = region{
apiURL: "https://api-eu.customer.io",
betaURL: "https://beta-api.customer.io",
trackURL: "https://track-eu.customer.io",
}
)
Expand All @@ -59,6 +63,7 @@ type ClientOps func(c *clientOptions)
func WithRegion(r region) ClientOps {
return func(c *clientOptions) {
c.apiURL = r.apiURL
c.betaURL = r.betaURL
c.trackURL = r.trackURL
}
}
Expand Down Expand Up @@ -131,6 +136,7 @@ func defaultClientOptions() (opts *clientOptions) {
// Set the default options
opts = &clientOptions{
apiURL: RegionUS.apiURL,
betaURL: RegionUS.betaURL,
httpTimeout: defaultHTTPTimeout,
requestTracing: false,
retryCount: defaultRetryCount,
Expand Down
8 changes: 6 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ func TestNewClient(t *testing.T) {
client, err := NewClient(WithTrackingKey(testSiteID, testTrackingAPIKey))
assert.NoError(t, err)
assert.NotNil(t, client)
assert.Equal(t, defaultUserAgent, client.options.userAgent)
assert.Equal(t, defaultHTTPTimeout, client.options.httpTimeout)
assert.Equal(t, defaultRetryCount, client.options.retryCount)
assert.Equal(t, defaultUserAgent, client.options.userAgent)
assert.Equal(t, false, client.options.requestTracing)
assert.Equal(t, RegionUS.apiURL, client.options.apiURL)
assert.Equal(t, RegionUS.betaURL, client.options.betaURL)
assert.Equal(t, RegionUS.trackURL, client.options.trackURL)
})

Expand Down Expand Up @@ -78,13 +79,15 @@ func TestNewClient(t *testing.T) {
client, err := NewClient(WithTrackingKey(testSiteID, testTrackingAPIKey), WithUserAgent("custom user agent"))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, "custom user agent", client.GetUserAgent())
})

t.Run("custom region (EU)", func(t *testing.T) {
client, err := NewClient(WithTrackingKey(testSiteID, testTrackingAPIKey), WithRegion(RegionEU))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, client.options.apiURL, "https://api-eu.customer.io")
assert.Equal(t, client.options.betaURL, "https://beta-api.customer.io")
assert.Equal(t, client.options.trackURL, "https://track-eu.customer.io")
})

Expand All @@ -93,6 +96,7 @@ func TestNewClient(t *testing.T) {
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, client.options.apiURL, "https://api.customer.io")
assert.Equal(t, client.options.betaURL, "https://beta-api.customer.io")
assert.Equal(t, client.options.trackURL, "https://track.customer.io")
})

Expand Down Expand Up @@ -136,7 +140,7 @@ func ExampleNewClient() {
return
}
fmt.Printf("loaded client: %s", client.options.userAgent)
// Output:loaded client: go-customerio: v1.3.2
// Output:loaded client: go-customerio: v1.3.3
}

// BenchmarkNewClient benchmarks the method NewClient()
Expand Down
88 changes: 88 additions & 0 deletions collections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package customerio

import (
"fmt"
"net/http"
)

// UpdateCollection will create or update a collection with raw data
// See: https://customer.io/docs/api/#operation/addCollection
// See: https://customer.io/docs/api/#operation/updateCollection
//
// The name of the collection. This is how you'll reference your collection in message.
// Updating the data or url for your collection fully replaces the contents of the collection.
// Data example: {"data":[{"property1":null,"property2":null}]}}
func (c *Client) UpdateCollection(collectionID, collectionName string, items []map[string]interface{}) error {
if collectionName == "" {
return ParamError{Param: "collectionName"}
}
/*
if len(data) > 56000 { // todo: is there a limit?
return errors.New("collection body size limited to 56000")
}
*/

// Create or Update (if id is given)
var err error
if len(collectionID) == 0 {
_, err = c.request(
http.MethodPost,
fmt.Sprintf("%s/v1/api/collections", c.options.betaURL),
map[string]interface{}{
"data": items,
"name": collectionName,
},
)
} else {
_, err = c.request(
http.MethodPut,
fmt.Sprintf("%s/v1/api/collections/%s", c.options.betaURL, collectionID),
map[string]interface{}{
"data": items,
"name": collectionName,
},
)
}

return err
}

// UpdateCollectionViaURL will create or update a collection using a URL to a JSON file
// See: https://customer.io/docs/api/#operation/addCollection
// See: https://customer.io/docs/api/#operation/updateCollection
//
// The name of the collection. This is how you'll reference your collection in message.
//
// The URL where your data is stored.
// If your URL does not include a Content-Type, Customer.io assumes your data is JSON.
// This URL can also be a google sheet that you've shared with cio_share@customer.io.
// Updating the data or url for your collection fully replaces the contents of the collection.
func (c *Client) UpdateCollectionViaURL(collectionID, collectionName string, jsonURL string) error {
if collectionName == "" {
return ParamError{Param: "collectionName"}
}

// Create or Update (if id is given)
var err error
if len(collectionID) == 0 {
_, err = c.request(
http.MethodPost,
fmt.Sprintf("%s/v1/api/collections", c.options.betaURL),
map[string]interface{}{
"url": jsonURL,
"name": collectionName,
},
)
} else {
_, err = c.request(
http.MethodPut,
fmt.Sprintf("%s/v1/api/collections/%s", c.options.betaURL, collectionID),
map[string]interface{}{
"url": jsonURL,
"name": collectionName,
},
)
}

return err
}
Loading

0 comments on commit 2adc038

Please sign in to comment.