From 7eec0c47a0cf4503a8d1deae056a0e6cf6340262 Mon Sep 17 00:00:00 2001 From: LNCracker Date: Tue, 11 Aug 2026 17:38:01 +0700 Subject: [PATCH] fix: rely on Link header for API pagination instead of item count --- api/client.go | 26 ++++++++++++++++++++++++++ api/paginator.go | 21 +++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 api/client.go create mode 100644 api/paginator.go 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 "" +}