-
Notifications
You must be signed in to change notification settings - Fork 39
/
pagination.go
52 lines (43 loc) · 1.31 KB
/
pagination.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
package sentry
import (
"strconv"
"strings"
)
// Page is a link and if it has results or not
type Page struct {
URL string
Results bool
}
// Link represents a link object as per: https://docs.sentry.io/api/pagination/
type Link struct {
Previous Page
Next Page
}
// NewLink creates a new link object via the link header string
func NewLink(linkheader string) *Link {
link := &Link{}
links := strings.SplitN(linkheader, ",", 2)
for _, page := range links {
data := strings.SplitN(page, ";", 4)
pagelink := strings.TrimLeft(strings.TrimSpace(data[0]), "<")
pagelink = strings.TrimRight(pagelink, ">")
pagetype := strings.Trim(strings.Split(data[1], "=")[1], `"`)
results, err := strconv.ParseBool(strings.Trim(strings.Split(strings.TrimSpace(data[2]), "=")[1], `"`))
if err != nil {
results = false
}
if pagetype == "previous" {
link.Previous.URL = pagelink
link.Previous.Results = results
} else {
link.Next.URL = pagelink
link.Next.Results = results
}
}
return link
}
// GetPage will fetch a page via the Link object and decode it from out.
// Should be used like `client.GetPage(link.Previous, make([]Organization, 0))`
func (c *Client) GetPage(p Page, out interface{}) (*Link, error) {
return c.rawWithPagination("GET", strings.TrimPrefix(p.URL, c.Endpoint), out, nil)
}