-
Notifications
You must be signed in to change notification settings - Fork 11
/
paginate.go
58 lines (45 loc) · 1.25 KB
/
paginate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package replicate
import (
"context"
"encoding/json"
"net/http"
)
// Page represents a paginated response from Replicate's API.
type Page[T any] struct {
Previous *string `json:"previous,omitempty"`
Next *string `json:"next,omitempty"`
Results []T `json:"results"`
rawJSON json.RawMessage `json:"-"`
}
func (p *Page[T]) RawJSON() json.RawMessage {
return p.rawJSON
}
var _ json.Unmarshaler = (*Page[Prediction])(nil)
func (p *Page[T]) UnmarshalJSON(data []byte) error {
p.rawJSON = data
type Alias Page[T]
alias := &struct{ *Alias }{Alias: (*Alias)(p)}
return json.Unmarshal(data, alias)
}
// Paginate takes a Page and the Client request method, and iterates through pages of results.
func Paginate[T any](ctx context.Context, client *Client, initialPage *Page[T]) (<-chan []T, <-chan error) {
resultsChan := make(chan []T)
errChan := make(chan error)
go func() {
defer close(resultsChan)
defer close(errChan)
resultsChan <- initialPage.Results
nextURL := initialPage.Next
for nextURL != nil {
page := &Page[T]{}
err := client.fetch(ctx, http.MethodGet, *nextURL, nil, page)
if err != nil {
errChan <- err
return
}
resultsChan <- page.Results
nextURL = page.Next
}
}()
return resultsChan, errChan
}