Skip to content

Commit 69154c9

Browse files
authored
Merge pull request #69 from rgeyer/pagination-parsing
Deterministic handling of pagination URIs
2 parents 2d147e9 + 96f82cc commit 69154c9

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

exporter/http.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"io/ioutil"
66
"net/http"
7+
neturl "net/url"
78
"strconv"
8-
"strings"
99
"time"
1010

1111
log "github.com/sirupsen/logrus"
@@ -72,17 +72,23 @@ func paginateTargets(targets []string, token string) []string {
7272
for _, link := range links {
7373
if link.Rel == "last" {
7474

75-
subs := strings.Split(link.URL, "&page=")
75+
u, err := neturl.Parse(link.URL)
76+
if err != nil {
77+
log.Errorf("Unable to parse page URL, Error: %s", err)
78+
}
79+
80+
q := u.Query()
7681

77-
lastPage, err := strconv.Atoi(subs[len(subs)-1])
82+
lastPage, err := strconv.Atoi(q.Get("page"))
7883
if err != nil {
7984
log.Errorf("Unable to convert page substring to int, Error: %s", err)
8085
}
8186

8287
// add all pages to the slice of targets to return
8388
for page := 2; page <= lastPage; page++ {
84-
pageURL := fmt.Sprintf("%s&page=%v", url, page)
85-
paginated = append(paginated, pageURL)
89+
q.Set("page", strconv.Itoa(page))
90+
u.RawQuery = q.Encode()
91+
paginated = append(paginated, u.String())
8692
}
8793

8894
break

0 commit comments

Comments
 (0)