diff --git a/api/client.go b/api/client.go new file mode 100644 index 0000000..5fbbbc2 --- /dev/null +++ b/api/client.go @@ -0,0 +1,26 @@ +package api + +import ( + "net/http" +) + +// FetchAllPages demonstrates the correct pagination loop logic. +func FetchAllPages(client *http.Client, initialURL string, limit int) ([]interface{}, error) { + var allResults []interface{} + currentURL := initialURL + + for currentURL != "" && (limit <= 0 || len(allResults) < limit) { + req, _ := http.NewRequest("GET", currentURL, nil) + resp, err := client.Do(req) + if err != nil { + return nil, err + } + + // ... logic to decode results ... + // results := decode(resp) + // allResults = append(allResults, results...) + + currentURL = GetNextPageURL(resp.Header) + } + return allResults, nil +} diff --git a/api/paginator.go b/api/paginator.go new file mode 100644 index 0000000..7e1b727 --- /dev/null +++ b/api/paginator.go @@ -0,0 +1,21 @@ +package api + +import ( + "net/http" + "regexp" +) + +var nextLinkRegex = regexp.MustCompile(`<(.*?)>; rel="next"`) + +// GetNextPageURL extracts the next page URL from the Link header. +func GetNextPageURL(header http.Header) string { + link := header.Get("Link") + if link == "" { + return "" + } + matches := nextLinkRegex.FindStringSubmatch(link) + if len(matches) > 1 { + return matches[1] + } + return "" +}