Skip to content

Commit

Permalink
pagination for teams
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Nov 19, 2024
1 parent 8fbe16f commit c3f4382
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
4 changes: 3 additions & 1 deletion client/pagination.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import "strconv"
import (
"strconv"
)

const limit = 100

Expand Down
32 changes: 28 additions & 4 deletions client/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type Team struct {
Users []User `json:"users"`
}

type PaginatedTeamsResponse struct {
Teams []Team `json:"teams"`
NextPageKey string `json:"nextPageKey"`
}

func (client *ApiClient) TeamCreate(payload TeamCreatePayload) (Team, error) {
if payload.Name == "" {
return Team{}, errors.New("must specify team name on creation")
Expand Down Expand Up @@ -84,15 +89,34 @@ func (client *ApiClient) GetTeams(params map[string]string) ([]Team, error) {
return nil, err
}

endpoint := "/teams/organizations/" + organizationId
var teams []Team

var res PaginatedTeamsResponse

for {
if err := client.http.Get("/teams/organizations/"+organizationId, params, &res); err != nil {
return nil, err
}

teams = append(teams, res.Teams...)

nextPageKey := res.NextPageKey
if nextPageKey == "" {
break
}

params = map[string]string{
"offset": nextPageKey,
}
}

return getAll[Team](client, endpoint, params)
return teams, nil
}

func (client *ApiClient) Teams() ([]Team, error) {
return client.GetTeams(nil)
return client.GetTeams(map[string]string{"limit": "100"})
}

func (client *ApiClient) TeamsByName(name string) ([]Team, error) {
return client.GetTeams(map[string]string{"name": name})
return client.GetTeams(map[string]string{"name": name, "limit": "100"})
}
28 changes: 23 additions & 5 deletions client/team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ var _ = Describe("Teams Client", func() {
Name: "team-name",
}

mockTeam2 := Team{
Id: "team-id2",
Name: "team-name2",
}

Describe("Get Single Team", func() {
var returnedTeam Team

Expand All @@ -40,16 +45,28 @@ var _ = Describe("Teams Client", func() {
Describe("Get All Teams", func() {
var returnedTeams []Team
mockTeams := []Team{mockTeam}
mockTeams2 := []Team{mockTeam2}

BeforeEach(func() {
mockOrganizationIdCall()
httpCall = mockHttpClient.EXPECT().
Get("/teams/organizations/"+organizationId, map[string]string{
"offset": "0",
"limit": "100",
"limit": "100",
}, gomock.Any()).
Do(func(path string, request interface{}, response *PaginatedTeamsResponse) {
*response = PaginatedTeamsResponse{
Teams: mockTeams,
NextPageKey: "next_page_key",
}
})
httpCall2 = mockHttpClient.EXPECT().
Get("/teams/organizations/"+organizationId, map[string]string{
"offset": "next_page_key",
}, gomock.Any()).
Do(func(path string, request interface{}, response *[]Team) {
*response = mockTeams
Do(func(path string, request interface{}, response *PaginatedTeamsResponse) {
*response = PaginatedTeamsResponse{
Teams: mockTeams2,
}
})
returnedTeams, _ = apiClient.Teams()
})
Expand All @@ -60,10 +77,11 @@ var _ = Describe("Teams Client", func() {

It("Should send GET request", func() {
httpCall.Times(1)
httpCall2.Times(1)
})

It("Should return teams", func() {
Expect(returnedTeams).To(Equal(mockTeams))
Expect(returnedTeams).To(Equal(append(mockTeams, mockTeams2...)))
})
})

Expand Down

0 comments on commit c3f4382

Please sign in to comment.