-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
318 lines (279 loc) · 7.54 KB
/
client.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"context"
"fmt"
"net/url"
"time"
"github.com/izumin5210/hx"
"github.com/srvc/fail/v4"
"github.com/taxio/esa/log"
)
const (
BaseUrl = "https://api.esa.io/v1"
)
type Client struct {
client *hx.Client
}
func NewClient(token, team string) (*Client, error) {
baseUrl, err := url.Parse(hx.Path(BaseUrl, "teams", team) + "/")
if err != nil {
return nil, fail.Wrap(err)
}
return &Client{
client: hx.NewClient(
hx.BaseURL(baseUrl),
hx.Bearer(token),
),
}, nil
}
type GetPostsResponsePost struct {
Number int `json:"number"`
Name string `json:"name"`
FullName string `json:"full_name"`
Wip bool `json:"wip"`
BodyMd string `json:"body_md"`
Url string `json:"url"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Tags []string `json:"tags"`
}
type GetPostsResponse struct {
Posts []*GetPostsResponsePost `json:"posts"`
PrevPage int `json:"prev_page"`
NextPage int `json:"next_page"`
TotalCount int `json:"total_count"`
Page int `json:"page"`
PerPage int `json:"per_page"`
MaxPerPage int `json:"max_per_page"`
}
const MaxPostsPerPage = 100
func (c *Client) GetPosts(ctx context.Context, max int, sortKey SortKey) ([]*Post, error) {
log.Printf("GetPosts. max: %d, sortKey: %s\n", max, sortKey)
var posts []*Post
remain := max
page := 0
for remain > 0 {
perPage := minInt(remain, MaxPostsPerPage)
remain = remain - perPage
log.Printf("Get post. page: %d, per_page: %d, sort: %s\n", page, perPage, sortKey)
var res GetPostsResponse
err := c.client.Get(
ctx,
hx.Path("posts"),
hx.Query("page", fmt.Sprint(page)),
hx.Query("per_page", fmt.Sprint(perPage)),
hx.Query("sort", sortKey.String()),
hx.WhenSuccess(hx.AsJSON(&res)),
hx.WhenFailure(hx.AsError()),
)
if err != nil {
return nil, fail.Wrap(err)
}
for _, p := range res.Posts {
posts = append(posts, &Post{
Number: p.Number,
Name: p.Name,
FullName: p.FullName,
BodyMd: p.BodyMd,
CreatedAt: p.CreatedAt,
UpdatedAt: p.UpdatedAt,
Url: p.Url,
Tags: p.Tags,
})
}
if res.NextPage == 0 {
log.Println("there is no next page")
break
}
page = res.NextPage
}
return posts, nil
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
type UserInfo struct {
MySelf bool `json:"myself"`
Name string `json:"name"`
ScreenName string `json:"screen_name"`
Icon string `json:"icon"`
}
type GetPostResponse struct {
Number int `json:"number"`
Name string `json:"name"`
FullName string `json:"full_name"`
Wip bool `json:"wip"`
BodyMd string `json:"body_md"`
CreatedAt time.Time `json:"created_at"`
Url string `json:"url"`
UpdatedAt time.Time `json:"updated_at"`
Tags []string `json:"tags"`
Category string `json:"category"`
RevisionNumber int `json:"revision_number"`
CreatedBy *UserInfo `json:"created_by"`
UpdatedBy *UserInfo `json:"updated_by"`
}
func (c *Client) GetPost(ctx context.Context, postId int) (*Post, error) {
log.Println("Get Post Detail")
var res GetPostResponse
err := c.client.Get(
ctx,
hx.Path("posts", postId),
hx.WhenSuccess(hx.AsJSON(&res)),
hx.WhenFailure(hx.AsError()),
)
if err != nil {
return nil, fail.Wrap(err)
}
post := Post{
Number: res.Number,
Name: res.Name,
FullName: res.FullName,
CreatedAt: res.CreatedAt,
UpdatedAt: res.UpdatedAt,
Message: "",
Url: res.Url,
Tags: res.Tags,
Category: res.Category,
OriginalRevision: PostRevision{
BodyMd: res.BodyMd,
Number: res.RevisionNumber,
User: res.UpdatedBy.ScreenName,
},
}
return &post, nil
}
func (c *Client) GetTemplatePosts(ctx context.Context, max int, sortKey SortKey) ([]*Post, error) {
log.Printf("GetTemplatePosts. max: %d, sortKey: %s\n", max, sortKey)
var posts []*Post
remain := max
page := 0
for remain > 0 {
perPage := minInt(remain, MaxPostsPerPage)
remain = remain - perPage
log.Printf("Get post. page: %d, per_page: %d, sort: %s\n", page, perPage, sortKey)
var res GetPostsResponse
err := c.client.Get(
ctx,
hx.Path("posts"),
hx.Query("page", fmt.Sprint(page)),
hx.Query("per_page", fmt.Sprint(perPage)),
hx.Query("sort", sortKey.String()),
hx.Query("q", "category:Templates"),
hx.WhenSuccess(hx.AsJSON(&res)),
hx.WhenFailure(hx.AsError()),
)
if err != nil {
return nil, fail.Wrap(err)
}
for _, p := range res.Posts {
posts = append(posts, &Post{
Number: p.Number,
Name: p.Name,
FullName: p.FullName,
BodyMd: p.BodyMd,
CreatedAt: p.CreatedAt,
UpdatedAt: p.UpdatedAt,
Url: p.Url,
Tags: p.Tags,
})
}
if res.NextPage == 0 {
log.Println("there is no next page")
break
}
page = res.NextPage
}
return posts, nil
}
type PatchPostRequestOriginalRevision struct {
BodyMd string `json:"body_md"`
Number int `json:"number"`
User string `json:"user"`
}
type PatchPostRequest struct {
Name string `json:"name"`
BodyMd string `json:"body_md"`
Tags []string `json:"tags"`
Category string `json:"category"`
Wip bool `json:"wip"`
Message string `json:"message"`
OriginalRevision *PatchPostRequestOriginalRevision `json:"original_revision"`
}
func (c *Client) UpdatePost(ctx context.Context, post *Post) error {
log.Println("Update Post")
req := PatchPostRequest{
Name: post.Name,
BodyMd: post.BodyMd,
Tags: post.Tags,
Category: post.Category,
Wip: false,
Message: post.Message,
OriginalRevision: &PatchPostRequestOriginalRevision{
BodyMd: post.OriginalRevision.BodyMd,
Number: post.OriginalRevision.Number,
User: post.OriginalRevision.User,
},
}
err := c.client.Patch(
ctx,
hx.Path("posts", post.Number),
hx.JSON(req),
hx.WhenFailure(hx.AsError()),
)
if err != nil {
return fail.Wrap(err)
}
return nil
}
type NewPostRequest struct {
Post NewPostRequestPost `json:"post"`
}
type NewPostRequestPost struct {
Name string `json:"name,omitempty"`
BodyMd string `json:"body_md,omitempty"`
Tags []string `json:"tags,omitempty"`
Category string `json:"category,omitempty"`
Wip bool `json:"wip,omitempty"`
Message string `json:"message,omitempty"`
User string `json:"user,omitempty"`
TemplatePostId int `json:"template_post_id,omitempty"`
}
func (c *Client) CreatePostFromTemplate(ctx context.Context, templateId int) (*Post, error) {
req := NewPostRequest{
Post: NewPostRequestPost{
TemplatePostId: templateId,
},
}
var res GetPostResponse
err := c.client.Post(
ctx,
hx.Path("posts"),
hx.JSON(req),
hx.WhenSuccess(hx.AsJSON(&res)),
hx.WhenFailure(hx.AsError()),
)
if err != nil {
return nil, fail.Wrap(err)
}
post := Post{
Number: res.Number,
Name: res.Name,
FullName: res.FullName,
BodyMd: res.BodyMd,
CreatedAt: res.CreatedAt,
UpdatedAt: res.UpdatedAt,
Url: res.Url,
Tags: res.Tags,
Category: res.Category,
OriginalRevision: PostRevision{
BodyMd: res.BodyMd,
Number: res.Number,
User: res.UpdatedBy.Name,
},
}
return &post, nil
}