This repository was archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaginator.go
108 lines (95 loc) · 2.15 KB
/
paginator.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package rego
import (
"encoding/json"
"fmt"
"log"
"net/url"
)
const (
// MaxLimit is the upper maximum limit for Session.Listing() requests
MaxLimit = 100
)
// Paginator is the interface that wraps methods for pagination of the Listing type
type Paginator interface {
Next() (Lister, error)
Previous() (Lister, error)
SetLimit(int)
}
type Page struct {
s *Session
url string
after string // Fullname of reference Thing
before string // Fullname of reference Thing
limit int // Limit of items returned
}
// Next returns a set of Thing items resulting from the requested API call.
//
// Consecutive calls will return subsequent items indefinitely.
func (p *Page) Next() (*Listing, error) {
v := p.values()
if len(p.before) > 0 {
v.Set("before", p.before)
}
resp, err := p.list(v)
if err != nil {
return nil, err
}
return resp, err
}
// Previous returns a set of Thing items resulting from the requested API call.
//
// Consecutive calls will return preceding items until source is exhausted.
func (p *Page) Previous() (*Listing, error) {
v := p.values()
if len(p.after) > 0 {
v.Set("after", p.after)
}
resp, err := p.list(v)
if err != nil {
return nil, err
}
return resp, err
}
// SetLimit sets the max number of links returned from calls to Previous and Next
func (p *Page) SetLimit(limit int) {
if limit < 0 {
limit = 0
}
if limit > MaxLimit {
limit = MaxLimit
}
p.limit = limit
}
func (p *Page) list(v url.Values) (*Listing, error) {
resp, err := p.s.get(p.url, v)
if err != nil {
return nil, err
}
defer resp.Body.Close()
list := Listing{}
err = json.NewDecoder(resp.Body).Decode(&list)
if err != nil {
return nil, err
}
if list.Kind != TypeListing {
// TODO.. handle error
log.Panic("Bricks!")
}
if len(list.Data.Children) > 0 {
item := struct {
Name string
}{}
err = json.Unmarshal(list.Data.Children[0].Data, &item)
p.before = item.Name
err = json.Unmarshal(list.Data.Children[len(list.Data.Children)-1].Data, &item)
p.after = item.Name
}
return &list, nil
}
func (p *Page) values() url.Values {
v := url.Values{}
if p.limit > 0 {
v.Set("limit", fmt.Sprintf("%d", p.limit))
}
return v
}