-
Notifications
You must be signed in to change notification settings - Fork 3
/
attributes.go
102 lines (90 loc) · 3.68 KB
/
attributes.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
package chartmogul
// attributesDefinition is internal struct used to define multiple new custom attributes.
type attributesDefinition struct {
Email string `json:"email,omitempty"`
Custom []*CustomAttribute `json:"custom"`
}
// CustomAttributes contains updated custom attributes.
type CustomAttributes struct {
Custom map[string]interface{} `json:"custom"`
}
type deleteCustomAttrs struct {
Custom []string `json:"custom"`
}
// CustomAttribute = typed custom attribute.
type CustomAttribute struct {
Type string `json:"type"`
Key string `json:"key"`
Value interface{} `json:"value"`
Source string `json:"source,omitempty"`
}
// AttributeWithSource covers the special case when you need to update customer's attribute
// and chage the source which shows in the ChartMogul UI.
type AttributeWithSource struct {
Value interface{} `json:"value"`
Source string `json:"source"`
}
const (
customersAttributesEndpoint = "customers/:uuid/attributes"
customerCustomAttributesEndpoint = "customers/:uuid/attributes/custom"
customAttributesEndpoint = "customers/attributes/custom"
// AttrTypeString is one of the possible data types for custom attributes.
AttrTypeString = "String"
// AttrTypeInteger is one of the possible data types for custom attributes.
AttrTypeInteger = "Integer"
// AttrTypeTimestamp is one of the possible data types for custom attributes.
AttrTypeTimestamp = "Timestamp"
// AttrTypeBoolean is one of the possible data types for custom attributes.
AttrTypeBoolean = "Boolean"
)
// RetrieveCustomersAttributes returns attributes for given customer UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#customer-attributes
func (api API) RetrieveCustomersAttributes(customerUUID string) (*Attributes, error) {
output := &Attributes{}
err := api.retrieve(customersAttributesEndpoint, customerUUID, output)
return output, err
}
// AddCustomAttributesToCustomer adds custom attributes to specific customer.
//
// See https://dev.chartmogul.com/v1.0/reference#customer-attributes
func (api API) AddCustomAttributesToCustomer(customerUUID string, customAttributes []*CustomAttribute) (*CustomAttributes, error) {
output := &CustomAttributes{}
err := api.add(customerCustomAttributesEndpoint,
customerUUID,
&attributesDefinition{Custom: customAttributes},
output)
return output, err
}
// AddCustomAttributesWithEmail adds custom attributes to customers with specific email.
//
// See https://dev.chartmogul.com/v1.0/reference#customer-attributes
func (api API) AddCustomAttributesWithEmail(email string, customAttributes []*CustomAttribute) (*Customers, error) {
output := &Customers{}
err := api.create(customAttributesEndpoint,
&attributesDefinition{Email: email, Custom: customAttributes},
output)
return output, err
}
// UpdateCustomAttributesOfCustomer updates custom attributes of a specific customer.
//
// See https://dev.chartmogul.com/v1.0/reference#customer-attributes
func (api API) UpdateCustomAttributesOfCustomer(customerUUID string, customAttributes map[string]interface{}) (*CustomAttributes, error) {
output := &CustomAttributes{}
err := api.putTo(customerCustomAttributesEndpoint,
customerUUID,
&CustomAttributes{Custom: customAttributes},
output)
return output, err
}
// RemoveCustomAttributes removes a list of custom attributes from a specific customer.
//
// See https://dev.chartmogul.com/v1.0/reference#customer-attributes
func (api API) RemoveCustomAttributes(customerUUID string, customAttributes []string) (*CustomAttributes, error) {
output := &CustomAttributes{}
err := api.deleteWhat(customerCustomAttributesEndpoint,
customerUUID,
&deleteCustomAttrs{Custom: customAttributes},
output)
return output, err
}