forked from preichenberger/go-coinbasepro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor.go
55 lines (45 loc) · 1.04 KB
/
cursor.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
package coinbasepro
import (
"fmt"
)
type Cursor struct {
Client *Client
Pagination *PaginationParams
Method string
Params interface{}
URL string
HasMore bool
}
func NewCursor(client *Client, method, url string,
paginationParams *PaginationParams) *Cursor {
return &Cursor{
Client: client,
Method: method,
URL: url,
Pagination: paginationParams,
HasMore: true,
}
}
func (c *Cursor) Page(i interface{}, direction string) error {
url := c.URL
if c.Pagination.Encode(direction) != "" {
url = fmt.Sprintf("%s?%s", c.URL, c.Pagination.Encode(direction))
}
res, err := c.Client.Request(c.Method, url, nil, c.Params, i)
if err != nil {
c.HasMore = false
return err
}
c.Pagination.Before = res.Header.Get("CB-BEFORE")
c.Pagination.After = res.Header.Get("CB-AFTER")
if c.Pagination.Done(direction) {
c.HasMore = false
}
return nil
}
func (c *Cursor) NextPage(i interface{}) error {
return c.Page(i, "next")
}
func (c *Cursor) PrevPage(i interface{}) error {
return c.Page(i, "prev")
}