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

feat(scw): support listing with page token #2411

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions api/audit_trail/v1alpha1/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package audit_trail

import (
"github.com/scaleway/scaleway-sdk-go/errors"
)

func (r *ListEventsResponse) UnsafeAppend(res interface{}) (*string, error) {
results, ok := res.(*ListEventsResponse)
if !ok {
return nil, errors.New("%T type cannot be appended to type %T", res, r)
}

r.Events = append(r.Events, results.Events...)

return results.NextPageToken, nil
}
46 changes: 46 additions & 0 deletions scw/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ func (c *Client) do(req *ScalewayRequest, res interface{}) (sdkErr error) {
return nil
}

type listerWithToken interface {
UnsafeAppend(interface{}) (*string, error)
}

type lister interface {
UnsafeGetTotalCount() uint64
UnsafeAppend(interface{}) (uint64, error)
Expand Down Expand Up @@ -301,6 +305,14 @@ func listerAppend(recv interface{}, elems interface{}) (uint64, error) {
panic(fmt.Errorf("%T does not support pagination but checks failed, should not happen", recv))
}

func listerTokenAppend(recv interface{}, elems interface{}) (*string, error) {
if l, isLister := recv.(listerWithToken); isLister {
return l.UnsafeAppend(elems)
}

panic(fmt.Errorf("%T does not support token pagination but checks failed, should not happen", recv))
}

func isLister(i interface{}) bool {
switch i.(type) {
case lister:
Expand All @@ -312,6 +324,15 @@ func isLister(i interface{}) bool {
}
}

func isListerWithToken(i interface{}) bool {
switch i.(type) {
case listerWithToken:
return true
default:
return false
}
}

const maxPageCount uint64 = math.MaxUint32

// doListAll collects all pages of a List request and aggregate all results on a single response.
Expand Down Expand Up @@ -348,6 +369,31 @@ func (c *Client) doListAll(req *ScalewayRequest, res interface{}) (err error) {
}
return nil
}
if isListerWithToken(res) {
var nextPageToken *string

for {
if nextPageToken != nil {
req.Query.Set("page_token", *nextPageToken)
}

nextPage := newVariableFromType(res)
err := c.do(req, nextPage)
if err != nil {
return err
}

// append results
nextPageToken, err = listerTokenAppend(res, nextPage)
if err != nil {
return err
}

if nextPageToken == nil {
return nil
}
}
}

return errors.New("%T does not support pagination", res)
}
Expand Down
Loading