-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
165 lines (128 loc) · 4.39 KB
/
client.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package vero
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/pkg/errors"
"github.com/uniplaces/vero-go"
)
const baseUrl = "https://api.getvero.com/api/v2/%v"
// VeroClient manages requests to the Vero API
type VeroClient struct {
authToken string
baseUrl string
}
type requestData map[string]interface{}
// NewClient returns a new instance of the Vero client
func NewClient(authToken string) vero_go.Client {
return VeroClient{authToken: authToken, baseUrl: baseUrl}
}
// Identify creates a new user profile if the user doesn’t exist yet
func (client VeroClient) Identify(userId string, data map[string]interface{}, email *string) ([]byte, error) {
endpoint := client.buildEndpoint("users/track")
requestData := requestData{}
requestData["auth_token"] = client.authToken
requestData["id"] = userId
requestData["data"] = data
if email != nil {
data["email"] = *email
}
return client.send(endpoint, requestData, http.MethodPost)
}
// Reidentify updates user id from existing user
func (client VeroClient) Reidentify(userId string, newUserId string) ([]byte, error) {
endpoint := client.buildEndpoint("users/reidentify")
requestData := requestData{}
requestData["auth_token"] = client.authToken
requestData["id"] = userId
requestData["new_id"] = newUserId
return client.send(endpoint, requestData, http.MethodPut)
}
// Update updates information from existing user
func (client VeroClient) Update(userId string, changes map[string]interface{}) ([]byte, error) {
endpoint := client.buildEndpoint("users/edit")
requestData := requestData{}
requestData["auth_token"] = client.authToken
requestData["id"] = userId
requestData["changes"] = changes
return client.send(endpoint, requestData, http.MethodPut)
}
// Tags lets you add or remove tags to or from any of your users
func (client VeroClient) Tags(userId string, add []string, remove []string) ([]byte, error) {
endpoint := client.buildEndpoint("users/tags/edit")
requestData := requestData{}
requestData["auth_token"] = client.authToken
requestData["id"] = userId
requestData["add"] = add
requestData["remove"] = remove
return client.send(endpoint, requestData, http.MethodPut)
}
// Unsubscribe unsubscribes a single user
func (client VeroClient) Unsubscribe(userId string) ([]byte, error) {
endpoint := client.buildEndpoint("users/unsubscribe")
requestData := requestData{}
requestData["auth_token"] = client.authToken
requestData["id"] = userId
return client.send(endpoint, requestData, http.MethodPost)
}
// Resubscribe lets you resubscribe a single user
func (client VeroClient) Resubscribe(userId string) ([]byte, error) {
endpoint := client.buildEndpoint("users/resubscribe")
requestData := requestData{}
requestData["auth_token"] = client.authToken
requestData["id"] = userId
return client.send(endpoint, requestData, http.MethodPost)
}
// Track endpoint tracks an event for a specific customer. If the customer profile doesn’t exist, Vero will create it
func (client VeroClient) Track(
eventName string,
identity map[string]string,
data map[string]interface{},
extras map[string]interface{},
) (
[]byte,
error,
) {
endpoint := client.buildEndpoint("events/track")
requestData := requestData{}
requestData["auth_token"] = client.authToken
requestData["identity"] = identity
requestData["event_name"] = eventName
requestData["data"] = data
requestData["extras"] = extras
return client.send(endpoint, requestData, http.MethodPost)
}
func (client VeroClient) buildEndpoint(endpoint string) string {
return fmt.Sprintf(client.baseUrl, endpoint)
}
func (VeroClient) send(url string, data map[string]interface{}, method string) ([]byte, error) {
payload, err := json.Marshal(data)
if err != nil {
return []byte{}, err
}
request, err := http.NewRequest(method, url, bytes.NewReader(payload))
if err != nil {
return []byte{}, err
}
request.Close = true
request.Header.Add("Accept", "application/json")
request.Header.Add("Content-Type", "application/json")
response, err := http.DefaultClient.Do(request)
if err != nil {
return []byte{}, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return []byte{}, err
}
if response.StatusCode != http.StatusOK {
return []byte{}, createErrorWithHTTPResponseMessage(body)
}
return body, nil
}
func createErrorWithHTTPResponseMessage(responseBody []byte) error {
return errors.New(string(responseBody))
}