-
Notifications
You must be signed in to change notification settings - Fork 23
/
device.go
109 lines (93 loc) · 2.85 KB
/
device.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package customerio
import (
"context"
"fmt"
"net/url"
)
type deviceV1 struct {
ID string `json:"id"`
Platform string `json:"platform"`
LastUsed string `json:"last_used,omitempty"`
Attributes map[string]interface{} `json:"attributes"`
}
type deviceV2 struct {
ID string `json:"token"`
Platform string `json:"platform"`
LastUsed string `json:"last_used,omitempty"`
Attributes map[string]interface{} `json:"attributes"`
}
func newDeviceV1(deviceID, platform string, data map[string]interface{}) (*deviceV1, error) {
if deviceID == "" {
return nil, ParamError{Param: "deviceID"}
}
if platform == "" {
return nil, ParamError{Param: "platform"}
}
d := &deviceV1{
ID: deviceID,
Platform: platform,
}
if len(data) > 0 {
d.Attributes = make(map[string]interface{})
}
for k, v := range data {
if k == "last_used" {
d.LastUsed = fmt.Sprintf("%v", v)
continue
}
d.Attributes[k] = v
}
return d, nil
}
func NewDevice(deviceID, platform string, data map[string]interface{}) (*deviceV2, error) {
d, err := newDeviceV1(deviceID, platform, data)
if err != nil {
return nil, err
}
return &deviceV2{
ID: d.ID,
Platform: d.Platform,
Attributes: d.Attributes,
LastUsed: d.LastUsed,
}, nil
}
// Delete deletes a customer
func (c *CustomerIO) Delete(customerID string) error {
return c.DeleteCtx(context.Background(), customerID)
}
// AddDeviceCtx adds a device for a customer
func (c *CustomerIO) AddDeviceCtx(ctx context.Context, customerID string, deviceID string, platform string, data map[string]interface{}) error {
if customerID == "" {
return ParamError{Param: "customerID"}
}
d, err := newDeviceV1(deviceID, platform, data)
if err != nil {
return err
}
body := map[string]interface{}{
"device": d,
}
return c.request(ctx, "PUT",
fmt.Sprintf("%s/api/v1/customers/%s/devices", c.URL, url.PathEscape(customerID)),
body)
}
// AddDevice adds a device for a customer
func (c *CustomerIO) AddDevice(customerID string, deviceID string, platform string, data map[string]interface{}) error {
return c.AddDeviceCtx(context.Background(), customerID, deviceID, platform, data)
}
// DeleteDeviceCtx deletes a device for a customer
func (c *CustomerIO) DeleteDeviceCtx(ctx context.Context, customerID string, deviceID string) error {
if customerID == "" {
return ParamError{Param: "customerID"}
}
if deviceID == "" {
return ParamError{Param: "deviceID"}
}
return c.request(ctx, "DELETE",
fmt.Sprintf("%s/api/v1/customers/%s/devices/%s", c.URL, url.PathEscape(customerID), url.PathEscape(deviceID)),
nil)
}
// DeleteDevice deletes a device for a customer
func (c *CustomerIO) DeleteDevice(customerID string, deviceID string) error {
return c.DeleteDeviceCtx(context.Background(), customerID, deviceID)
}