Skip to content

Commit

Permalink
Merge pull request #235 from principis/group-pagination
Browse files Browse the repository at this point in the history
Add List Groups pagination
  • Loading branch information
nukosuke authored Jul 13, 2022
2 parents b219ca3 + c395468 commit ad089a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
23 changes: 20 additions & 3 deletions zendesk/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ type Group struct {
UpdatedAt time.Time `json:"updated_at,omitempty"`
}

// GroupListOptions is options for GetGroups
//
// ref: https://developer.zendesk.com/rest_api/docs/support/groups#list-groups
type GroupListOptions struct {
PageOptions
}

// GroupAPI an interface containing all methods associated with zendesk groups
type GroupAPI interface {
GetGroups(ctx context.Context) ([]Group, Page, error)
GetGroups(ctx context.Context, opts *GroupListOptions) ([]Group, Page, error)
GetGroup(ctx context.Context, groupID int64) (Group, error)
CreateGroup(ctx context.Context, group Group) (Group, error)
UpdateGroup(ctx context.Context, groupID int64, group Group) (Group, error)
Expand All @@ -31,13 +38,23 @@ type GroupAPI interface {

// GetGroups fetches group list
// https://developer.zendesk.com/rest_api/docs/support/groups#list-groups
func (z *Client) GetGroups(ctx context.Context) ([]Group, Page, error) {
func (z *Client) GetGroups(ctx context.Context, opts *GroupListOptions) ([]Group, Page, error) {
var data struct {
Groups []Group `json:"groups"`
Page
}

body, err := z.get(ctx, "/groups.json")
tmp := opts
if tmp == nil {
tmp = &GroupListOptions{}
}

u, err := addOptions("/groups.json", tmp)
if err != nil {
return []Group{}, Page{}, err
}

body, err := z.get(ctx, u)
if err != nil {
return []Group{}, Page{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion zendesk/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestGetGroups(t *testing.T) {
client := newTestClient(mockAPI)
defer mockAPI.Close()

groups, _, err := client.GetGroups(ctx)
groups, _, err := client.GetGroups(ctx, nil)
if err != nil {
t.Fatalf("Failed to get groups: %s", err)
}
Expand Down
8 changes: 4 additions & 4 deletions zendesk/mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ad089a1

Please sign in to comment.