Skip to content

Commit

Permalink
Merge pull request #14 from ConductorOne/lauren/check-total-paginatio…
Browse files Browse the repository at this point in the history
…n-offset

Check total count header when generating pagination offset
  • Loading branch information
laurenleach authored Aug 8, 2024
2 parents 07a2816 + 0730471 commit 062a732
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
8 changes: 1 addition & 7 deletions pkg/connector/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package connector

import (
"strconv"

v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
"github.com/conductorone/baton-sdk/pkg/annotations"
"github.com/conductorone/baton-sdk/pkg/pagination"
Expand Down Expand Up @@ -41,11 +39,7 @@ func parsePageToken(i string, resourceID *v2.ResourceId) (*pagination.Bag, int,

// convertPageToken converts a string token into an int.
func convertPageToken(token string) (int, error) {
if token == "" {
return 0, nil
}

return strconv.Atoi(token)
return servicenow.ConvertPageToken(token)
}

func mapGroupMembers(resources []servicenow.GroupMember) []string {
Expand Down
33 changes: 14 additions & 19 deletions pkg/servicenow/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"io"
"net/http"
"net/url"
"strings"
"text/template"

"github.com/tomnomnom/linkheader"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -451,6 +449,12 @@ func (c *Client) doRequest(ctx context.Context, urlAddress string, method string
}
}

totalCountHeader := rawResponse.Header.Get("X-Total-Count")
totalCount, err := ConvertPageToken(totalCountHeader)
if err != nil {
return "", err
}

var pageToken string
pagingLinks := linkheader.Parse(rawResponse.Header.Get("Link"))
for _, link := range pagingLinks {
Expand All @@ -459,26 +463,17 @@ func (c *Client) doRequest(ctx context.Context, urlAddress string, method string
if err != nil {
return "", err
}

pageToken = nextPageUrl.Query().Get("sysparm_offset")
offset := nextPageUrl.Query().Get("sysparm_offset")
token, err := ConvertPageToken(offset)
if err != nil {
return "", err
}
if token < totalCount {
pageToken = offset
}
break
}
}

return pageToken, nil
}

type URLParams map[string]string

func GenerateURL(baseURL string, params URLParams) (string, error) {
tmpl, err := template.New("url").Parse(baseURL)
if err != nil {
return "", err
}
var urlBuilder strings.Builder
err = tmpl.Execute(&urlBuilder, params)
if err != nil {
return "", err
}
return urlBuilder.String(), nil
}
29 changes: 29 additions & 0 deletions pkg/servicenow/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package servicenow

import (
"strconv"
"strings"
"text/template"
)

type URLParams map[string]string

func GenerateURL(baseURL string, params URLParams) (string, error) {
tmpl, err := template.New("url").Parse(baseURL)
if err != nil {
return "", err
}
var urlBuilder strings.Builder
err = tmpl.Execute(&urlBuilder, params)
if err != nil {
return "", err
}
return urlBuilder.String(), nil
}

func ConvertPageToken(token string) (int, error) {
if token == "" {
return 0, nil
}
return strconv.Atoi(token)
}

0 comments on commit 062a732

Please sign in to comment.