Skip to content

Commit

Permalink
fix pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaeta committed Jul 9, 2024
1 parent 681590b commit ecd35b7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
17 changes: 2 additions & 15 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ type Config struct {
}

type Connector struct {
client client.PrivXClient
baseUrl string
apiClientId string
apiClientSecret string
oAuthClientID string
oAuthClientSecret string
client client.PrivXClient
}

// ResourceSyncers returns a ResourceSyncer for each resource type that should be synced from the upstream service.
Expand Down Expand Up @@ -76,13 +71,5 @@ func New(ctx context.Context, config Config) (*Connector, error) {
return nil, err
}

privXConnector := &Connector{
baseUrl: config.BaseUrl,
apiClientId: config.ApiClientId,
apiClientSecret: config.ApiClientSecret,
oAuthClientID: config.OAuthClientID,
oAuthClientSecret: config.OAuthClientSecret,
client: *privXClient,
}
return privXConnector, nil
return &Connector{client: *privXClient}, nil
}
32 changes: 28 additions & 4 deletions pkg/connector/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@ package connector

import (
"strconv"

"github.com/conductorone/baton-sdk/pkg/pagination"
)

const (
ResourcePageSizeDefault = 100
)

func parsePageToken(token string) (int, error) {
if token == "" {
return 0, nil
// parsePageToken takes a pagination and parses it into offset and limit (in
// that order) and picks sensible defaults.
func parsePageToken(pageToken *pagination.Token) (int, int, error) {
offset := 0
limit := ResourcePageSizeDefault

if pageToken == nil {
return offset, limit, nil
}
return strconv.Atoi(token)

if pageToken.Token != "" {
offsetValue, err := strconv.Atoi(pageToken.Token)
if err != nil {
return 0, 0, err
}
offset = offsetValue
}

if pageToken.Size > 0 {
limit = pageToken.Size
}

return offset, limit, nil
}
10 changes: 5 additions & 5 deletions pkg/connector/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type roleBuilder struct {
}

func (o *roleBuilder) ResourceType(ctx context.Context) *v2.ResourceType {
return userResourceType
return roleResourceType
}

// List returns all the users from the database as resource objects.
Expand All @@ -46,12 +46,12 @@ func (o *roleBuilder) List(
) {
logger := ctxzap.Extract(ctx)

offset, err := parsePageToken(pToken.Token)
offset, limit, err := parsePageToken(pToken)
if err != nil {
logger.Error("invalid page token", zap.Error(err))
}

privXRoles, nextToken, err := o.client.GetRoles(ctx, offset, pToken.Size)
privXRoles, nextToken, err := o.client.GetRoles(ctx, offset, limit)
if err != nil {
logger.Debug("Error fetching users", zap.Error(err))
return nil, "", nil, err
Expand Down Expand Up @@ -100,7 +100,7 @@ func (o *roleBuilder) Grants(
zap.String("pToken", pToken.Token),
)

offset, err := parsePageToken(pToken.Token)
offset, limit, err := parsePageToken(pToken)
if err != nil {
logger.Error("invalid page token", zap.Error(err))
}
Expand All @@ -109,7 +109,7 @@ func (o *roleBuilder) Grants(
ctx,
resource.Id.Resource,
offset,
pToken.Size,
limit,
)
if err != nil {
return nil, "", nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/connector/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ func (o *userBuilder) List(
zap.String("pToken", pToken.Token),
)

offset, err := parsePageToken(pToken.Token)
offset, limit, err := parsePageToken(pToken)
if err != nil {
logger.Error("invalid page token", zap.Error(err))
}

privXUsers, nextToken, err := o.client.GetUsers(ctx, offset, pToken.Size)
privXUsers, nextToken, err := o.client.GetUsers(ctx, offset, limit)
if err != nil {
logger.Debug(
"Error fetching users",
Expand Down

0 comments on commit ecd35b7

Please sign in to comment.