forked from customerio/go-customerio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.go
88 lines (81 loc) · 2.54 KB
/
collections.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
}