-
Notifications
You must be signed in to change notification settings - Fork 27
/
content.go
98 lines (82 loc) · 2.02 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
package confluence
import (
"encoding/json"
"net/http"
"net/url"
"strings"
)
type Content struct {
Id string `json:"id"`
Type string `json:"type"`
Status string `json:"status"`
Title string `json:"title"`
Body struct {
Storage struct {
Value string `json:"value"`
Representation string `json:"representation"`
} `json:"storage"`
} `json:"body"`
Version struct {
Number int `json:"number"`
} `json:"version"`
}
func (w *Wiki) contentEndpoint(contentID string) (*url.URL, error) {
return url.ParseRequestURI(w.endPoint.String() + "/content/" + contentID)
}
func (w *Wiki) DeleteContent(contentID string) error {
contentEndPoint, err := w.contentEndpoint(contentID)
if err != nil {
return err
}
req, err := http.NewRequest("DELETE", contentEndPoint.String(), nil)
if err != nil {
return err
}
_, err = w.sendRequest(req)
if err != nil {
return err
}
return nil
}
func (w *Wiki) GetContent(contentID string, expand []string) (*Content, error) {
contentEndPoint, err := w.contentEndpoint(contentID)
if err != nil {
return nil, err
}
data := url.Values{}
data.Set("expand", strings.Join(expand, ","))
contentEndPoint.RawQuery = data.Encode()
req, err := http.NewRequest("GET", contentEndPoint.String(), nil)
if err != nil {
return nil, err
}
res, err := w.sendRequest(req)
if err != nil {
return nil, err
}
var content Content
err = json.Unmarshal(res, &content)
if err != nil {
return nil, err
}
return &content, nil
}
func (w *Wiki) UpdateContent(content *Content) (*Content, error) {
jsonbody, err := json.Marshal(content)
if err != nil {
return nil, err
}
contentEndPoint, err := w.contentEndpoint(content.Id)
req, err := http.NewRequest("PUT", contentEndPoint.String(), strings.NewReader(string(jsonbody)))
req.Header.Add("Content-Type", "application/json")
res, err := w.sendRequest(req)
if err != nil {
return nil, err
}
var newContent Content
err = json.Unmarshal(res, &newContent)
if err != nil {
return nil, err
}
return &newContent, nil
}