Skip to content

Commit

Permalink
Fix: add pagination for teams api
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Nov 19, 2024
1 parent 74b8a6d commit bacca6c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 21 deletions.
8 changes: 2 additions & 6 deletions client/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,24 +233,20 @@ type EnvironmentDeployResponse struct {
Id string `json:"id"`
}

func (Environment) getEndpoint() string {
return "/environments"
}

func (client *ApiClient) EnvironmentsByName(name string) ([]Environment, error) {
organizationId, err := client.OrganizationId()
if err != nil {
return nil, err
}

return getAll(client, map[string]string{
return getAll[Environment](client, "/environments", map[string]string{
"organizationId": organizationId,
"name": name,
})
}

func (client *ApiClient) ProjectEnvironments(projectId string) ([]Environment, error) {
return getAll(client, map[string]string{
return getAll[Environment](client, "/environments", map[string]string{
"projectId": projectId,
})
}
Expand Down
9 changes: 2 additions & 7 deletions client/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ type Pagination struct {
params map[string]string
}

type Paginated interface {
Environment
getEndpoint() string
}

func (p *Pagination) getParams() map[string]string {
return map[string]string{
"limit": strconv.Itoa(limit),
Expand All @@ -29,7 +24,7 @@ func (p *Pagination) next(currentPageSize int) bool {
}

// params - additional params. may be nil.
func getAll[P Paginated](client *ApiClient, params map[string]string) ([]P, error) {
func getAll[P any](client *ApiClient, endpoint string, params map[string]string) ([]P, error) {
p := Pagination{
offset: 0,
params: params,
Expand All @@ -45,7 +40,7 @@ func getAll[P Paginated](client *ApiClient, params map[string]string) ([]P, erro

var pageResults []P

err := client.http.Get(P{}.getEndpoint(), pageParams, &pageResults)
err := client.http.Get(endpoint, pageParams, &pageResults)
if err != nil {
return nil, err
}
Expand Down
9 changes: 2 additions & 7 deletions client/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,9 @@ func (client *ApiClient) GetTeams(params map[string]string) ([]Team, error) {
return nil, err
}

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

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

return result, err
return getAll[Team](client, endpoint, params)
}

func (client *ApiClient) Teams() ([]Team, error) {
Expand Down
5 changes: 4 additions & 1 deletion client/team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ var _ = Describe("Teams Client", func() {
BeforeEach(func() {
mockOrganizationIdCall()
httpCall = mockHttpClient.EXPECT().
Get("/teams/organizations/"+organizationId, nil, gomock.Any()).
Get("/teams/organizations/"+organizationId, map[string]string{
"offset": "0",
"limit": "100",
}, gomock.Any()).
Do(func(path string, request interface{}, response *[]Team) {
*response = mockTeams
})
Expand Down

0 comments on commit bacca6c

Please sign in to comment.