-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.go
More file actions
183 lines (151 loc) · 4.24 KB
/
Copy pathuser.go
File metadata and controls
183 lines (151 loc) · 4.24 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package intercom
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
) //import
const (
USERS_GET_API_ENDPOINT string = "https://api.intercom.io/users"
USERS_POST_API_ENDPOINT string = "https://api.intercom.io/users"
USERS_DELETE_API_ENDPOINT string = "https://api.intercom.io/users"
) //const
func NewUserAvatar() *UserAvatar_t {
return &UserAvatar_t{
IntercomType: "avatar",
} //UserAvatar_t
} //NewUserAavatar
/*
func NewCompanies() *Companies_t {
return &Companies_t{
IntercomType: "company.list",
} //Avatar_t
} //NewAavatar
*/
func NewUserLocation() *UserLocation_t {
return &UserLocation_t{
IntercomType: "location_data",
} //UserLocation_t
} //NewUserLocation
func NewUser(httpMethod string) *User_t {
httpMethod = strings.TrimSpace(strings.ToUpper(httpMethod))
if httpMethod == "POST" {
return &User_t{
CustomAttributes: make(map[string]interface{}),
Companies: make([]UserCompanyPost_t, 0),
} //User_t
} //if
return &User_t{
CustomAttributes: make(map[string]interface{}),
Companies: UserCompanyGet_t{},
} //User_t
} //NewUser
func NewUserList() *UserList_t {
return &UserList_t{
IntercomType: "user.list",
} //UserList_t
} //NewUserList
func (this *Intercom_t) DeleteUser(user *User_t) (err error) {
var (
intercomUrl string = USERS_DELETE_API_ENDPOINT
req *http.Request
resp *http.Response
client = new(http.Client)
) //var
if user.UserId != "" {
intercomUrl += "?user_id=" + user.UserId
} else if user.Email != "" {
intercomUrl += "?email=" + url.QueryEscape(user.Email)
} //else if
// Create new GET request
if req, err = http.NewRequest("DELETE", intercomUrl, nil); err != nil {
return err
} //if
// Set authentication and headers
req.SetBasicAuth(this.AppId, this.ApiKey)
req.Header.Set("Accept", "application/json")
// Perform DELETE request
if resp, err = client.Do(req); err != nil {
return err
} //if
defer resp.Body.Close()
// Check reponse code and report any errors
// Intercom sends back a 200 for valid requests
if resp.StatusCode != 200 {
return errors.New(resp.Status)
} //if
// Decode JSON response into User_t struct
// Full User objecs are returned on DELETE
if err = json.NewDecoder(resp.Body).Decode(user); err != nil {
return err
} //if
return nil
} //DeleteUser
func (this *Intercom_t) GetUser(user *User_t) (err error) {
var (
intercomUrl string = USERS_GET_API_ENDPOINT
req *http.Request
resp *http.Response
client = new(http.Client)
) //var
if user.UserId != "" {
intercomUrl += "?user_id=" + user.UserId
} else if user.Email != "" {
intercomUrl += "?email=" + url.QueryEscape(user.Email)
} //else if
// Create new GET request
if req, err = http.NewRequest("GET", intercomUrl, nil); err != nil {
return err
} //if
// Set authentication and headers
req.SetBasicAuth(this.AppId, this.ApiKey)
req.Header.Set("Accept", "application/json")
// Perform GET request
if resp, err = client.Do(req); err != nil {
return err
} //if
defer resp.Body.Close()
// Check reponse code and report any errors
// Intercom sends back a 200 for valid requests
if resp.StatusCode != 200 {
return errors.New(resp.Status)
} //if
// Decode JSON response into User_t struct
if err = json.NewDecoder(resp.Body).Decode(user); err != nil {
return err
} //if
return nil
} //GetUser
func (this *Intercom_t) PostUser(user *User_t) (err error) {
var (
req *http.Request
resp *http.Response
buffer = new(bytes.Buffer)
client = new(http.Client)
) //var
// Encode user struct into JSON
if err = json.NewEncoder(buffer).Encode(*user); err != nil {
return err
} //if
// Create new POST request
if req, err = http.NewRequest("POST", USERS_POST_API_ENDPOINT, buffer); err != nil {
return err
} //if
// Set authentication and headers
req.SetBasicAuth(this.AppId, this.ApiKey)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
// Perform POST request
if resp, err = client.Do(req); err != nil {
return err
} //if
defer resp.Body.Close()
// Check reponse code and report any errors
// Intercom sends back a 200 for valid requests
if resp.StatusCode != 200 {
return errors.New(resp.Status)
} //if
return err
} //PostUser