-
Notifications
You must be signed in to change notification settings - Fork 514
/
paging_result.go
170 lines (134 loc) · 3.26 KB
/
paging_result.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// A facebook graph api client in go.
// https://github.com/huandu/facebook/
//
// Copyright 2012, Huan Du
// Licensed under the MIT license
// https://github.com/huandu/facebook/blob/master/LICENSE
package facebook
import (
"bytes"
"fmt"
"net/http"
)
// PagingResult represents facebook API call result with paging information.
type PagingResult struct {
session *Session
paging pagingData
previous string
next string
}
type pagingData struct {
Data []Result `facebook:",required"`
Paging *pagingNavigator
UsageInfo *UsageInfo
}
type pagingNavigator struct {
Previous string
Next string
}
func newPagingResult(session *Session, res Result) (*PagingResult, error) {
// quick check whether Result is a paging response.
if _, ok := res["data"]; !ok {
return nil, fmt.Errorf("facebook: current Result is not a paging response")
}
pr := &PagingResult{
session: session,
}
paging := &pr.paging
err := res.Decode(paging)
if err != nil {
return nil, err
}
paging.UsageInfo = res.UsageInfo()
if paging.Paging != nil {
pr.previous = paging.Paging.Previous
pr.next = paging.Paging.Next
}
return pr, nil
}
// Data gets current data.
func (pr *PagingResult) Data() []Result {
return pr.paging.Data
}
// UsageInfo returns API usage information, including
// business use case, app, page, ad account rate limiting.
func (pr *PagingResult) UsageInfo() *UsageInfo {
return pr.paging.UsageInfo
}
// Decode decodes the current full result to a struct. See Result#Decode.
func (pr *PagingResult) Decode(v interface{}) (err error) {
res := Result{
"data": pr.Data(),
}
return res.Decode(v)
}
// Previous reads previous page.
func (pr *PagingResult) Previous() (noMore bool, err error) {
if !pr.HasPrevious() {
noMore = true
return
}
return pr.navigate(&pr.previous)
}
// Next reads next page.
func (pr *PagingResult) Next() (noMore bool, err error) {
if !pr.HasNext() {
noMore = true
return
}
return pr.navigate(&pr.next)
}
// HasPrevious checks whether there is previous page.
func (pr *PagingResult) HasPrevious() bool {
return pr.previous != ""
}
// HasNext checks whether there is next page.
func (pr *PagingResult) HasNext() bool {
return pr.next != ""
}
func (pr *PagingResult) navigate(url *string) (noMore bool, err error) {
var pagingURL string
// add session information in paging url.
params := Params{}
pr.session.prepareParams(params)
// Per #182, access_token is always useless.
// As we may need to keep other params, do a manual delete here.
delete(params, "access_token")
if len(params) == 0 {
pagingURL = *url
} else {
buf := &bytes.Buffer{}
buf.WriteString(*url)
buf.WriteRune('&')
params.Encode(buf)
pagingURL = buf.String()
}
var request *http.Request
var res Result
request, err = http.NewRequest("GET", pagingURL, nil)
if err != nil {
return
}
res, err = pr.session.Request(request)
if err != nil {
return
}
if pr.paging.Paging != nil {
pr.paging.Paging.Next = ""
pr.paging.Paging.Previous = ""
}
paging := &pr.paging
err = res.Decode(paging)
if err != nil {
return
}
paging.UsageInfo = res.UsageInfo()
if paging.Paging == nil || len(paging.Data) == 0 {
*url = ""
noMore = true
} else {
pr.previous = paging.Paging.Previous
pr.next = paging.Paging.Next
}
return
}