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

Commit

Permalink
Merge pull request #1 from jjideenschmiede/development
Browse files Browse the repository at this point in the history
Update contacts function
  • Loading branch information
gowizzard authored Oct 17, 2021
2 parents 10465d3 + c45625a commit 9f76771
Showing 1 changed file with 40 additions and 16 deletions.
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

0 comments on commit 9f76771

Please sign in to comment.