-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: support fetching environments with the limit+offset feature (#356)
* Fix: support fetching environments with the limit+offset feature * revert generics * replaced 100 and 33 with consts
- Loading branch information
1 parent
dbedc77
commit 4d9a448
Showing
4 changed files
with
156 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package client | ||
|
||
import "strconv" | ||
|
||
const limit = 100 | ||
|
||
type Pagination struct { | ||
offset int | ||
params map[string]string | ||
} | ||
|
||
// Note: all the FIXME should be uncommented when Alpine adds support for go 1.18. | ||
// This will enable generics. | ||
|
||
type Paginated interface { | ||
// FIXME: Environment | ||
getEndpoint() string | ||
} | ||
|
||
func (p *Pagination) getParams() map[string]string { | ||
return map[string]string{ | ||
"limit": strconv.Itoa(limit), | ||
"offset": strconv.Itoa(p.offset), | ||
} | ||
} | ||
|
||
// returns true if there is more data. | ||
func (p *Pagination) next(currentPageSize int) bool { | ||
p.offset += currentPageSize | ||
return currentPageSize == limit | ||
} | ||
|
||
// params - additional params. may be nil. | ||
// FIXME: func getAll[P Paginated](client *ApiClient, params map[string]string) ([]P, error) { | ||
func getAll(client *ApiClient, params map[string]string) ([]Environment, error) { | ||
p := Pagination{ | ||
offset: 0, | ||
params: params, | ||
} | ||
|
||
// FIXME: var allResults []P | ||
var allResults []Environment | ||
|
||
for { | ||
pageParams := p.getParams() | ||
for k, v := range params { | ||
pageParams[k] = v | ||
} | ||
|
||
// FIXME: var pageResults []P | ||
var pageResults []Environment | ||
|
||
// FIXME: err := client.http.Get(P{}.getEndpoint(), pageParams, &pageResults) | ||
err := client.http.Get(Environment{}.getEndpoint(), pageParams, &pageResults) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
allResults = append(allResults, pageResults...) | ||
|
||
if more := p.next(len(pageResults)); !more { | ||
break | ||
} | ||
} | ||
|
||
return allResults, nil | ||
} |