Skip to content
This repository has been archived by the owner on Mar 9, 2023. It is now read-only.

Update contacts function #1

Merged
merged 1 commit into from
Oct 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package golexoffice
import (
"bytes"
"encoding/json"
"fmt"
)

// ContactsReturn is to decode json data
Expand Down Expand Up @@ -202,30 +203,53 @@ type ContactReturn struct {
}

// Contacts is to get a list of all contacts
func Contacts(token string) (ContactsReturn, error) {
func Contacts(token string) ([]ContactsReturnContent, error) {

// Set config for new request
c := Config{"/v1/contacts/", "GET", token, "application/json", nil}
// To save the contact data
var contacts []ContactsReturnContent

// Send request
response, err := c.Send()
if err != nil {
return ContactsReturn{}, err
}
// To call the page
page := 0

// Close request
defer response.Body.Close()
// Loop over all sites
for {

// Decode data
var decode ContactsReturn
// 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++
}

err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return ContactsReturn{}, err
}

// Return data
return decode, nil
return contacts, nil

}

Expand Down