-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.go
More file actions
264 lines (238 loc) · 7.23 KB
/
posts.go
File metadata and controls
264 lines (238 loc) · 7.23 KB
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
package docbase
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
type (
PostID int
Scope string
)
func (p PostID) Int() int {
return int(p)
}
const (
ScopeEveryone Scope = "everyone"
ScopeGroup Scope = "group"
ScopePrivate Scope = "private"
)
func ParsePostID(s string) (PostID, error) {
i, err := strconv.ParseInt(s, 10, 0)
if err != nil {
return 0, err
}
return PostID(i), nil
}
func (p PostID) String() string {
return fmt.Sprintf("%d", p)
}
type Post struct {
ID PostID `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Draft bool `json:"draft"`
Archived bool `json:"archived"`
URL string `json:"url"`
CreatedAt string `json:"created_at"` // ISO 8601
UpdatedAt string `json:"updated_at"` // ISO 8601
Scope Scope `json:"scope"`
SharingURL string `json:"sharing_url"`
Tags []Tag `json:"tags"`
User User `json:"user"`
Stars int `json:"stars_count"`
GoodJob int `json:"good_jobs_count"`
Comments []interface{} `json:"comments"`
Groups []interface{} `json:"groups"`
}
type Meta struct {
PreviousPageURL string `json:"previous_page"`
NextPageURL string `json:"next_page"`
Total int `json:"total"`
}
func ListPosts(ctx context.Context, domain string, param url.Values) ([]Post, *Meta, error) {
return defaultClient.ListPosts(ctx, domain, param)
}
func (c *Client) ListPosts(ctx context.Context, domain string, param url.Values) ([]Post, *Meta, error) {
req, err := c.NewRequest(ctx, http.MethodGet, buildURL("teams", domain, "posts"), nil, ¶m)
if err != nil {
return nil, nil, err
}
resp, err := c.Client.Do(req)
if err != nil {
return nil, nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("%d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
data := struct {
Posts []Post `json:"posts"`
Meta *Meta `json:"meta"`
}{}
b, err := ioutil.ReadAll(resp.Body) // TOOD(micheam): change to io.ReadAll
if err != nil {
return nil, nil, err
}
err = json.Unmarshal(b, &data)
if err != nil {
return nil, nil, err
}
return data.Posts, data.Meta, nil
}
func GetPost(ctx context.Context, domain string, id PostID) (*Post, error) {
return defaultClient.GetPost(ctx, domain, id)
}
func (c *Client) GetPost(ctx context.Context, domain string, id PostID) (*Post, error) {
req, err := c.NewRequest(ctx, http.MethodGet, buildURL("teams", domain, "posts", fmt.Sprint(id)), nil, nil)
if err != nil {
return nil, err
}
resp, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
post := new(Post)
b, err := ioutil.ReadAll(resp.Body) // TOOD(micheam): change to io.ReadAll
if err != nil {
return nil, err
}
err = json.Unmarshal(b, post)
if err != nil {
return nil, err
}
return post, nil
}
type PostOption struct {
Draft *bool `json:"draft,omitempty"`
Notice *bool `json:"notice,omitempty"`
Tags []string `json:"tags"`
Scope string `json:"scope,omitempty"` // TODO(micheam): 指定可能な値を明示する everyon (default), group, private
Groups []int `json:"groups"` // require on scope:groups
}
func NewPost(ctx context.Context, domain string, title string, body io.Reader, option PostOption) (*Post, error) {
return defaultClient.NewPost(ctx, domain, title, body, option)
}
func (c *Client) NewPost(ctx context.Context, domain string, title string, body io.Reader, option PostOption) (*Post, error) {
if domain == "" {
return nil, errors.New("`domain` must not be empty")
}
if title == "" {
return nil, errors.New("`title` must not be empty")
}
var _body = new(bytes.Buffer)
{
b, err := ioutil.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("failed to read body: %w", err)
}
type RequestBody struct {
Title string `json:"title"`
Body string `json:"body"`
PostOption
}
bb, err := json.Marshal(RequestBody{title, string(b), option})
if err != nil {
return nil, fmt.Errorf("failed to marshal body: %w", err)
}
_body = bytes.NewBuffer(bb)
}
req, err := c.NewRequest(ctx, http.MethodPost, buildURL("teams", domain, "posts"), _body, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
resp, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to do http reqest: %w", err)
}
defer resp.Body.Close()
if 300 <= resp.StatusCode {
// TODO(micheam): handle error object.
// エラーの詳細情報がBodyで返却されるので、ちゃんと扱う
return nil, fmt.Errorf("docbase api returns NG: %s", resp.Status)
}
var (
created = new(Post)
bytes = []byte{}
)
if bytes, err = ioutil.ReadAll(resp.Body); err != nil {
return nil, fmt.Errorf("failed to read Response body: %w", err)
}
if err := json.Unmarshal(bytes, created); err != nil {
return nil, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return created, nil
}
// UpdateFields は、更新対象のフィールドを保持します。
type UpdateFields struct {
Title *string `json:"title,omitempty"`
Draft *bool `json:"draft,omitempty"`
Notice *bool `json:"notice,omitempty"`
Tags *[]string `json:"tags,omitempty"`
Scope *string `json:"scope,omitempty"`
Groups *[]int `json:"groups,omitempty"`
}
func UpdatePost(ctx context.Context, domain string, id PostID, body io.Reader, fields UpdateFields) (*Post, error) {
return defaultClient.UpdatePost(ctx, domain, id, body, fields)
}
func (c *Client) UpdatePost(ctx context.Context, domain string, id PostID, body io.Reader, fields UpdateFields) (*Post, error) {
if domain == "" {
return nil, errors.New("`domain` must not be empty")
}
var _body *bytes.Buffer
{
rb := struct {
Body *string `json:"body,omitempty"`
UpdateFields
}{
UpdateFields: fields,
}
if body != nil {
b, err := ioutil.ReadAll(body)
if err != nil {
return nil, fmt.Errorf("failed to read body: %w", err)
}
rb.Body = stringPtr(string(b))
}
bb, err := json.Marshal(rb)
if err != nil {
return nil, fmt.Errorf("failed to marshal body: %w", err)
}
_body = bytes.NewBuffer(bb)
}
req, err := c.NewRequest(ctx, http.MethodPatch, buildURL("teams", domain, "posts", id.String()), _body, nil)
if err != nil {
return nil, fmt.Errorf("failed to patch request: %w", err)
}
resp, err := c.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to do http reqest: %w", err)
}
defer resp.Body.Close()
if 300 <= resp.StatusCode {
// TODO(micheam): handle error object.
// エラーの詳細情報がBodyで返却されるので、ちゃんと扱う
return nil, fmt.Errorf("docbase api returns NG: %s", resp.Status)
}
var (
created = new(Post)
bytes = []byte{}
)
if bytes, err = ioutil.ReadAll(resp.Body); err != nil {
return nil, fmt.Errorf("failed to read Response body: %w", err)
}
if err := json.Unmarshal(bytes, created); err != nil {
return nil, fmt.Errorf("failed to unmarshal response body: %w", err)
}
return created, nil
}