forked from huandu/facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paging_result.go
146 lines (116 loc) · 2.6 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
// A facebook graph api client in go.
// https://github.com/huandu/facebook/
//
// Copyright 2012 - 2014, Huan Du
// Licensed under the MIT license
// https://github.com/huandu/facebook/blob/master/LICENSE
package facebook
import (
"bytes"
"fmt"
"net/http"
)
type pagingData struct {
Data []Result `facebook:",required"`
Paging *pagingNavigator
}
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("current Result is not a paging response.")
}
pr := &PagingResult{
session: session,
}
paging := &pr.paging
err := res.Decode(paging)
if err != nil {
return nil, err
}
if paging.Paging != nil {
pr.previous = paging.Paging.Previous
pr.next = paging.Paging.Next
}
return pr, nil
}
// Get current data.
func (pr *PagingResult) Data() []Result {
return pr.paging.Data
}
// 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)
}
// Read previous page.
func (pr *PagingResult) Previous() (noMore bool, err error) {
if !pr.HasPrevious() {
noMore = true
return
}
return pr.navigate(&pr.previous)
}
// Read next page.
func (pr *PagingResult) Next() (noMore bool, err error) {
if !pr.HasNext() {
noMore = true
return
}
return pr.navigate(&pr.next)
}
// Check whether there is previous page.
func (pr *PagingResult) HasPrevious() bool {
return pr.previous != ""
}
// Check 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)
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
}
if paging.Paging == nil || len(paging.Data) == 0 {
*url = ""
noMore = true
} else {
pr.previous = paging.Paging.Previous
pr.next = paging.Paging.Next
}
return
}