forked from Virtomize/confluence-go-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.go
183 lines (163 loc) · 4.94 KB
/
content.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
171
172
173
174
175
176
177
178
179
180
181
182
183
package goconfluence
import (
"net/url"
"strconv"
"strings"
)
// getContentIDEndpoint creates the correct api endpoint by given id
func (a *API) getContentIDEndpoint(id string) (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/content/" + id)
}
// getContentEndpoint creates the correct api endpoint
func (a *API) getContentEndpoint() (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/content/")
}
// getContentChildEndpoint creates the correct api endpoint by given id and type
func (a *API) getContentChildEndpoint(id string, t string) (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/content/" + id + "/child/" + t)
}
// getContentGenericEndpoint creates the correct api endpoint by given id and type
func (a *API) getContentGenericEndpoint(id string, t string) (*url.URL, error) {
return url.ParseRequestURI(a.endPoint.String() + "/content/" + id + "/" + t)
}
// GetContentByID querys content by id
func (a *API) GetContentByID(id string, query ContentQuery) (*Content, error) {
ep, err := a.getContentIDEndpoint(id)
if err != nil {
return nil, err
}
ep.RawQuery = addContentQueryParams(query).Encode()
return a.SendContentRequest(ep, "GET", nil)
}
// GetContent querys content using a query parameters
func (a *API) GetContent(query ContentQuery) (*Search, error) {
ep, err := a.getContentEndpoint()
if err != nil {
return nil, err
}
ep.RawQuery = addContentQueryParams(query).Encode()
return a.SendSearchRequest(ep, "GET")
}
// GetChildPages returns a content list of child page objects
func (a *API) GetChildPages(id string) (*Search, error) {
ep, err := a.getContentChildEndpoint(id, "page")
if err != nil {
return nil, err
}
return a.SendSearchRequest(ep, "GET")
}
// GetComments returns a list of comments belonging to id
func (a *API) GetComments(id string) (*Search, error) {
ep, err := a.getContentChildEndpoint(id, "comment")
if err != nil {
return nil, err
}
return a.SendSearchRequest(ep, "GET")
}
// GetAttachments returns a list of attachments belonging to id
func (a *API) GetAttachments(id string) (*Search, error) {
ep, err := a.getContentChildEndpoint(id, "attachment")
if err != nil {
return nil, err
}
return a.SendSearchRequest(ep, "GET")
}
// GetHistory returns history information
func (a *API) GetHistory(id string) (*History, error) {
ep, err := a.getContentGenericEndpoint(id, "history")
if err != nil {
return nil, err
}
return a.SendHistoryRequest(ep, "GET")
}
// GetLabels returns a list of labels attachted to a content object
func (a *API) GetLabels(id string) (*Labels, error) {
ep, err := a.getContentGenericEndpoint(id, "label")
if err != nil {
return nil, err
}
return a.SendLabelRequest(ep, "GET", nil)
}
// AddLabels returns adds labels
func (a *API) AddLabels(id string, labels *[]Label) (*Labels, error) {
ep, err := a.getContentGenericEndpoint(id, "label")
if err != nil {
return nil, err
}
return a.SendLabelRequest(ep, "POST", labels)
}
// DeleteLabel removes a label by name from content identified by id
func (a *API) DeleteLabel(id string, name string) (*Labels, error) {
ep, err := a.getContentGenericEndpoint(id, "label/"+name)
if err != nil {
return nil, err
}
return a.SendLabelRequest(ep, "DELETE", nil)
}
// GetWatchers returns a list of watchers
func (a *API) GetWatchers(id string) (*Watchers, error) {
ep, err := a.getContentGenericEndpoint(id, "notification/child-created")
if err != nil {
return nil, err
}
return a.SendWatcherRequest(ep, "GET")
}
// CreateContent creates content
func (a *API) CreateContent(c *Content) (*Content, error) {
ep, err := a.getContentEndpoint()
if err != nil {
return nil, err
}
return a.SendContentRequest(ep, "POST", c)
}
// UpdateContent updates content
func (a *API) UpdateContent(c *Content) (*Content, error) {
ep, err := a.getContentIDEndpoint(c.ID)
if err != nil {
return nil, err
}
return a.SendContentRequest(ep, "PUT", c)
}
// DelContent deletes content by id
func (a *API) DelContent(id string) (*Content, error) {
ep, err := a.getContentIDEndpoint(id)
if err != nil {
return nil, err
}
return a.SendContentRequest(ep, "DELETE", nil)
}
// addContentQueryParams adds the defined query parameters
func addContentQueryParams(query ContentQuery) *url.Values {
data := url.Values{}
if len(query.Expand) != 0 {
data.Set("expand", strings.Join(query.Expand, ","))
}
if query.Limit != 0 {
data.Set("limit", strconv.Itoa(query.Limit))
}
if query.OrderBy != "" {
data.Set("orderby", query.OrderBy)
}
if query.PostingDay != "" {
data.Set("postingDay", query.PostingDay)
}
if query.SpaceKey != "" {
data.Set("spaceKey", query.SpaceKey)
}
if query.Start != 0 {
data.Set("start", strconv.Itoa(query.Start))
}
if query.Status != "" {
data.Set("status", query.Status)
}
if query.Title != "" {
data.Set("title", query.Title)
}
if query.Trigger != "" {
data.Set("trigger", query.Trigger)
}
if query.Type != "" {
data.Set("type", query.Type)
}
return &data
}