-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdoc.go
108 lines (99 loc) · 2.41 KB
/
doc.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
package yuqueg
import (
"errors"
"fmt"
)
// DocService encapsulate authenticated token
type DocService struct {
client *Client
}
// NewDoc create Doc for external use
func NewDoc(client *Client) *DocService {
return &DocService{
client: client,
}
}
// List doc of a repo
func (doc DocService) List(namespace string) (BookDetail, error) {
var b BookDetail
if len(namespace) == 0 {
return b, errors.New("repo namespace or id is required")
}
url := fmt.Sprintf("repos/%s/docs/", namespace)
_, err := doc.client.RequestObj(url, &b, EmptyRO)
if err != nil {
return b, err
}
return b, nil
}
// Get detail info of a doc
func (doc DocService) Get(namespace string, slug string, data *DocGet) (DocDetail, error) {
var b DocDetail
if len(namespace) == 0 {
return b, errors.New("repo namespace or id is required")
}
url := fmt.Sprintf("repos/%s/docs/%s", namespace, slug)
_, err := doc.client.RequestObj(url, &b, &RequestOption{
Data: StructToMapStr(data),
})
if err != nil {
return b, err
}
return b, nil
}
// Create doc
func (doc DocService) Create(namespace string, data *DocCreate) (DocDetail, error) {
var b DocDetail
if len(namespace) == 0 {
return b, errors.New("repo namespace or id is required")
}
if len(data.Format) == 0 {
data.Format = "markdown"
}
url := fmt.Sprintf("repos/%s/docs", namespace)
_, err := doc.client.RequestObj(url, &b, &RequestOption{
Method: "POST",
Data: StructToMapStr(data),
})
if err != nil {
return b, err
}
return b, nil
}
// Update doc
func (doc DocService) Update(namespace string, id string, data *DocCreate) (DocDetail, error) {
var b DocDetail
if len(namespace) == 0 {
return b, errors.New("repo namespace or id is required")
}
if len(id) == 0 {
return b, errors.New("doc id is required")
}
url := fmt.Sprintf("repos/%s/docs/%s", namespace, id)
_, err := doc.client.RequestObj(url, &b, &RequestOption{
Method: "PUT",
Data: StructToMapStr(data),
})
if err != nil {
return b, err
}
return b, nil
}
// Delete doc
func (doc DocService) Delete(namespace string, id string) (DocDetail, error) {
var b DocDetail
if len(namespace) == 0 {
return b, errors.New("repo namespace or id is required")
}
if len(id) == 0 {
return b, errors.New("doc id is required")
}
url := fmt.Sprintf("repos/%s/docs/%s", namespace, id)
_, err := doc.client.RequestObj(url, &b, &RequestOption{
Method: "DELETE",
})
if err != nil {
return b, err
}
return b, nil
}