-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontacts.go
279 lines (226 loc) · 7.18 KB
/
contacts.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//**********************************************************
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <[email protected]>
//
// This file is part of goplacetel.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor
//
//**********************************************************
package goplacetel
import (
"encoding/json"
"fmt"
"time"
)
// ContactBody is to build a new contact
type ContactBody struct {
Id int `json:"id,omitempty"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
EmailWork string `json:"email_work"`
Company string `json:"company"`
Address string `json:"address"`
AddressWork string `json:"address_work"`
PhoneWork string `json:"phone_work"`
MobileWork string `json:"mobile_work"`
Phone string `json:"phone"`
Mobile string `json:"mobile"`
Fax string `json:"fax"`
FaxWork string `json:"fax_work"`
FacebookUrl string `json:"facebook_url"`
LinkedinUrl string `json:"linkedin_url"`
XingUrl string `json:"xing_url"`
TwitterAccount string `json:"twitter_account"`
Blocked bool `json:"blocked"`
}
// ContactReturn is to decode the json return
type ContactReturn struct {
Id int `json:"id"`
UserId int `json:"user_id"`
Speeddial interface{} `json:"speeddial"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
EmailWork string `json:"email_work"`
Company string `json:"company"`
Address string `json:"address"`
AddressWork string `json:"address_work"`
PhoneWork string `json:"phone_work"`
MobileWork string `json:"mobile_work"`
Phone string `json:"phone"`
Mobile string `json:"mobile"`
Fax string `json:"fax"`
FaxWork string `json:"fax_work"`
FacebookUrl string `json:"facebook_url"`
LinkedinUrl string `json:"linkedin_url"`
XingUrl string `json:"xing_url"`
TwitterAccount string `json:"twitter_account"`
Blocked bool `json:"blocked"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
}
// DeleteContactReturn is to decode the json data
type DeleteContactReturn struct {
Id int `json:"id"`
UserId int `json:"user_id"`
Speeddail interface{} `json:"speeddail"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Number1 string `json:"number1"`
Number2 string `json:"number2"`
Number3 string `json:"number3"`
Number4 string `json:"number4"`
Number5 string `json:"number5"`
IsOnBlacklist bool `json:"isOnBlacklist"`
Email string `json:"email"`
MoreNumbers interface{} `json:"more_numbers"`
ClientId int `json:"client_id"`
IsGlobal bool `json:"is_global"`
Company string `json:"company"`
Address string `json:"address"`
ContactBookId string `json:"contact_book_id"`
ImportFrom interface{} `json:"import_from"`
ImportId interface{} `json:"import_id"`
EmailWork string `json:"email_work"`
AddressWork string `json:"address_work"`
Fax string `json:"fax"`
FaxWork string `json:"fax_work"`
Color interface{} `json:"color"`
FacebookUrl string `json:"facebook_url"`
LinkedinUrl string `json:"linkedin_url"`
XingUrl string `json:"xing_url"`
GoogleplusUrl interface{} `json:"googleplus_url"`
TwitterAccount string `json:"twitter_account"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// Contacts is to get all contacts from the api
func Contacts(token string) ([]ContactReturn, error) {
// To decode the json data
var contacts []ContactReturn
// To call the pages
page := 1
// Loop over all sites
for {
// Set config for new request
r := Request{fmt.Sprintf("/contacts?page=%d&per_page=25", page), "GET", token, nil}
// Send new request
response, err := r.Send()
if err != nil {
return nil, err
}
// Decode data
var decode []ContactReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return nil, err
}
// Close response body
response.Body.Close()
// Add contacts
for _, value := range decode {
contacts = append(contacts, value)
}
// Check length & break the loop
if len(decode) < 25 {
break
} else {
page++
}
}
// Return data
return contacts, nil
}
// Contact is to get a contact by id
func Contact(id string, token string) (ContactReturn, error) {
// Set config for new request
r := Request{"/contacts/" + id, "GET", token, nil}
// Send new request
response, err := r.Send()
if err != nil {
return ContactReturn{}, err
}
// Close response body after function ends
defer response.Body.Close()
// Decode data
var decode ContactReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ContactReturn{}, err
}
// Return data
return decode, nil
}
// AddContact is to add a new contact
func AddContact(body ContactBody, token string) (ContactReturn, error) {
// Convert data
convert, err := json.Marshal(body)
if err != nil {
return ContactReturn{}, err
}
// Set config for new request
r := Request{"/contacts", "POST", token, convert}
// Send new request
response, err := r.Send()
if err != nil {
return ContactReturn{}, err
}
// Close response body after function ends
defer response.Body.Close()
// Decode data
var decode ContactReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ContactReturn{}, err
}
// Return data
return decode, nil
}
// UpdateContact is to add a new contact
func UpdateContact(body ContactBody, token string) (ContactReturn, error) {
// Convert data
convert, err := json.Marshal(body)
if err != nil {
return ContactReturn{}, err
}
// Set config for new request
r := Request{fmt.Sprintf("/contacts/%d", body.Id), "PUT", token, convert}
// Send new request
response, err := r.Send()
if err != nil {
return ContactReturn{}, err
}
// Close response body after function ends
defer response.Body.Close()
// Decode data
var decode ContactReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ContactReturn{}, err
}
// Return data
return decode, nil
}
// DeleteContact is to add a new contact
func DeleteContact(id string, token string) (DeleteContactReturn, error) {
// Set config for new request
r := Request{"/contacts/" + id, "DELETE", token, nil}
// Send new request
response, err := r.Send()
if err != nil {
return DeleteContactReturn{}, err
}
// Close response body after function ends
defer response.Body.Close()
// Decode data
var decode DeleteContactReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return DeleteContactReturn{}, err
}
// Return data
return decode, nil
}