-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
56 lines (46 loc) · 892 Bytes
/
users.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
package helpscout
import (
"net/http"
"net/url"
"strconv"
)
type UsersLister interface {
Process(c User) bool
}
type User struct {
Id uint `json:"id"`
Type string `json:"type"`
FirstName string `json:"first"`
LastName string `json:"last"`
Email string `json:"email"`
}
func (c *Client) ListUsers(lister UsersLister) error {
page := 1
query := &url.Values{}
for {
var uList struct {
Users []User `json:"users"`
}
req := &generalListApiCallReq{
Embedded: &uList,
}
err := c.doApiCall(http.MethodGet, "/users", query, nil, req)
if err != nil {
return err
}
if req.Page.TotalPages == 0 {
break
}
for _, user := range uList.Users {
if !lister.Process(user) {
return ErrorInterrupted
}
}
if req.Page.Number == req.Page.TotalPages {
break
}
page++
query.Set("page", strconv.Itoa(page))
}
return nil
}