-
Notifications
You must be signed in to change notification settings - Fork 4
/
article_label.go
152 lines (121 loc) · 3.44 KB
/
article_label.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
package zed
import "fmt"
// ArticleLabel struct
type ArticleLabel struct {
ID *float64 `json:"id,omitempty"`
URL *string `json:"url,omitempty"`
Name *string `json:"name,omitempty"`
CreatedAt *string `json:"created_at,omitempty"`
UpdatedAt *string `json:"updated_at,omitempty"`
}
// LabelResponse struct
type LabelResponse struct {
Label *ArticleLabel `json:"label,omitempty"`
}
// LabelCollectionResponse struct
type LabelCollectionResponse struct {
Results []ArticleLabel `json:"labels,omitempty"`
Count *int64 `json:"count,omitempty"`
Next *string `json:"next_page,omitempty"`
Page *int64 `json:"page,omitempty"`
PageCount *int64 `json:"page_count,omitempty"`
PerPage *int64 `json:"per_page,omitempty"`
Previous *string `json:"previous_page,omitempty"`
}
// LabelService struct
type LabelService struct {
client *Client
}
// Create func creates a single new article
func (s *LabelService) Create(id *int64, l *ArticleLabel) (*ArticleLabel, error) {
label := &ArticleLabel{}
if l.Name == nil {
return label, fmt.Errorf("missing required field: label name")
}
lw := &LabelResponse{Label: l}
url := fmt.Sprintf("help_center/articles/%v/labels.json", *id)
req, err := s.client.NewRequest("POST", url, lw)
if err != nil {
return label, err
}
result := LabelResponse{}
_, err = s.client.Do(req, result)
if err != nil {
return label, err
}
label = result.Label
return label, err
}
// List function lists labels used in all articles
func (s *LabelService) List() ([]ArticleLabel, error) {
resource := []ArticleLabel{}
rp, next, _, err := s.getPage("")
if err != nil {
return resource, err
}
resource = append(resource, rp...)
for next != nil {
rp, nx, _, err := s.getPage(*next)
if err != nil {
return resource, err
}
next = nx
resource = append(resource, rp...)
}
return resource, err
}
// Get function lists lablels used in an article with a given id
func (s *LabelService) Get(id *int64) ([]ArticleLabel, error) {
resource := []ArticleLabel{}
url := fmt.Sprintf("help_center/articles/%v/labels.json", *id)
rp, next, _, err := s.getPage(url)
if err != nil {
return resource, err
}
resource = append(resource, rp...)
for next != nil {
rp, nx, _, err := s.getPage(*next)
if err != nil {
return resource, err
}
next = nx
resource = append(resource, rp...)
}
return resource, err
}
func (s *LabelService) getPage(url string) ([]ArticleLabel, *string, *Response, error) {
if url == "" {
url = "help_center/articles/labels.json"
}
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, nil, err
}
result := LabelCollectionResponse{}
resp, err := s.client.Do(req, result)
if err != nil {
return nil, nil, resp, err
}
next := result.Next
resource := result.Results
return resource, next, resp, err
}
// Delete func deletes a single article
func (s *LabelService) Delete(articleID, id *int64) error {
if articleID == nil {
return fmt.Errorf("missing required field: article id")
}
if id == nil {
return fmt.Errorf("missing required field: id")
}
url := fmt.Sprintf("help_center/articles/%v/labels/%v.json", *articleID, *id)
req, err := s.client.NewRequest("DELETE", url, nil)
if err != nil {
return fmt.Errorf("creating new request failed: %v\n", err)
}
_, err = s.client.Do(req, nil)
if err != nil {
return fmt.Errorf("request failed with: %v\n", err)
}
return err
}