forked from customerio/go-customerio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
customers.go
88 lines (83 loc) · 2.73 KB
/
customers.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"
"net/url"
)
// UpdateCustomer will add/update a customer and sets their attributes
// If not found, a customer will be created. If found, the attributes will be updated
// See: https://customer.io/docs/api/#operation/identify
// AKA: Identify()
// Only use "email" if the workspace is setup to use email instead of ID
func (c *Client) UpdateCustomer(customerIDOrEmail string, attributes map[string]interface{}) error {
if customerIDOrEmail == "" {
return ParamError{Param: "customerIDOrEmail"}
}
_, err := c.request(
http.MethodPut,
fmt.Sprintf("%s/api/v1/customers/%s", c.options.trackURL, url.PathEscape(customerIDOrEmail)),
attributes,
)
return err
}
// DeleteCustomer will remove a customer given their id or email
// If not found, a customer will be created. If found, the attributes will be updated
// See: https://customer.io/docs/api/#operation/delete
// Only use "email" if the workspace is setup to use email instead of ID
func (c *Client) DeleteCustomer(customerIDOrEmail string) error {
if customerIDOrEmail == "" {
return ParamError{Param: "customerIDOrEmail"}
}
_, err := c.request(
http.MethodDelete,
fmt.Sprintf("%s/api/v1/customers/%s", c.options.trackURL, url.PathEscape(customerIDOrEmail)),
nil,
)
return err
}
// UpdateDevice will add/update a customer's device
// If not found, a device will be created. If found, the attributes will be updated
// See: https://customer.io/docs/api/#operation/add_device
// Only use "email" if the workspace is setup to use email instead of ID
func (c *Client) UpdateDevice(customerIDOrEmail string, device *Device) error {
if customerIDOrEmail == "" {
return ParamError{Param: "customerIDOrEmail"}
}
if device == nil {
return ParamError{Param: "device"}
} else if device.ID == "" {
return ParamError{Param: "deviceID"}
} else if !acceptedPlatforms(device.Platform) {
return ParamError{Param: "devicePlatform"}
}
_, err := c.request(
http.MethodPut,
fmt.Sprintf("%s/api/v1/customers/%s/devices", c.options.trackURL, url.PathEscape(customerIDOrEmail)),
map[string]interface{}{
"device": device,
},
)
return err
}
// DeleteDevice will remove a customer's device
// See: https://customer.io/docs/api/#operation/delete_device
// Only use "email" if the workspace is setup to use email instead of ID
func (c *Client) DeleteDevice(customerIDOrEmail, deviceID string) error {
if customerIDOrEmail == "" {
return ParamError{Param: "customerIDOrEmail"}
}
if deviceID == "" {
return ParamError{Param: "deviceID"}
}
_, err := c.request(
http.MethodDelete,
fmt.Sprintf(
"%s/api/v1/customers/%s/devices/%s",
c.options.trackURL,
url.PathEscape(customerIDOrEmail),
url.PathEscape(deviceID),
),
nil,
)
return err
}