-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
336 lines (282 loc) · 7.64 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package backlog
import (
"bytes"
"encoding/json"
"errors"
"io"
"mime/multipart"
"net/http"
"net/url"
"path"
"strings"
)
const (
apiVersion = "v2"
)
// ClinetError is a description of a Backlog API client error.
type ClinetError struct {
msg string
}
func (e *ClinetError) Error() string {
return e.msg
}
func newClientError(msg string) *ClinetError {
return &ClinetError{msg: msg}
}
// Client is Backlog API client.
type Client struct {
url *url.URL
httpClient *http.Client
token string
wrapper *wrapper
Issue *IssueService
Project *ProjectService
PullRequest *PullRequestService
Space *SpaceService
User *UserService
Wiki *WikiService
}
// FormParams wraps url.Values.
type FormParams struct {
*url.Values
}
// NewFormParams returns new FormParams.
func NewFormParams() *FormParams {
return &FormParams{&url.Values{}}
}
// NewReader converts FormParams to io.Reader.
func (p *FormParams) NewReader() io.Reader {
return strings.NewReader(p.Encode())
}
// QueryParams is query parameters for request.
type QueryParams struct {
*url.Values
}
// NewQueryParams returns new QueryParams.
func NewQueryParams() *QueryParams {
return &QueryParams{&url.Values{}}
}
// withOptions sets request query parameters from options.
func (p *QueryParams) withOptions(options []*QueryOption, validOptions ...queryType) error {
for _, option := range options {
if err := option.validate(validOptions); err != nil {
return err
}
}
for _, option := range options {
if err := option.set(p); err != nil {
return err
}
}
return nil
}
type clientGet func(spath string, query *QueryParams) (*http.Response, error)
type clientPost func(spath string, form *FormParams) (*http.Response, error)
type clientPatch func(spath string, form *FormParams) (*http.Response, error)
type clientDelete func(spath string, form *FormParams) (*http.Response, error)
type clientUpload func(spath, fileName string, r io.Reader) (*http.Response, error)
type method struct {
Get clientGet
Post clientPost
Patch clientPatch
Delete clientDelete
Upload clientUpload
}
type wrapper struct {
CreateFormFile func(w *multipart.Writer, fname string) (io.Writer, error)
Copy func(dst io.Writer, src io.Reader) error
}
func createFormFile(w *multipart.Writer, fname string) (io.Writer, error) {
return w.CreateFormFile("file", fname)
}
func copy(dst io.Writer, src io.Reader) error {
_, err := io.Copy(dst, src)
return err
}
// NewClient creates a new Backlog API Client.
func NewClient(baseURL, token string) (*Client, error) {
if len(token) == 0 {
return nil, newClientError("missing token")
}
parsedURL, err := url.ParseRequestURI(baseURL)
if err != nil {
return nil, err
}
c := &Client{
url: parsedURL,
httpClient: http.DefaultClient,
token: token,
wrapper: &wrapper{
CreateFormFile: createFormFile,
Copy: copy,
},
}
m := &method{
Get: func(spath string, query *QueryParams) (*http.Response, error) {
return c.get(spath, query)
},
Post: func(spath string, form *FormParams) (*http.Response, error) {
return c.post(spath, form)
},
Patch: func(spath string, form *FormParams) (*http.Response, error) {
return c.patch(spath, form)
},
Delete: func(spath string, form *FormParams) (*http.Response, error) {
return c.delete(spath, form)
},
Upload: func(spath, fileName string, r io.Reader) (*http.Response, error) {
return c.upload(spath, fileName, r)
},
}
activityOptionService := &ActivityOptionService{}
c.Issue = &IssueService{
method: m,
Attachment: &IssueAttachmentService{
method: m,
},
}
c.Project = &ProjectService{
method: m,
Activity: &ProjectActivityService{
method: m,
Option: activityOptionService,
},
User: &ProjectUserService{
method: m,
},
Option: &ProjectOptionService{},
}
c.PullRequest = &PullRequestService{
method: m,
Attachment: &PullRequestAttachmentService{
method: m,
},
}
c.Space = &SpaceService{
method: m,
Activity: &SpaceActivityService{
method: m,
Option: activityOptionService,
},
Attachment: &SpaceAttachmentService{
method: m,
},
}
c.User = &UserService{
method: m,
Activity: &UserActivityService{
method: m,
Option: activityOptionService,
},
Option: &UserOptionService{},
}
c.Wiki = &WikiService{
method: m,
Attachment: &WikiAttachmentService{
method: m,
},
Option: &WikiOptionService{},
}
return c, nil
}
// Creates new request.
func (c *Client) newReqest(method, spath string, header http.Header, body io.Reader, query *QueryParams) (*http.Request, error) {
if spath == "" {
return nil, errors.New("spath must not be empty")
}
if query == nil {
query = NewQueryParams()
}
query.Set("apiKey", c.token)
u := *c.url
u.Path = path.Join(u.Path, "api", apiVersion, spath)
u.RawQuery = query.Encode()
req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
req.Header = header
return req, nil
}
// Do http request, and return Response.
func (c *Client) do(method, spath string, header http.Header, body io.Reader, query *QueryParams) (*http.Response, error) {
req, err := c.newReqest(method, spath, header, body, query)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
return checkResponse(resp)
}
// Get method of http reqest.
// It creates new http reqest and do and return Response.
func (c *Client) get(spath string, query *QueryParams) (*http.Response, error) {
return c.do(http.MethodGet, spath, nil, nil, query)
}
// Post method of http reqest.
// It creates new http reqest and do and return Response.
func (c *Client) post(spath string, form *FormParams) (*http.Response, error) {
header := http.Header{}
header.Set("Content-Type", "application/x-www-form-urlencoded")
if form == nil {
form = NewFormParams()
}
return c.do(http.MethodPost, spath, header, form.NewReader(), nil)
}
// Patch method of http reqest.
// It creates new http reqest and do and return Response.
func (c *Client) patch(spath string, form *FormParams) (*http.Response, error) {
header := http.Header{}
header.Set("Content-Type", "application/x-www-form-urlencoded")
if form == nil {
form = NewFormParams()
}
return c.do(http.MethodPatch, spath, header, form.NewReader(), nil)
}
// Delete method of http reqest.
// It creates new http reqest and do and return Response.
func (c *Client) delete(spath string, form *FormParams) (*http.Response, error) {
header := http.Header{}
header.Set("Content-Type", "application/x-www-form-urlencoded")
if form == nil {
form = NewFormParams()
}
return c.do(http.MethodDelete, spath, header, form.NewReader(), nil)
}
// Upload file method used http reqest.
// It creates new http reqest and do and return Response.
func (c *Client) upload(spath, fileName string, r io.Reader) (*http.Response, error) {
if fileName == "" {
return nil, newClientError("fname is required")
}
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
w.Close()
fw, err := c.wrapper.CreateFormFile(w, fileName)
if err != nil {
return nil, err
}
if err = c.wrapper.Copy(fw, r); err != nil {
return nil, err
}
header := http.Header{}
header.Set("Content-Type", w.FormDataContentType())
return c.do(http.MethodPost, spath, header, &buf, nil)
}
// Check HTTP status code. If it has errors, return error.
func checkResponse(r *http.Response) (*http.Response, error) {
if sc := r.StatusCode; 200 <= sc && sc <= 299 {
return r, nil
}
if r.Body == nil {
return nil, newClientError("response body is empty")
}
defer r.Body.Close()
e := &APIResponseError{}
if err := json.NewDecoder(r.Body).Decode(e); err != nil {
return nil, err
}
return nil, e
}