-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpages.go
135 lines (122 loc) · 3.75 KB
/
pages.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
package crowi
import (
"fmt"
"net/http"
"net/url"
"time"
"golang.org/x/net/context"
)
// PagesService handles communication with the Pages related
// methods of the Crowi API.
type PagesService service
// Create makes a page in your Crowi. The request requires
// the path and page content used for the page name
func (s *PagesService) Create(ctx context.Context, path, body string) (*Page, error) {
var page Page
params := url.Values{}
params.Set("access_token", s.client.config.Token)
params.Set("path", path)
params.Set("body", body)
err := s.client.newRequest(ctx, http.MethodPost, "/_api/pages.create", params, &page)
if err != nil {
return nil, err
}
return &page, nil
}
// Update updates the page content. A page_id is necessary to know which
// page should be updated.
func (s *PagesService) Update(ctx context.Context, id, body string) (*Page, error) {
var page Page
params := url.Values{}
params.Set("access_token", s.client.config.Token)
params.Set("page_id", id)
params.Set("body", body)
err := s.client.newRequest(ctx, http.MethodPost, "/_api/pages.update", params, &page)
if err != nil {
return nil, err
}
return &page, nil
}
// PagesListOptions specifies the optional parameters to the
// PagesService.List methods.
type PagesListOptions struct {
ListOptions
}
// List shows the list of pages. A parameter of path or user is required.
func (s *PagesService) List(ctx context.Context, path, user string, opt *PagesListOptions) (*Pages, error) {
var pages Pages
params := url.Values{}
params.Set("access_token", s.client.config.Token)
params.Set("path", path)
params.Set("user", user)
err := s.client.newRequest(ctx, http.MethodGet, "/_api/pages.list", params, &pages)
if err != nil {
return nil, err
}
if opt != nil && opt.ListOptions.Pagenation {
offset := 0
var p []PageInfo
for {
params.Set("offset", fmt.Sprintf("%d", offset))
err := s.client.newRequest(ctx, http.MethodGet, "/_api/pages.list", params, &pages)
if err != nil {
break
}
p = append(p, pages.Pages...)
offset += 50
}
pages.Pages = p
}
return &pages, nil
}
func (s *PagesService) Get(ctx context.Context, path string) (*Page, error) {
var page Page
params := url.Values{}
params.Set("access_token", s.client.config.Token)
params.Set("path", path)
err := s.client.newRequest(ctx, http.MethodGet, "/_api/pages.get", params, &page)
if err != nil {
return nil, err
}
return &page, nil
}
type Page struct {
Page PageInfo `json:"page"`
OK bool `json:"ok"`
Error string `json:"error"`
}
type PageInfo struct {
Revision PageRevision `json:"revision"`
V int `json:"__v"`
RedirectTo interface{} `json:"redirectTo"`
UpdatedAt time.Time `json:"updatedAt"`
LastUpdateUser interface{} `json:"lastUpdateUser"`
Creator interface{} `json:"creator"`
Path string `json:"path"`
ID string `json:"_id"`
CreatedAt time.Time `json:"createdAt"`
CommentCount int `json:"commentCount"`
SeenUsers []interface{} `json:"seenUsers"`
Liker []interface{} `json:"liker"`
GrantedUsers []string `json:"grantedUsers"`
Grant int `json:"grant"`
Status string `json:"status"`
Extended PageExtended `json:"extended,omitempty"`
}
type PageRevision struct {
ID string `json:"_id"`
Author interface{} `json:"author"`
Body string `json:"body"`
Path string `json:"path"`
V int `json:"__v"`
CreatedAt time.Time `json:"createdAt"`
Format string `json:"format"`
}
type PageExtended struct {
Slack string `json:"slack"`
}
type Pages struct {
Pages []PageInfo `json:"pages"`
OK bool `json:"ok"`
Error string `json:"error"`
}