Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: add pagination for teams api #983

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
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
31 changes: 24 additions & 7 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,20 +89,32 @@ func (client *ApiClient) GetTeams(params map[string]string) ([]Team, error) {
return nil, err
}

var result []Team
var teams []Team

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

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["offset"] = nextPageKey
}

return result, err
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"})
}
30 changes: 26 additions & 4 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,13 +45,29 @@ 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, nil, gomock.Any()).
Do(func(path string, request interface{}, response *[]Team) {
*response = mockTeams
Get("/teams/organizations/"+organizationId, map[string]string{
"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",
"limit": "100",
}, gomock.Any()).
Do(func(path string, request interface{}, response *PaginatedTeamsResponse) {
*response = PaginatedTeamsResponse{
Teams: mockTeams2,
}
})
returnedTeams, _ = apiClient.Teams()
})
Expand All @@ -57,10 +78,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
Loading