This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
contacts.go
350 lines (283 loc) · 9.21 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//**********************************************************
//
// This file is part of lexoffice.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor
//
//**********************************************************
package golexoffice
import (
"bytes"
"encoding/json"
"fmt"
)
// ContactsReturn is to decode json data
type ContactsReturn struct {
Content []ContactsReturnContent `json:"content"`
First bool `json:"first"`
Last bool `json:"last"`
TotalPages int `json:"totalPages"`
TotalElements int `json:"totalElements"`
NumberOfElements int `json:"numberOfElements"`
Size int `json:"size"`
Number int `json:"number"`
Sort []ContactsReturnSort `json:"sort"`
}
type ContactsReturnContent struct {
Id string `json:"id,omitempty"`
Version int `json:"version,omitempty"`
Roles ContactBodyRoles `json:"roles"`
Company ContactBodyCompany `json:"company"`
Addresses ContactBodyAddresses `json:"addresses"`
EmailAddresses ContactBodyEmailAddresses `json:"emailAddresses"`
PhoneNumbers ContactBodyPhoneNumbers `json:"phoneNumbers"`
Note string `json:"note"`
Archived bool `json:"archived,omitempty"`
}
type ContactsReturnRoles struct {
Customer ContactsReturnCustomer `json:"customer"`
Vendor ContactsReturnVendor `json:"vendor"`
}
type ContactsReturnCustomer struct {
Number int `json:"number,omitempty"`
}
type ContactsReturnVendor struct {
Number int `json:"number,omitempty"`
}
type ContactsReturnCompany struct {
Name string `json:"name"`
TaxNumber string `json:"taxNumber"`
VatRegistrationId string `json:"vatRegistrationId"`
AllowTaxFreeInvoices bool `json:"allowTaxFreeInvoices"`
ContactPersons []ContactsReturnContactPersons `json:"contactPersons"`
}
type ContactsReturnContactPersons struct {
Salutation string `json:"salutation"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
EmailAddress string `json:"emailAddress"`
PhoneNumber string `json:"phoneNumber"`
}
type ContactsReturnAddresses struct {
Billing []ContactsReturnBilling `json:"billing"`
Shipping []ContactsReturnShipping `json:"shipping"`
}
type ContactsReturnBilling struct {
Supplement string `json:"supplement"`
Street string `json:"street"`
Zip string `json:"zip"`
City string `json:"city"`
CountryCode string `json:"countryCode"`
}
type ContactsReturnShipping struct {
Supplement string `json:"supplement"`
Street string `json:"street"`
Zip string `json:"zip"`
City string `json:"city"`
CountryCode string `json:"countryCode"`
}
type ContactsReturnEmailAddresses struct {
Business []string `json:"business"`
Office []string `json:"office"`
Private []string `json:"private"`
Other []string `json:"other"`
}
type ContactsReturnPhoneNumbers struct {
Business []string `json:"business"`
Office []string `json:"office"`
Mobile []string `json:"mobile"`
Private []string `json:"private"`
Fax []string `json:"fax"`
Other []string `json:"other"`
}
type ContactsReturnSort struct {
Property string `json:"property"`
Direction string `json:"direction"`
IgnoreCase bool `json:"ignoreCase"`
NullHandling string `json:"nullHandling"`
Ascending bool `json:"ascending"`
}
// ContactBody is to create a new contact
type ContactBody struct {
Id string `json:"id,omitempty"`
Version int `json:"version,omitempty"`
Roles ContactBodyRoles `json:"roles"`
Company ContactBodyCompany `json:"company"`
Addresses ContactBodyAddresses `json:"addresses"`
EmailAddresses ContactBodyEmailAddresses `json:"emailAddresses"`
PhoneNumbers ContactBodyPhoneNumbers `json:"phoneNumbers"`
Note string `json:"note"`
Archived bool `json:"archived,omitempty"`
}
type ContactBodyRoles struct {
Customer ContactBodyCustomer `json:"customer"`
Vendor ContactBodyVendor `json:"vendor"`
}
type ContactBodyCustomer struct {
Number int `json:"number,omitempty"`
}
type ContactBodyVendor struct {
Number int `json:"number,omitempty"`
}
type ContactBodyCompany struct {
Name string `json:"name"`
TaxNumber string `json:"taxNumber"`
VatRegistrationId string `json:"vatRegistrationId"`
AllowTaxFreeInvoices bool `json:"allowTaxFreeInvoices"`
ContactPersons []ContactBodyContactPersons `json:"contactPersons"`
}
type ContactBodyContactPersons struct {
Salutation string `json:"salutation"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
EmailAddress string `json:"emailAddress"`
PhoneNumber string `json:"phoneNumber"`
}
type ContactBodyAddresses struct {
Billing []ContactBodyBilling `json:"billing"`
Shipping []ContactBodyShipping `json:"shipping"`
}
type ContactBodyBilling struct {
Supplement string `json:"supplement"`
Street string `json:"street"`
Zip string `json:"zip"`
City string `json:"city"`
CountryCode string `json:"countryCode"`
}
type ContactBodyShipping struct {
Supplement string `json:"supplement"`
Street string `json:"street"`
Zip string `json:"zip"`
City string `json:"city"`
CountryCode string `json:"countryCode"`
}
type ContactBodyEmailAddresses struct {
Business []string `json:"business"`
Office []string `json:"office"`
Private []string `json:"private"`
Other []string `json:"other"`
}
type ContactBodyPhoneNumbers struct {
Business []string `json:"business"`
Office []string `json:"office"`
Mobile []string `json:"mobile"`
Private []string `json:"private"`
Fax []string `json:"fax"`
Other []string `json:"other"`
}
// ContactReturn is to decode json return
type ContactReturn struct {
ID string `json:"id"`
ResourceUri string `json:"resourceUri"`
CreatedDate string `json:"createdDate"`
UpdatedDate string `json:"updatedDate"`
Version int `json:"version"`
}
// Contacts is to get a list of all contacts
func Contacts(token string) ([]ContactsReturnContent, error) {
// To save the contact data
var contacts []ContactsReturnContent
// To call the page
page := 0
// Loop over all sites
for {
// Set config for new request
c := Config{fmt.Sprintf("/v1/contacts?page=%d", page), "GET", token, "application/json", nil}
// Send request
response, err := c.Send()
if err != nil {
return nil, err
}
// Decode data
var decode ContactsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return nil, err
}
// Close request
response.Body.Close()
// Add contacts
for _, value := range decode.Content {
contacts = append(contacts, value)
}
// Check length & break the loop
if decode.TotalPages == page {
break
} else {
page++
}
}
// Return data
return contacts, nil
}
// Contact is to get a contact by id
func Contact(id, token string) (ContactsReturnContent, error) {
// Set config for new request
c := Config{"/v1/contacts/" + id, "GET", token, "application/json", nil}
// Send request
response, err := c.Send()
if err != nil {
return ContactsReturnContent{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode ContactsReturnContent
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ContactsReturnContent{}, err
}
// Return data
return decode, nil
}
// AddContact is to add a new contact
func AddContact(body ContactBody, token string) (ContactReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return ContactReturn{}, err
}
// Set config for new request
c := Config{"/v1/contacts/", "POST", token, "application/json", bytes.NewBuffer(convert)}
// Send request
response, err := c.Send()
if err != nil {
return ContactReturn{}, err
}
// Close request
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 body
convert, err := json.Marshal(body)
if err != nil {
return ContactReturn{}, err
}
// Set config for new request
c := Config{"/v1/contacts/" + body.Id, "PUT", token, "application/json", bytes.NewBuffer(convert)}
// Send request
response, err := c.Send()
if err != nil {
return ContactReturn{}, err
}
// Close request
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
}