-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbacklog.go
310 lines (265 loc) · 6.96 KB
/
backlog.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
package backlog
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/google/go-querystring/query"
"github.com/pkg/errors"
)
// httpClient defines the minimal interface needed for an http.Client to be implemented.
type httpClient interface {
Do(*http.Request) (*http.Response, error)
}
// Client : backlog client
type Client struct {
apiKey string
endpoint string
baseURL *url.URL
debug bool
log ilogger
httpclient httpClient
}
// Option defines an option for a Client
type Option func(*Client)
// OptionHTTPClient - provide a custom http client to the backlog client.
func OptionHTTPClient(client httpClient) func(*Client) {
return func(c *Client) {
c.httpclient = client
}
}
// OptionDebug enable debugging for the client
func OptionDebug(b bool) func(*Client) {
return func(c *Client) {
c.debug = b
}
}
// OptionLog set logging for client.
func OptionLog(l logger) func(*Client) {
return func(c *Client) {
c.log = internalLog{logger: l}
}
}
// New builds a backlog client from the provided token, baseURL and options
func New(apiKey, endpoint string, options ...Option) *Client {
baseURL, _ := url.Parse(endpoint)
s := &Client{
apiKey: apiKey,
endpoint: endpoint,
baseURL: baseURL,
httpclient: &http.Client{},
log: log.New(os.Stderr, "kenzo0107/backlog", log.LstdFlags|log.Lshortfile),
}
for _, opt := range options {
opt(s)
}
return s
}
// Debugf print a formatted debug line.
func (c *Client) Debugf(format string, v ...interface{}) {
if c.debug {
if err := c.log.Output(2, fmt.Sprintf(format, v...)); err != nil {
c.Debugln(err)
}
}
}
// Debugln print a debug line.
func (c *Client) Debugln(v ...interface{}) {
if c.debug {
if err := c.log.Output(2, fmt.Sprintln(v...)); err != nil {
c.Debugln(err)
}
}
}
// Debug returns if debug is enabled.
func (c *Client) Debug() bool {
return c.debug
}
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }
// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }
// Int64 is a helper routine that allocates a new int64 value
// to store v and returns a pointer to it.
func Int64(v int64) *int64 { return &v }
// NewRequest creates an API request. A relative URL can be provided in urlStr,
// in which case it is resolved relative to the BaseURL of the Client.
// Relative URLs should always be specified without a preceding slash. If
// specified, the value pointed to by body is JSON encoded and included as the
// request body.
func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
if strings.HasSuffix(c.baseURL.Path, "/") {
return nil, fmt.Errorf("baseURL must not have a trailing slash, but %q does", c.baseURL)
}
u, err := c.baseURL.Parse(c.baseURL.Path + urlStr)
if err != nil {
return nil, err
}
// add 'apiKey' to the url path
q := u.Query()
q.Set("apiKey", c.apiKey)
u.RawQuery = q.Encode()
var buf io.ReadWriter
if body != nil {
buf = &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
if er := enc.Encode(body); er != nil {
return nil, er
}
}
req, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
return req, nil
}
// UploadMultipartFile uploads multipart file
func (c *Client) UploadMultipartFile(ctx context.Context, method, urlStr, fpath, field string, v interface{}) (err error) {
fullpath, err := filepath.Abs(fpath)
if err != nil {
return err
}
file, err := os.Open(filepath.Clean(fullpath))
if err != nil {
return err
}
defer func() {
if er := file.Close(); er != nil {
err = er
}
}()
pr, pw := io.Pipe()
mw := multipart.NewWriter(pw)
errc := make(chan error)
go func() {
defer func() {
if er := pw.Close(); er != nil {
errc <- er
}
}()
ioWriter, er := mw.CreateFormFile(field, filepath.Base(fpath))
if er != nil {
errc <- er
return
}
_, errcp := io.Copy(ioWriter, file)
if errcp != nil {
errc <- errcp
return
}
if errcl := mw.Close(); errcl != nil {
errc <- errcl
return
}
}()
if strings.HasSuffix(c.baseURL.Path, "/") {
return fmt.Errorf("baseURL must not have a trailing slash, but %q does", c.baseURL)
}
u, err := c.baseURL.Parse(c.baseURL.Path + urlStr)
if err != nil {
return err
}
// add 'apiKey' to the url path
q := u.Query()
q.Set("apiKey", c.apiKey)
u.RawQuery = q.Encode()
req, err := http.NewRequest(method, u.String(), pr)
if err != nil {
return err
}
req.Header.Set("Content-Type", mw.FormDataContentType())
if err := c.Do(ctx, req, &v); err != nil {
return err
}
return nil
}
// Do sends an API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred. If v implements the io.Writer
// interface, the raw response body will be written to v, without attempting to
// first decode it. If rate limit is exceeded and reset time is in the future,
// Do returns *RateLimitError immediately without making a network API call.
//
// The provided ctx must be non-nil, if it is nil an error is returned. If it is canceled or times out,
// ctx.Err() will be returned.
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) error {
if ctx == nil {
return errors.New("context must be non-nil")
}
req = req.WithContext(ctx)
resp, err := c.httpclient.Do(req)
if err != nil {
// If we got an error, and the context has been canceled,
// the context's error is probably more useful.
select {
case <-ctx.Done():
return ctx.Err()
default:
}
return err
}
defer func() {
if er := resp.Body.Close(); er != nil {
err = er
}
}()
err = checkStatusCode(resp, c)
if err != nil {
return err
}
if v != nil {
if w, ok := v.(io.Writer); ok {
if _, er := io.Copy(w, resp.Body); er != nil {
return er
}
} else {
decErr := json.NewDecoder(resp.Body).Decode(v)
if decErr == io.EOF {
decErr = nil // ignore EOF errors caused by empty response body
}
if decErr != nil {
err = decErr
}
}
}
return err
}
// AddOptions adds the parameters in opt as URL query parameters to s. opt
// must be a struct whose fields may contain "url" tags.
func (c *Client) AddOptions(s string, opts interface{}) (string, error) {
v := reflect.ValueOf(opts)
if v.Kind() == reflect.Ptr && v.IsNil() {
return s, nil
}
u, err := url.Parse(s)
if err != nil {
return s, err
}
qs, err := query.Values(opts)
if err != nil {
return s, err
}
u.RawQuery = qs.Encode()
return u.String(), nil
}
type p struct {
Count int
}