-
Notifications
You must be signed in to change notification settings - Fork 1
/
guide_articles.go
110 lines (93 loc) · 2.97 KB
/
guide_articles.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
package zendesk
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
)
type ArticleResponse struct {
Article Article `json:"article"`
}
type ArticlesResponse struct {
Articles []Article `json:"articles"`
CursorPaginationResponse
}
type Article struct {
AuthorID UserID `json:"author_id"`
Body string `json:"body"`
CommentsDisabled bool `json:"comments_disabled"`
ContentTagIds []any `json:"content_tag_ids"`
CreatedAt time.Time `json:"created_at"`
Draft bool `json:"draft"`
EditedAt time.Time `json:"edited_at"`
HTMLURL string `json:"html_url"`
ID ArticleID `json:"id"`
LabelNames []any `json:"label_names"`
Locale string `json:"locale"`
Name string `json:"name"`
Outdated bool `json:"outdated"`
PermissionGroupID PermissionGroupID `json:"permission_group_id"`
Position int64 `json:"position"`
Promoted bool `json:"promoted"`
SectionID SectionID `json:"section_id"`
SourceLocale string `json:"source_locale"`
Title string `json:"title"`
UpdatedAt time.Time `json:"updated_at"`
URL string `json:"url"`
UserSegmentID UserSegmentID `json:"user_segment_id"`
VoteCount uint64 `json:"vote_count"`
VoteSum int64 `json:"vote_sum"`
}
// https://developer.zendesk.com/api-reference/help_center/help-center-api/articles/
type ArticleService struct {
client *client
}
// https://developer.zendesk.com/api-reference/help_center/help-center-api/articles/#show-article
func (s ArticleService) Show(ctx context.Context, id ArticleID) (Article, error) {
target := ArticleResponse{}
request, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("/api/v2/help_center/articles/%d", id),
http.NoBody,
)
if err != nil {
return Article{}, err
}
if err := s.client.ZendeskRequest(request, &target); err != nil {
return Article{}, err
}
return target.Article, nil
}
// https://developer.zendesk.com/api-reference/help_center/help-center-api/articles/#list-articles
func (s ArticleService) List(
ctx context.Context,
pageHandler func(response ArticlesResponse) error,
) error {
query := url.Values{}
query.Set("page[size]", "100")
for {
target := ArticlesResponse{}
request, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
fmt.Sprintf("/api/v2/help_center/articles?%s", query.Encode()),
http.NoBody,
)
if err != nil {
return err
}
if err := s.client.ZendeskRequest(request, &target); err != nil {
return err
}
if err := pageHandler(target); err != nil {
return err
}
if !target.Meta.HasMore {
break
}
query.Set("page[after]", target.Meta.AfterCursor)
}
return nil
}