This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
post_lists.go
136 lines (122 loc) · 3.8 KB
/
post_lists.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
package tent
import (
"errors"
"net/http"
"net/url"
"strconv"
"strings"
)
type PageHeader struct {
ETag string
Count int
NotModified bool
}
type PageRequest struct {
ETag string
CountOnly bool
Limit int
}
type PostListPage struct {
Mentions []*PostMention `json:"mentions,omitempty"`
Versions []*PostVersion `json:"versions,omitempty"`
Posts []*Post `json:"posts,omitempty"`
Links PageLinks `json:"pages"`
Header PageHeader `json:"-"`
}
type PageLinks struct {
First string `json:"first,omitempty"`
Prev string `json:"prev,omitempty"`
Next string `json:"next,omitempty"`
Last string `json:"last,omitempty"`
accept string
baseURL string
client *Client
}
func (links *PageLinks) get(query string) (*PostListPage, error) {
if query == "" {
return nil, ErrNoPage
}
page := &PostListPage{Links: PageLinks{accept: links.accept, baseURL: links.baseURL, client: links.client}}
links.baseURL = strings.SplitN(links.baseURL, "?", 2)[0] + query
header := make(http.Header)
header.Set("Accept", links.accept)
_, err := links.client.requestJSONURL("GET", links.baseURL, header, nil, page)
if err != nil {
return nil, err
}
return page, nil
}
func (client *Client) GetFeed(q *PostsFeedQuery, r *PageRequest) (*PostListPage, error) {
return client.getPostListPage("", "", "", MediaTypePostsFeed, r, q.Values)
}
func (client *Client) GetVersions(entity, post string, r *PageRequest) (*PostListPage, error) {
return client.getPostListPage(entity, post, "", MediaTypePostVersions, r, nil)
}
func (client *Client) GetChildren(entity, post, version string, r *PageRequest) (*PostListPage, error) {
return client.getPostListPage(entity, post, version, MediaTypePostChildren, r, nil)
}
func (client *Client) GetMentions(entity, post string, r *PageRequest) (*PostListPage, error) {
return client.getPostListPage(entity, post, "", MediaTypePostMentions, r, nil)
}
var ErrNoPage = errors.New("tent: the requested page does not exist")
func (client *Client) getPostListPage(entity, post, version, mediaType string, r *PageRequest, query url.Values) (*PostListPage, error) {
header := make(http.Header)
header.Set("Accept", mediaType)
if r != nil && r.ETag != "" {
header.Set("If-None-Match", r.ETag)
}
limit := 0
if r != nil {
limit = r.Limit
}
page := &PostListPage{}
urlFunc := func(server *MetaPostServer) string {
var pu string
if mediaType == MediaTypePostsFeed {
pu = server.URLs.PostsFeed
} else {
pu = server.URLs.PostURL(entity, post, version)
}
page.Links.baseURL = pu
if len(query) > 0 || limit > 0 {
u, err := url.Parse(pu)
if err != nil {
return pu
}
uq := u.Query()
for k, v := range query {
uq[k] = v
}
if limit > 0 {
uq.Set("limit", strconv.Itoa(limit))
}
pu = appendQuery(pu, uq.Encode())
}
return pu
}
if r != nil && r.ETag != "" {
header.Set("If-None-Match", r.ETag)
}
if r != nil && r.CountOnly {
var err error
page.Header, err = client.requestCount(urlFunc, header)
return nil, err
}
resHeader, err := client.requestJSON("GET", urlFunc, header, nil, page)
if err != nil {
if resErr, ok := err.(*ResponseError); ok && resErr.Type == ErrBadStatusCode && resErr.Response.StatusCode == 304 {
page.Header.ETag = resHeader.Get("Etag")
page.Header.NotModified = true
return page, nil
}
return nil, err
}
page.Links.client = client
page.Links.accept = mediaType
page.Header.ETag = resHeader.Get("Etag")
return page, nil
}
func (f *PostListPage) First() (*PostListPage, error) { return f.Links.get(f.Links.First) }
func (f *PostListPage) Prev() (*PostListPage, error) { return f.Links.get(f.Links.Prev) }
func (f *PostListPage) Next() (*PostListPage, error) { return f.Links.get(f.Links.Next) }
func (f *PostListPage) Last() (*PostListPage, error) { return f.Links.get(f.Links.Last) }