-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package connector | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" | ||
"github.com/conductorone/baton-sdk/pkg/pagination" | ||
) | ||
|
||
const ResourcesPageSize uint = 50 | ||
|
||
func parsePageToken(i string, resourceID *v2.ResourceId) (*pagination.Bag, uint, error) { | ||
b := &pagination.Bag{} | ||
err := b.Unmarshal(i) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
if b.Current() == nil { | ||
b.Push(pagination.PageState{ | ||
ResourceTypeID: resourceID.ResourceType, | ||
ResourceID: resourceID.Resource, | ||
}) | ||
} | ||
|
||
page, err := convertPageToken(b.PageToken()) | ||
if err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
return b, page, nil | ||
} | ||
|
||
// convertPageToken converts a string token into an int. | ||
func convertPageToken(token string) (uint, error) { | ||
if token == "" { | ||
return 1, nil | ||
} | ||
|
||
page, err := strconv.ParseUint(token, 10, 32) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to parse page token: %w", err) | ||
} | ||
|
||
return uint(page), nil | ||
} | ||
|
||
// prepareNextToken prepares the next page token. | ||
// It calculates the next page number and returns it as a string. | ||
func prepareNextToken(page uint, pageTotal int, total uint) string { | ||
var token string | ||
|
||
next := page + uint(pageTotal) | ||
if next < total+1 { | ||
token = strconv.FormatUint(uint64(next), 10) | ||
} | ||
|
||
return token | ||
} |